1、整合原理
- 我们要将MyBatis配置整合到Spring的配置文件中;
- MyBatis配置文件中配置了: 数据源、别名、mapper映射文件,延迟加载
- 此时要将(数据源、别名、mapper映射文件)配置写入到Spring的配置文件中,所以mybatis配置文件中只写类的延时加载。
- Spring配置文件中新配置sqlSessionFactory对象,以及UserMapper对象(两种方式)
- 整合完成
2、整合代码
1、Spring配置文件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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置数据源-->
<!--引入数据库数据源-->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/>
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!--配置SqlSessionFactory工厂对象(这些配置是Mybatis配置,此时写在spring配置文件中)-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="datasource"/>
<!--配置开启别名(可选)-->
<property name="typeAliasesPackage" value="com.project.SSM.user4crud"/>
<!--关联映射文件-->
<property name="mapperLocations" value="classpath:com/project/SSM/user4crud/mapper/*.xml"/>
<!--关联mybatis主配置文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--sping和mybatis整合部分-->
<!--从工厂中获取usermapper接口的对象,mapper中有几个接口文件,就要写几个此方法-->
<!--<bean id="usermapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="SqlSessionFactory"/>
<property name="mapperInterface" value="com.project.SSM.user4crud.mapper.IUserMapper"/>
</bean>-->
<!--配置mapper接口扫描,替换上面方法(注解方式下使用)
从配置的包中,扫描所有的接口,并且创建接口的代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.project.SSM.user4crud.mapper"/>
<!-- mapper下有多个接口情况下,再用
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>-->
</bean>
</beans>
数据源配置,此处使用的是阿里的druid连接池
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///xxxxx?useUnicode=true&characterEncoding=UTF-8
username=root
password=xxxx
2、Mybatis配置文件:
<?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>
<!--配置别名,数据源,映射文件等等都需要写在spring配置文件中-->
<!--这里只配置延时加载-->
<!--配置延迟加载-->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="lazyLoadTriggerMethods" value=""/>
</settings>
</configuration>