Spring整合MyBatis

概述

  • 将实际需要的映射器,数据源,sqlsession,sqlSessionFactory 放到spring的ioc容器中
  • sqlsession转变为SqlSessionTemplate
  • mybatis配置文件的数据源,注册映射文件等任务都交给了spring的配置文件处理

<?xml version="1.0" encoding="UTF-8"?>
<!--每个网站后面要对应一个xsd-->
<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:mybatis="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://mybatis.org/schema/mybatis-spring
       http://mybatis.org/schema/mybatis-spring.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</beans>

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">
    <!--一般在这个文件只写别名,和setting,其他转移到spring配置文件-->
<configuration>

</configuration>

数据源

这里使用spring提供的数据源类,在创建sqlSessionFactory 时需要数据源

  <!--读取数据库的配置文件-->
<context:property-placeholder location="classpath:db.properties" />

    <!--配置数据源,连接数据库-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${password}"/>
        <property name="driverClassName" value="${driverClassName}"/>
    </bean>

sqlSessionFactory

  • 属性解释
    configLocation 读取mybatis配置文件
    mapper 读取mapper文件
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <!--        注入数据源-->
        <property name="dataSource" ref="dataSource"/>
<!--        读取mybatis配置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--        读取mapper-->
        <property name="mapperLocations" value="classpath:org/mapper/*.xml"/>
    </bean>

sqlSessionTemplate

如果不使用最原始的注册mapper方法,这个bean可以省略
注意: 使用构造器注入

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

映射文件

  • 环境:
    映射文件(UserMapper.xml)
    映射对应的接口(UserMapper.java)
    mybatis配置文件
  • 注:下面几种注册映射文件的方法中,最后一种最方便
1.基本方式

a.新建mapper接口的实现类

public class UserMapperImp implements UserMapper{
//    调用接口方法需要SqlSessionTemplate
    private final SqlSessionTemplate sqlSessionTemplate;

    public UserMapperImp(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    public List<User> selectById(int id) {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.selectById(id);
    }
}

b.为实现类配置bean

    <bean id="UserMapper1" class="org.mapper.UserMapperImp">
        <constructor-arg name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
    </bean>

c.使用

  ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring-config.xml");
  UserMapper mapper = cpx.getBean("UserMapper1",UserMapper.class);
  List<User> users = mapper.selectById(1);
2.实现类继承SqlSessionDaoSupport方式
  • 省略了 SqlSessionTemplate 这一步
  • SqlSessionDaoSupport 里面封装了SqlSessionTemplate,通过getSqlSessionTemplate就可以拿到

a.实现类继承SqlSessionDaoSupport 实现mapper接口

public class UserMapperSupportImp extends SqlSessionDaoSupport implements UserMapper {
//    父类提供的方法 getSqlSessionTemplate() 
    public List<User> selectById(int id){
        SqlSessionTemplate sqlSessionTemplate = getSqlSessionTemplate();
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.selectById(id);
    }
}

b.为实现类配置bean

    <bean id="UserMapper2" class="org.mapper.UserMapperSupportImp">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
3.MapperFactoryBean方式
  • 无需实现类
  • 名字是MapperFactoryBean的id
  • 属性
    mapperInterface: mapper接口
  • mapper和接口需要在同一个包下
  • 鸡肋在每一个mapper都需要这样建立一个bean

a.为MapperFactoryBean配置bean

    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="mapperInterface" value="org.mapper.UserMapper"/>
    </bean>

b.使用

UserMapper mapper = cpx.getBean("userMapper",UserMapper.class);
4.mybatis:scan 方式
  • 将一个包下的所有mapper注册。横扫饥饿,做回自己。
  • 名字是接口名, 首字母小写

a.声明命名空间

    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    http://mybatis.org/schema/mybatis-spring
    http://mybatis.org/schema/mybatis-spring.xsd

b.配置 base-package:需要扫描的包

<mybatis:scan base-package="org.mapper"/>

c.使用

        UserMapper mapper1 = cpx.getBean("userMapper",UserMapper.class);

        TestMapper mapper2 = cpx.getBean(TestMapper.class);

事务(aop,xml实现)

  • 通过aop给一个方法创建事务。
  • 流程 开启事务–配置事务通知-织入事务
ACiD

原子性; 过程原子
一致性: 前后数据库状态一致
隔离性: 事务之间隔离
持久性: 一旦提交,数据永久改变

<?xml version="1.0" encoding="UTF-8"?>
<!--每个网站后面要对应一个xsd-->
<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:mybatis="http://mybatis.org/schema/mybatis-spring"
       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-4.2.xsd
      http://mybatis.org/schema/mybatis-spring
       http://mybatis.org/schema/mybatis-spring.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</bean>
开启事务
<!--    开启事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource"/>
    </bean>
配置事务通知

name : 需要配置事务的方法
propagation :一般为REQUIRED,
read-only:用于select,只读

<!--    配置事务-->
    <tx:advice id="myTx" transaction-manager="transactionManager">
        <tx:attributes>
<!--                                                REQUIRED如果没有事务,新建事务,默认 -->
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="deleteById" propagation="REQUIRED"/>
<!--            <tx:method name="test" propagation="REQUIRED"/>-->
<!--                                          一次性完成-->
<!--            <tx:method name="*" propagation="REQUIRED"/>-->
        </tx:attributes>
    </tx:advice>
事务织入
<!--    事务织入-->
    <aop:config>
        <aop:pointcut id="cut" expression="execution(public * org.mapper.*.*(..))" />
        <aop:advisor advice-ref="myTx" pointcut-ref="cut"/>
    </aop:config>

目前不太理解为什么 tx:method 和切入点都在声明一种方法,感觉很多余。一个勉强的解释是
tx:method 是在声明一种规则,而aop:config在强调在哪里切入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值