MyBatis-Spring官方文档
导入依赖
<dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.3</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.0.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.1</version></dependency><!--spring操作数据库的话还需要这个--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.0.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.7</version></dependency></dependencies>
搭建MyBatis
看这篇文章
MyBatis基础与构建
第一种整合方式
创建Spring配置文件整合MyBatis
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--DataSource:使用Spring的数据源替换Mybatis的配置,c3p0 dbcp druid这里使用Spring提供的JDBC--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?useUnicode=true&characterEncoding=utf8"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!--sqlSessionFactory:--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!--绑定Mybatis配置文件--><property name="configLocation" value="classpath:mybatis-config.xml"/><property name="mapperLocations" value="classpath:com/lyd/mapper/*.xml"/></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><!--只能通过构造器注入,因为没有set方法--><constructor-arg index="0" ref="sqlSessionFactory"/></bean><bean id="empMapper" class="com.lyd.mapper.EmpMapper"><property name="sqlSession" ref="sqlSession"/></bean></beans>
创建Mapper接口的实现类
package com.lyd.mapper;import com.lyd.pojo.Emp;import org.mybatis.spring.SqlSessionTemplate;public interface IEmpMapper {Emp selectById();}public class EmpMapper implements IEmpMapper{//在原来所有操作都使用sqlSerssion来执行,,现在都是用SqlSessiontempleprivate SqlSessionTemplate sqlSession;public void setSqlSession(SqlSessionTemplate sqlSession){this.sqlSession=sqlSession;}@Overridepublic Emp selectById() {IEmpMapper mapper = sqlSession.getMapper(IEmpMapper.class);return mapper.selectById();}}
使用
@Testpublic void test1() throws IOException {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");IEmpMapper iempMapper =context.getBean("empMapper",IEmpMapper.class);Emp emp = iempMapper.selectById();System.out.println(emp.toString());}
第二种整合方式
创建一个新的Mapper接口实现类
package com.lyd.mapper;import com.lyd.pojo.Emp;import org.apache.ibatis.session.SqlSession;import org.mybatis.spring.support.SqlSessionDaoSupport;//继承SqlSessionDaoSupportpublic class EmpMapper2 extends SqlSessionDaoSupport implements IEmpMapper{@Overridepublic Emp selectById() {//通过getSqlSession方法就可以得到一个sqlSession对象SqlSession sqlSession =getSqlSession();IEmpMapper empMapper = sqlSession.getMapper(IEmpMapper.class);return empMapper.selectById();}}
改进Spring配合文件
sqlSession就不用写了,少一步,其他的一样,写上刚才新的Mapper实现类
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--DataSource:使用Spring的数据源替换Mybatis的配置,c3p0 dbcp druid这里使用Spring提供的JDBC--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?useUnicode=true&characterEncoding=utf8"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!--sqlSessionFactory:--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!--绑定Mybatis配置文件--><property name="configLocation" value="classpath:mybatis-config.xml"/><property name="mapperLocations" value="classpath:com/lyd/mapper/*.xml"/></bean><!--sqlSession--><!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">--><!-- <!–只能通过构造器注入,因为没有set方法–>--><!-- <constructor-arg index="0" ref="sqlSessionFactory"/>--><!-- </bean>--><!--上面都是固定的写法:下面都是才是自己编写的实现类,有多少实现类就编写多少bean注入sqlSession对象--><bean id="empMapper2" class="com.lyd.mapper.EmpMapper2"><!--继承的父类需要注入这个sqlSessionFactory--><property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean></beans>
使用
@Testpublic void test1() throws IOException {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");IEmpMapper iempMapper =context.getBean("empMapper2",IEmpMapper.class);Emp emp = iempMapper.selectById();System.out.println(emp.toString());}
