一、导入依赖
MyBatis-Spring 需要以下版本:
MyBatis-Spring | MyBatis | Spring 框架 | Spring Batch | Java |
---|---|---|---|---|
2.0 | 3.5+ | 5.0+ | 4.0+ | Java 8+ |
1.3 | 3.4+ | 3.2.2+ | 2.1+ | Java 6+ |
导入pom依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
二、数据源、sqlSessionFactory、sqlSession
在配置文件spring-dao.xml中配置
dataSource:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
sqlSessionFactory:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:cn/kexing/mapper/*.xml"></property>
</bean>
SqlSessionTemplate:(官方推荐使用Template)
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
:注入mybatis核心配置文件,spring内部集成了mybaits后,mybatis核心配置文件一般只写typeAliases
和setting
一些设置。
三、接口、mapper、实现类
package cn.kexing.mapper;
import cn.kexing.pojo.Student;
import java.util.List;
public interface StudentMapper {
public List<Student> selectAll();
}
<?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="cn.kexing.mapper.StudentMapper">
<select id="selectAll" resultType="student">
select * from student
</select>
</mapper>
实现类:这里相对于mybatis单独使用多了一个实现类,以往我们都是在测试代码中getMapper,因为整合了spring,这里通过set注入方式将sqlSession封装在实现类中以此来调用getmapper实现对应的方法
package cn.kexing.mapper;
import cn.kexing.pojo.Student;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class StudentMapperImpl implements StudentMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<Student> selectAll() {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.selectAll();
}
}
注入sqlSession
<bean id="studentMapperImpl" class="cn.kexing.mapper.StudentMapperImpl">
<property name="sqlSession" ref="sqlSession"></property>
</bean>
四、测试
@Test
public void select() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
StudentMapperImpl studentMapperImpl = context.getBean("studentMapperImpl", StudentMapperImpl.class);
for (Student student : studentMapperImpl.selectAll()) {
System.out.println(student);
}
}
在测试中我们几乎看不到mybaits的存在
Output
Student{id=1, name='小明', tid=1}
Student{id=2, name='小红', tid=1}
Student{id=3, name='小张', tid=1}
Student{id=4, name='小李', tid=1}
Student{id=5, name='小王', tid=1}