title: spring-3-整合mybatis
data:
tags:
- java
- ssm框架
categories: spring5
版本和依赖
MyBatis-Spring 需要以下版本:
maven依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
第一种方式
spring核心配置文件(IOC)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bean="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<bean:scan base-package="com.wang"/>
<!--类似于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?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--在这个sqlSessionFactoryBean中可以配置几乎全部的mybatis-config.xml的配置项-->
<!--也可以把这个和mybatis-config.xml文件绑定起来
示例:
<property name="configuration" value="classpath:mybatis-config.xml"/>-->
<!--但是:
我们一般只在这个地方子需要配置数据源和连接mybatis-config.xml配置文件就可以了-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--sqlSession:mybatis-spring写了一个模板专门用来帮助我们造sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--构造时必须要有参数-->
<!--且:只能用构造函数加参数,因为无set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--把我们做好的service层构造放入IOC容器中-->
<!--我们要用的时候直接取这个EmployeeMapperImpl,就可以直接获取我们要的数据了-->
<bean id="EmployeeMapperImpl" class="com.wang.service.EmployeeMapperImpl">
<!--帮助我们把sqlSessionTemplate注入-->
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
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>
<settings>
<!--日志开关,此处为默认-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<!--开启懒加载,在需要的时候才会读取数据 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭强制加载,即关闭在加载实例的时候就读取全部数据-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<!--package是为这个包下的类都创建一个开头小写的别名-->
<package name="com.wang"/>
</typeAliases>
<mappers>
<mapper resource="mapper/EmployeeMapper.xml"/>
</mappers>
</configuration>
多了一个service的层(用来直接获取Mapper接口实例化好的对象,直接调用对应的方法)
//使用这个类之前需要先传入一个从spring配置文件中得到的sqlSessionTemplate对象
//我们这里一般在springIOC容器中帮助注入
@Component
public class EmployeeMapperImpl implements EmployeeMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<Employee> queryEmployees() {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
return mapper.queryEmployees();
}
}
测试:
@org.junit.Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");
EmployeeMapperImpl employeeMapperImpl = context.getBean("EmployeeMapperImpl", EmployeeMapperImpl.class);
List<Employee> employees = employeeMapperImpl.queryEmployees();
Iterator<Employee> iterator = employees.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
第二种方式
主要在service层发生了修改
@Component
public class EmployeeMapperImpl extends SqlSessionDaoSupport implements EmployeeMapper {
//我们这里直接继承sqlSessionDaoSupport,使用里面的getSqlSession方法直接获取sqlSession
//而不是用DI的方式取注入sqlSession
private SqlSession sqlSession = getSqlSession();
// public void setSqlSession(SqlSessionTemplate sqlSession) {
// this.sqlSession = sqlSession;
// }
public List<Employee> queryEmployees() {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
return mapper.queryEmployees();
}
不需要DI之后
我们就不需要再IOC容器中管理这些Service层了
- 便于管理
- 便于事务操作
在Application.xml核心配置文件中:
- 我们不需要再把sqlsessiontemplate也放入到IOC容器中了
- 但是在我们的service层类中仍需要放到IOC容器中,不过只需要再注入sqlSessionFactory类就可以了
<?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">
<!-- driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
username = root
password = 123456-->
<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?useUnicode=true&characterEncoding=utf-8"/>
<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"/>
</bean>
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
<!-- <constructor-arg index="0" ref="sqlSessionFactory"/>-->
<!-- </bean>-->
<!-- <bean id="EmployeeMapper" class="com.wang.service.EmployeeMapperImpl">-->
<!-- <property name="sqlSession" ref="sqlSession"/>-->
<!-- </bean>-->
<bean id="userEmployeeMapper" class="com.wang.service.EmployeeMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>