mybatis-spring 整合

一。注:建一个普通的web项目所需要的jar包:

 

二。总共有两种集成方式

1实现dao层service接口

package cn.sm1234.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sm1234.dao.CustomerMapper;
import cn.sm1234.domain.Customer;

public class CustomerMapperImpl extends SqlSessionDaoSupport implements CustomerMapper {

    public void saveCustomer(Customer customer) {
        SqlSession sqlSession = this.getSqlSession();
        sqlSession.insert("saveCustomer",customer);
        //这里不需要事务提交
    }

}

从上述代码中不难看到,它不仅仅实现了接口,还通过集成SqlSessionDaoSupport获得了sqlsession,其实这是个假象,翻看源码你就可以看到他并没有产生sqlsessionfactorybuilder对象的数据源以及对数据库进行操作的sql文件,,所以他要整合数据源,以及sql文件,进而就产生了mybatis-spring的另一个配置文件,如下:

<?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">
    
    <!-- 读取jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 创建DataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>    
    
    <!-- 创建SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 关联连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载sql映射文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    
    <!-- 创建CustomerMapperImpl对象,注入SqlSessionFactory -->
    <bean id="customerMapper" class="cn.sm1234.dao.impl.CustomerMapperImpl">
        <!-- 关联sqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
</beans>

上面数据库连接池使用的是commens dbcp工具,这个jar包的其中一个核心类起到了重要的作用,这个核心类就是BasicDataSource,也可以使用c3p0连接池;整合数据源的同时,还能加载sql文件,mybatis-spring这个jar包就起到了关键性的作用,它里面有个sqlsessionfactorybean核心类,就有此功能。当然这里的每一个bean标签的id不要随意起名字,会报错的,,,,,,

二。第二个就是不用实现dao层的接口,可以省略一些代码。。。这里只要把dao蹭的接口整合到配置文件里面即可,同时注入sqlsessionfactory类即可,下面就是一个简单的配置类:能实现这个功能,还是全靠了mybatis-spring这个jar包里面的MapperFactoryBean这个类

<?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">
    
    <!-- 读取jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 创建DataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>    
    
    <!-- 创建SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 关联连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载sql映射文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!-- 配置Mapper接口 -->
    <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 关联Mapper接口 -->
        <property name="mapperInterface" value="cn.sm1234.dao.CustomerMapper"/>
        <!-- 关联SqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>

三。测试类

package cn.sm1234.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.sm1234.dao.CustomerMapper;
import cn.sm1234.domain.Customer;


public class MyBatisSpringTest {

    @Test
    public void test(){
        //1.加载spring配置
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //2.获取对象
        CustomerMapper customerMapper = (CustomerMapper)ac.getBean("customerMapper");
        
        //3.调用方法
        Customer customer = new Customer();
        customer.setName("小美");
        customer.setGender("");
        customer.setTelephone("020-666666");
        customer.setAddress("广州体育中心");
        
        customerMapper.saveCustomer(customer);
    }
}

扫描sql映射文件、dao层接口、spring ioc、开启事务管理配置如下,细节上的东西太多了,配置了N边才不出错了,,,

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-4.2.xsd">
    
    <!-- 读取jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 创建DataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>    
    
    <!-- 创建SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 关联连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载sql映射文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!-- Mapper接口的扫描 -->
    <!-- 
        注意:如果使用Mapper接口包扫描,那么每个Mapper接口在Spring容器中的id名称为类名: 例如 CustomerMapper -> customerMapper
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置Mapper接口所在包路径  -->
        <property name="basePackage" value="cn.sm1234.dao"/>
    </bean>
    
    <!-- 开启Spring的IOC注解扫描 -->
    <context:component-scan base-package="cn.sm1234"/>
    
    <!-- 开启Spring的事务 -->
    <!-- -事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 启用Spring事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

配置文件具体可参考:https://blog.csdn.net/weixin_38322156/article/details/72154976

原文地址:https://www.cnblogs.com/gxlaqj/p/11337584.html