一、dao接口+实现类的方式
1、先创建好整合工程结构
2、对于Spring和Mybatis 整合,我们先从数据库开始,即先创建一张简单的数据表,Sql如下
CREATE TABLE `t_user` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `username` VARCHAR(100) DEFAULT NULL, `password` VARCHAR(100) DEFAULT NULL, `address` VARCHAR(100) DEFAULT NULL, `sex` VARCHAR(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
然后根据从上面的数据表创建对应的实体类
1 package cn.test.ssm.po; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private int id; 7 private String username; 8 private String password; 9 private String address; 10 private String sex; 11 12 public int getId() { 13 return id; 14 } 15 16 public String getUsername() { 17 return username; 18 } 19 20 public String getPassword() { 21 return password; 22 } 23 24 public String getAddress() { 25 return address; 26 } 27 28 public String getSex() { 29 return sex; 30 } 31 32 public void setId(int id) { 33 this.id = id; 34 } 35 36 public void setUsername(String username) { 37 this.username = username; 38 } 39 40 public void setPassword(String password) { 41 this.password = password; 42 } 43 44 public void setAddress(String address) { 45 this.address = address; 46 } 47 48 public void setSex(String sex) { 49 this.sex = sex; 50 } 51 52 public User(String username, String password, String address, String sex) { 53 this.username = username; 54 this.password = password; 55 this.address = address; 56 this.sex = sex; 57 } 58 59 public User() { 60 } 61 62 @Override 63 public String toString() { 64 return "User{" + 65 "id=" + id + 66 ", username=‘" + username + ‘\‘‘ + 67 ", password=‘" + password + ‘\‘‘ + 68 ", address=‘" + address + ‘\‘‘ + 69 ", sex=‘" + sex + ‘\‘‘ + 70 ‘}‘; 71 } 72 }
3、然后我们开始配置Mybatis的相关配置文件,具体有Mybatis的核心配置文件SqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <typeAliases> 8 <!--批量别名定义:Mybatis在定义别名的时候会自动扫描包中的po类,自动的将别名定义为类名(首字母大写或者小写都可以)--> 9 <package name="cn.test.ssm.po"></package> 10 </typeAliases> 11 12 <!--配置一个SQL语句和映射的配置文件--> 13 <mappers> 14 <mapper resource="sqlmap/UserMapper.xml" /> 15 </mappers> 16 17 </configuration>
然后就是配置UserMapper.xml映射文件,由于只是测试Spring和Mybatis的整合,所以可以只是实现相对简单的功能,这样的话,UserMapper配置文件也就比较简单
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!--mapper为根元素,namespace指定了命名空间--> 6 <mapper namespace="test"> 7 8 <select id="findUserById" parameterType="int" resultType="cn.test.ssm.po.User"> 9 SELECT * FROM t_user WHERE id = #{id} 10 </select> 11 12 </mapper>
4、下来就是根据UserMapper中的statment实现的功能,写dao接口文件
1 package cn.test.ssm.dao; 2 3 import cn.test.ssm.po.User; 4 5 public interface UserDao { 6 7 public User findUserById(int id) throws Exception; 8 }
5、我们编写dao接口的实现类。在dao接口的实现类过程中,我们需要知道,由于Spring整合Mybatis的原因,Spring需要管理SqlSessionFactory,而实现类方法中需要得到这个SqlSessionFactory,这里我们可以继承SqlSessionDaoSupport这个类,我们看一下这个类中的方法
6、下来我们就具体的配置applicationContext配置文件,其中需要配置数据库信息,加载其他的配置文件,管理配置SqlSessionFactory(要加载Mybatis核心配置文件),然后我们管理UserDao接口的bean,同上面分析的,就需要配置SqlSessionFactory这个属性
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx.xsd"> 16 17 18 <!--加载数据库信息的配置文件--> 19 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> 20 21 <!--配置数据源--> 22 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 23 <property name="driverClass" value="${jdbc.driver}" /> 24 <property name="jdbcUrl" value="${jdbc.url}" /> 25 <property name="user" value="${jdbc.username}" /> 26 <property name="password" value="${jdbc.password}" /> 27 </bean> 28 29 <!--配置SqlSessionFactory--> 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <!--加载Mybatis的配置文件--> 32 <property name="configLocation" value="mybatis/SqlMapConfig.xml"></property> 33 <!--配置数据源--> 34 <property name="dataSource" ref="dataSource"></property> 35 </bean> 36 37 <!--配置Dao接口--> 38 <bean id="userDao" class="cn.test.ssm.dao.UserDaoImpl"> 39 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 40 </bean> 41 </beans>
7、编写好applicationContext之后,就开始实现UserDao这个接口,也是如同上面的分析,实现SqlSessionDaoSupport这个抽象类,从而得到SqlSessionFactory,在方法体内获取SqlSession
1 package cn.test.ssm.dao; 2 3 import cn.test.ssm.po.User; 4 import org.apache.ibatis.session.SqlSession; 5 import org.mybatis.spring.support.SqlSessionDaoSupport; 6 7 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ 8 9 // SqlSessionDaoSupport中有 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) 方法 10 11 @Override 12 public User findUserById(int id) throws Exception { 13 SqlSession sqlSession = getSqlSession(); //SqlSessionDaoSupport中有private SqlSession sqlSession;这个属性和方法 14 User user = sqlSession.selectOne("test.findUserById",1); 15 //使用spring来记性管理,在方法结束的时候就会自己关闭SqlSession 16 return user; 17 } 18 }
8、编写相应的Junit进行测试
1 package cn.test.ssm.dao; 2 3 import cn.test.ssm.po.User; 4 import org.apache.ibatis.session.SqlSession; 5 import org.mybatis.spring.support.SqlSessionDaoSupport; 6 7 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ 8 9 // SqlSessionDaoSupport中有 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) 方法 10 11 @Override 12 public User findUserById(int id) throws Exception { 13 SqlSession sqlSession = getSqlSession(); //SqlSessionDaoSupport中有private SqlSession sqlSession;这个属性和方法 14 User user = sqlSession.selectOne("test.findUserById",1); 15 //使用spring来记性管理,在方法结束的时候就会自己关闭SqlSession 16 return user; 17 } 18 }
通过上面的测试程序得到结果
原文:https://www.cnblogs.com/fsmly/p/10347277.html