1.需要的jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
以及target目录资源导出失败的解决方案!
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.配置Spring-dao.xml
1.创建SqlSessionFactory
在spring-dao里,需要创建SqlSessionFactory的话 ,需要注入数据源。由spring来替代mybatis管理数据源datasource。
<!--注册Datasource的bean-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8;useSSL=false;useUnicode=true;CharacterEncoding=utf8"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<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的配置文件mybatis-config.xml,可以省略-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--登记**mapper.xml如userMapper.xml-->
<property name="mapperLocations" value="classpath:com/kxy/mapper/*.xml"/>
</bean>
<property name="mapperLocations" value="classpath:com/kxy/mapper/*.xml"/>
实质上就是原来mybatis的登记mapper的操作。也就是说登记mapper的活也交给spring管理了。
<mappers>
<mapper class="com.kxy.mapper.UserMapper"/>
</mappers>
最后其实mybatis的配置文件mybatis-config.xml可以不要。全权交给spring管理。
2.创建sqlsession
注入sqlsessionFactory对象
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
既然创建了sqlsession,那么就可以拿到它的mapper了,通过mapper又可以调用相应的方法。因此我们需要一个实现类去封装这一块。
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> getAllUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getAllUser();
}
}
并且将实现类注入到容器中去:
SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现。当调用 SQL ,SqlSessionTemplate 将会保证使用的 SqlSession 与当前 Spring 的事务相关。 此外,它管理 session 的生命周期,包含必要的关闭、提交或回滚操作。SqlSessionTemplate 是线程安全的,可以被多个 DAO 或映射器所共享使用。
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="userMapper" class="com.kxy.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
Springdao.xml关于sqlsessionFactory和sqlsession的创建的代码实质上是固定的。参考下列代码。
<?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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true;characterEncoding=utf-8;useSSL=false;serverTimezone=GMT%2B8"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<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/kxy/mapper/userMapper.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="userMapperImpl" class="com.kxy.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
总结
在配置spring-dao的时候,其核心的两个bean实质上是sqlsessionFactory和sqlsessionTemperlate(但是我们在使用的时候将他习惯命名成sqlsession)。而datasource数据源不止一种,像本案例中的是采用的jdbc的数据源,也是最常用的一种数据源。
值得注意的是:你必须要登记注册某个功能实现的xml,这一步可以在注册sqlsessionFactory的时候登记,即我们所说的spring接管myabatis实现登记xml。那么如果spring做了这个事情,mybatis核心配置文件就不需要再去配置了,如果你两边都配置了,则会导致报异常。所以,一般来讲,我们习惯用spring去做登记操作,那么mybatis里相关的登记操作就要去掉了。到最后你会发现,mybatis核心配置文件里的配置都没有了,你就可以删掉核心配置文件了,在sqlsessionFactory注入的时候也可以取消绑定mybatis
使用SqlSessionDaoSupport简化实体类注入sqlsession
1.实现类继承SqlSessionDaoSupport
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
public List<User> getAllUser() {
return getSqlSession().getMapper(UserMapper.class).getAllUser();
}
}
只要实现类继承了SqlSessionDaoSupport 类,便不需要再使用setter去注入sqlsession了,因为在SqlSessionDaoSupport 的源码里spring已经帮我们做了这个操作,当我们在实现方法里调用getSqlSession就可以得到一个sqlsession。
2.注册实现类的bean(SqlSessionFactory和SqlSessionTemplate注入)
<bean id="userMapperImpl2" class="com.kxy.mapper.UserMapperImpl2">
<property name="sqlSessionTemplate" ref="sqlSession"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>