目录
Spring整合mybatis步骤
方法一
1.替换mybatis的DataSource,注册到Spring,由Spring管理
spring管理jdbc
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.10</version>
</dependency>
2.替换SqlSessionFactory
用org.mybatis.spring.SqlSessionFactoryBean
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
**在SplSessionFactory中绑定mybatis配置文件**
3.创建sqlSessionTemplate
4.在spring中注册dao(spring-dao.xml)
5.注册bean
测试
spring-dao.xml
<?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.xsd">
<!--1.DataSource:使用Spring的数据源替换Mybatis的配置-->
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--2.导入SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<!--绑定配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/my/mapper/*.xml"/>
</bean>
<!--3.sqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
applicationContext.xml
<?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.xsd">
<!--4.注册dao-->
<import resource="spring-dao.xml"/>
<!--5.注册bean-->
<bean id="userMapperImpl" class="com.my.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>
</beans>
pojo
@Data
public class User {
private int id;
private String name;
private String pwd;
}
1.userMapper
public interface UserMapper {
public List<User> queryAll();
}
2.userMapper.xml
<mapper namespace="com.my.mapper.UserMapper">
<select id="queryAll" resultType="user">
select * from mybatis.user
</select>
</mapper>
3.userMapperImpl.xml
public class UserMapperImpl implements UserMapper{
//原先,所有的操作,都是用SqlSession来执行,现在有用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
//由spring配置文件赋值
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> queryAll() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.queryAll();
}
}
4.mybatis-config.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>
<typeAliases>
<package name="com.my.pojo"/>
</typeAliases>
</configuration>
Test
@Test
public void my1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapperImpl userMapperImpl = context.getBean("userMapperImpl", UserMapperImpl.class);//spring中id名
List<User> list = userMapperImpl.queryAll();
for (User user : list) {
System.out.println(user);
}
}
方法二
UserMapperImpl2 .java
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> queryAll() {
return getSqlSession().getMapper(UserMapper.class).queryAll();
}
}
注意:SqlSessionDaoSupport 能获取getSqlSession。所以代替spring配置文件中的sqlSessionTemplate
spring-dao.xml
<?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.xsd">
<!--1.DataSource:使用Spring的数据源替换Mybatis的配置-->
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--2.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<!--绑定配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/my/mapper/*.xml"/>
</bean>
</beans>
applicationContext.xml
<?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.xsd">
<!--3.注册dao-->
<import resource="spring-dao.xml"/>
<!--4.注册bean-->
<bean id="userMapperImpl2" class="com.my.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>