Spring整合MyBatis的原理及实现

1、整合原理

  1. 我们要将MyBatis配置整合到Spring的配置文件中;
  2. MyBatis配置文件中配置了: 数据源别名mapper映射文件延迟加载
  3. 此时要将(数据源、别名、mapper映射文件)配置写入到Spring的配置文件中,所以mybatis配置文件中只写类的延时加载。
  4. Spring配置文件中新配置sqlSessionFactory对象,以及UserMapper对象(两种方式)
  5. 整合完成

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>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值