一、
1. 实体bean
package com.mybatisBean; public class User { private Integer id; private String name; private String age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
2.dao类
package com.mybatisDao; import java.util.List; import com.mybatisBean.User; public interface UserDao { public List<User> selectAll(); }
3.dao 映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mybatisDao.UserDao"> <select id="selectAll" resultType="com.mybatisBean.User"> select * from user </select> </mapper>
4.配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> --> <mappers> <mapper resource="mybatisMapper/UserMapper.xml"/> </mappers> </configuration>
5. 测试
package com.mybatisTest; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mybatisBean.User; import com.mybatisDao.UserDao; /* * mybatis 第一种方式 * 1. 创建实体,2. 创建dao ,声明接口 3. 创建dao 对应的映射文件 * 4. 创建mybatis 配置文件,声明数据库连接,配置映射文件 5.通过mybatis配置文件和sessionfactoryBuilder引用 * 第一种方式在 mybatis 配置文件中配置数据库链接 */ public class TestUser { public static void main(String[] args) throws Exception{ String resource="mybatis-configuration.xml"; Reader reader=Resources.getResourceAsReader(resource); SqlSessionFactoryBuilder bulider=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=bulider.build(reader); SqlSession session=factory.openSession(); UserDao userdao=session.getMapper(UserDao.class); List<User> list=userdao.selectAll(); for(User user:list){ System.out.println(user.getName()+":"+user.getAge()); } session.commit(); session.close(); } }
jar 包 ,主要有 mybatis-3.1.1.jar 和 mysql-connector-java-5.1.jar
二、
多出的部分。
1. service 接口和实现
package com.mybatisService.User; import java.util.List; import com.mybatisBean.User; public interface UserService { public List<User> selectAll(); } package com.mybatisService.User; import java.util.List; import com.mybatisBean.User; import com.mybatisDao.UserDao; public class UserImpl implements UserService{ private UserDao userdao; public UserDao getUserdao() { return userdao; } public void setUserdao(UserDao userdao) { this.userdao = userdao; } @Override public List<User> selectAll() { return this.userdao.selectAll(); } }
2. spring 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-configuration.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatisDao.UserDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <bean id="userservice" class="com.mybatisService.User.UserImpl"> <property name="userdao" ref="userDao"></property> </bean> </beans>
mybatis 配置文件中的 数据库链接不需要了 。
基于以上 添加 mybatis
添加jar 包 mybats-3.1.jar mysql-connector-java-5.1 mybatis-spring.jar ,commons-dbcp,commons-pool,spring-jdbc 等
UserDao.java
package com.mybatisdao; import java.util.List; import com.bean.User; public interface UserDao { public List<User> selectAll(); }
UserDaoMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mybatisdao.UserDao"> <select id="selectAll" resultType="com.bean.User"> select * from user </select> </mapper>
mybatis-configuration.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"/> <property name="username" value="root"/> <property name="password" value="***"/> </dataSource> </environment> </environments> --> <!--结合spring 时要在Spring 配置文件中配置 数据库链接--> <mappers> <mapper resource="com/mybatisMapper/UserDaoMapper.xml"/> </mappers> </configuration>
更改HappyNewYearIml.java
package com.service; import java.util.List; import javax.jws.WebService; import com.bean.Person; import com.bean.User; import com.mybatisdao.UserDao; @WebService(endpointInterface = "com.service.HappyNewYear") public class HappyNewYearImpl implements HappyNewYear { private UserDao userdao; //主要改动的地方 public UserDao getUserdao() { return userdao; } public void setUserdao(UserDao userdao) { this.userdao = userdao; } public String sayHello(Person person) { List<User> list=userdao.selectAll(); String ss=""; for(User u:list){ if(u.getId()+""==person.getFirstName()){ ss=u.getName()+"年龄"+u.getAge(); } } return ss==""?"没有":ss; } }
applicationContext.xml更改
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="person" class="com.bean.Person"></bean> <bean id="restSample" class="com.rest.HelloLove"></bean> <context:component-scan base-package="com.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
<!--主要更改部分begin-->
<!-- 下面3个bean 的 class 需要导入 mybatis-spring.jar ,commons-dbcp,commons-pool,spring-jdbc --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:com/resoureces/mybatis-configuration.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatisdao.UserDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <bean id="happyNewYearImpl" class="com.service.HappyNewYearImpl"> <property name="userdao" ref="userDao"></property> </bean> <!-- 当jaxws:endpoint的 implementor需要引入Spring bean 时 要加 # ,这是第一种方式 --> <jaxws:endpoint id="greetServicce" implementor="#happyNewYearImpl" address="/greetServicce" /> <!-- 第二种方式 --> <!-- <jaxws:endpoint id="greetServicce" implementorClass="com.service.HappyNewYearImpl" address="/greetServicce"> <jaxws:implementor> <bean ref="userDao"></bean> 找不到ref 属性 ,应该是版本问题 </jaxws:implementor> </jaxws:endpoint> --> <!--主要更改部分end--> <jaxrs:server id="restServiceContainer" address="/rest"> <jaxrs:serviceBeans> <ref bean="restSample"/> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:languageMappings> <entry key="en" value="en-gb"/> </jaxrs:languageMappings> </jaxrs:server> </beans>
测试
package com.test; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.bean.User; import com.mybatisdao.UserDao; public class testmybatis { public static void main(String[] args) throws IOException{ String resource="com/resoureces/mybatis-configuration.xml"; Reader reader=Resources.getResourceAsReader(resource); SqlSessionFactoryBuilder bulider=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=bulider.build(reader); SqlSession session=factory.openSession(); UserDao userdao=session.getMapper(UserDao.class); List<User> list=userdao.selectAll(); for(User u:list){ System.out.println(u.getName()+"年龄"+u.getAge()); } session.commit(); session.close(); } }
请结合 http://www.cnblogs.com/zhangchenglzhao/p/3505176.html