Spring整合Mybatis
使用Spring整合Mybatis时我们可以不需要mybatis_config.xml文件。,有这个文件只是说明我们用到了MyBatis
什么是MyBatis-Spring?
Mybaits-Spring 会帮助你将MyBaits代码无缝地整合到Spring中,它将允许Mybatis参与到Spring的事物管理之中,创建映射器mapper和SqlSession注入到bean中。
SqlSessionTemplate:
SqlSessionTemplate是Mybatis-Spring的核心,作为SqlSession的一个实现,意味着可以无缝替代你代码中已经在实现的sqlsession。
使用SqlSessionTemplate是因为它是线程安全的
配置
首先在pom.xml文件中导入相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-study</artifactId>
<groupId>com.yu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-10-mybatis</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring 操作数据库的话还需要一个Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.14</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
然后在spring配置文件中依次配置 dataSource,sessionFactory,SqlSessionTemplate
这里新建配置文件spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 数据源 -->
<!-- DataSource:使用Spring数据源替换mybatis的配置-->
<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/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</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/yu/mapper/*.xml"/>
</bean>
<!--配置:就是我们使用的SQLSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入SqlSessionTemplate,因为他没有set方法 -->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
在mybaits-confi.xml文件中配置
这个文件中的内容spring也可以配置,
但是一般在这个配置文件中留下两项,一个是别名管理,一个是设置,
<?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>
<typeAliases>
<!--<typeAlias alias="Book" type="com.yu.pojo.Book" />-->
<package name="com.yu.pojo"/>
</typeAliases>
<!-- 设置-->
<!-- <settings>-->
<!-- <setting name="" value=""/>-->
<!-- </settings>-->
</configuration>
实体类:Student.java
package com.yu.pojo;
public class Student {
private int id;
private String name;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
映射接口:StudentMapper.java
package com.yu.mapper;
import com.yu.pojo.Student;
import java.util.List;
public interface StudentMapper {
public List<Student> selectStudent();
}
映射文件:StudentMapper.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yu.mapper.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>
<select id="selectStudent" resultMap="StudentResult">
SELECT * FROM student
</select>
</mapper>
我们还需要新建一个类:StudentMapperImpl.java
package com.yu.mapper;
import com.yu.pojo.Student;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class StudentMapperImpl implements StudentMapper{
//以前都是使用sqlsession,现在都使用SqlSessionTemplate
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<Student> selectStudent() {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
List<Student> students = mapper.selectStudent();
return students;
}
}
然后在applicationContext.xml配置文件中注入自己写的实现类,并且引入spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<import resource="spring-dao.xml"/>
<bean id="studentMapper" class="com.yu.mapper.StudentMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
</beans>
最后测试:
import com.yu.mapper.StudentMapper;
import com.yu.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Mytest {
@Test
public void test1() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentMapper studentMapper = context.getBean("studentMapper", StudentMapper.class);
for (Student student : studentMapper.selectStudent()) {
System.out.println(student);
}
}
}
Spring整合Mybatis 方法二
直接继承SqlSessionDaoSupport
SqlSessionDaoSupport 是一个抽象的支持类,用来为你提供sqlsession,调用getSqlSesion() 方法你会得到一个sqlsessionTemplate,之后可以执行SQL方法。
package com.yu.mapper;
import com.yu.pojo.Student;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{
@Override
public List<Student> selectStudent() {
return getSqlSession().getMapper(StudentMapper.class).selectStudent();
}
}
现在注入到spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<import resource="spring-dao.xml"/>
<bean id="studentMapper" class="com.yu.mapper.StudentMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
<bean id="studentMapper2" class="com.yu.mapper.StudentMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
测试: