栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-spring整合——这可太详细了

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-spring整合——这可太详细了

spring-mybatis整合

文章目录
  • spring-mybatis整合
    • 1.环境配置
      • 1.1导入依赖:
        • sringAop
        • spring核心
        • springJdbc
        • springTx
        • springTest
        • mybatis
        • 整合中间件
        • 数据库驱动
        • 数据源
        • 日志
        • 辅助工具
        • 测试
        • mvn配置
      • 1.2 数据库配置
      • 1.3 applicationContext.xml
      • 1.4 mybatis-config.xml
      • 1.5 log4j2.xml
    • 2.整合方式
      • 2.1 传统Dao开发整合
        • 定义接口:
        • 编写实现类:
        • 测试:
      • 2.2 Mapper接口方式开发整合
        • 定义接口:
        • 定义xml:
        • 注册/不注册:
        • 测试:
      • 2.3 MapperScannerConfigurer
      • 2.4 整合事务
        • 开启事务注解
        • 开启spring注解扫描
        • service层
    • 1.环境配置
      • 1.1导入依赖:
        • sringAop
        • spring核心
        • springJdbc
        • springTx
        • springTest
        • mybatis
        • 整合中间件
        • 数据库驱动
        • 数据源
        • 日志
        • 辅助工具
        • 测试
        • mvn配置
      • 1.2 数据库配置
      • 1.3 applicationContext.xml
      • 1.4 mybatis-config.xml
      • 1.5 log4j2.xml
    • 2.整合方式
      • 2.1 传统Dao开发整合
        • 定义接口:
        • 编写实现类:
        • 测试:
      • 2.2 Mapper接口方式开发整合
        • 定义接口:
        • 定义xml:
        • 注册/不注册:
        • 测试:
      • 2.3 MapperScannerConfigurer
      • 2.4 整合事务
        • 开启事务注解
        • 开启spring注解扫描
        • service层

1.环境配置 1.1导入依赖: sringAop
       
            
        
            aopalliance
            aopalliance
            1.0
        
        
            org.aspectj
            aspectjweaver
            1.9.7
        
        
            org.springframework
            spring-aop
            5.2.12.RELEASE
        
        
            org.springframework
            spring-aspects
            5.2.12.RELEASE
        
spring核心
    
            org.springframework
            spring-beans
            5.2.12.RELEASE
        
        
            org.springframework
            spring-context
            5.2.12.RELEASE
        
        
            org.springframework
            spring-core
            5.2.12.RELEASE
        
        
            org.springframework
            spring-expression
            5.2.12.RELEASE
        
springJdbc
            
        
            org.springframework
            spring-jdbc
            5.2.12.RELEASE
        
springTx
            
        
            org.springframework
            spring-tx
            5.2.12.RELEASE
        
springTest
            
        
            org.springframework
            spring-test
            5.2.12.RELEASE
        
mybatis
            
        
            org.mybatis
            mybatis
            3.5.5
        
整合中间件
        
        
        
            org.mybatis
            mybatis-spring
            1.3.2
        
数据库驱动
        
        
            mysql
            mysql-connector-java
            8.0.27
        
数据源
        
        
            org.apache.commons
            commons-dbcp2
            
                
                    commons-logging
                    commons-logging
                
            
            2.8.0
        
        
            org.apache.commons
            commons-pool2
            2.8.1
        

日志
        
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.9.1
        
辅助工具
        
            org.projectlombok
            lombok
            1.18.22
        
测试
        
            junit
            junit
            4.13.1
            test
        
mvn配置
    
    
        
            
                src/main/java
                
                    **
    public Customer findById(Integer id);
}

编写实现类:

继承SqlSessionDaoSupport抽象类,注入SqlSessionFactory 对象,并通过getSqlSession方法获取SqlSession

public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
    @Override
    public Customer findById(Integer id) {
        
        return this.getSqlSession().selectOne("findById",id);
    }
}

因为是在SqlSessionDaoSupport 类中SqlSessionFactory 对象是通过setter 方法注入的,所以可以在applicationContext.xml 中直接注入:

    
        
        
    
测试:

这里需要注意的是

  1. Junit 和Spring 也是需要整合的,不能简单的通过编写test直接运行,这样并不会加载Spring 的容器,而是仅运行了一个测试类

  2. 首先需要导入Spring-test dependency

    •             
              
                  org.springframework
                  spring-test
                  5.2.12.RELEASE
              
      
  3. 其次需要在测试类上写明启动环境以及加载的配置文件

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
    
  4. 完整代码:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
    @Slf4j
    public class CustomerDaoTest {
    
        @Autowired
        CustomerDao customerDao;
    
        @Test
        public void test1(){
            System.out.println(customerDao.findById(1));
        }
    }
    
2.2 Mapper接口方式开发整合

对于上面的通过SqlSession 进行开发会很繁琐,无形中增加了工作负担,所以Mybatis-spring 提供了根据Mapper 接口动态代理生成Mapper 对象的类——MapperFactoryBean,依赖于:

  • mapperInterface:用于指定接口
  • sqlSessionFactory:用于指定SqlSessionFactory 也可以指定SqlSessionTemplate ,如果同时指定只会启动SqlSessionFactory

这种方式的本质是通过程序员编写Mapper 接口,由框架通过定义创建接口的动态代理对象,这个代理对象的方法体等同于上面Dao接口的实现类方法,也就是获取SqlSession 并且通过sqlSession 根据id调用方法,如果按照以下规范去书写,可以在配置文件(mybatis-config.xml)中不引入映射文件(xxxMapper.xml),框架会直接扫描进行绑定:

  1. Mapper 接口和xml映射文件名称一致:CustomerMapper—CustomerMapper.xml

  2. mapper 和xml 必须需要在一个包内

  3. xml 中的namspce 和Mapper 接口的类路径一致:

    
    
  4. Mapper 接口中的方法名和xml 中的每个执行语句的id 一致

  5. Mapper 接口中的输入参数和xml中的parameterType 一致

  6. Mapper 接口中的输出参数和xml 中resultType 一致

四种Mapper 的配置方式:

  • 面向xml文件:
    • 需要将每个xml都进行注册
  • 面向接口:
    • 需要将每个接口进行注册
  • 面向包:
    • 需要将包的每个接口都注册,且接口名要与xml名一致
  • 不注册:
    • xml必须和接口在一个包下
定义接口:
public interface CustomerMapper {

    
    public Customer findById(Integer id);
}
定义xml:

xml 的namespace需要是对应接口的全限定路径:







    
        select * from t_customer where id = #{id}
    


注册/不注册:
    
    

        


    
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Slf4j
public class CustomerDaoTest {

    @Autowired
    CustomerMapper customerMapper;

    @Test
    public void test1(){
        System.out.println(customerMapper.findById(1));
    }
}
2.3 MapperScannerConfigurer

所有的mapper 接口不需要一个一个的在applicationContext中进行注册,myabtis-spring 团队提供了一种自动扫描的形式来配置,采用mybatisScannerConfigurer 类:

需要配置的属性:

  • basePackage:指定映射接口文件的包路径,当扫描多个包时可以使用分号或逗号作为分隔符,指定包路径后,扫描该报及其子包中的所有文件
  • annotationClass:指定要扫描的注解名称,只有被注解标识的类才会被配置为映射器
  • sqlSessionFactoryBeanName:指定在spring中定义的SqlSessionFactory的Bean名称
  • sqlSessionTemplateBeanName:指定在Spring 中定义的SqlSessionTemplate 的Bean名称,如果定义次属性,sqlSessionFactoryBeanName 将不起作用
    • narkerInterface:创建映射器的接口

    
        

    

这样所有的mapper接口都会注册到IOC容器中,但是映射xml文件还需要继续进行配置。

也可以通过@Mapper 注解到interface 上面,通过配置annotationClass来进行扫描

2.4 整合事务

在Mybatis 中事务是交由spring 进行管理的,而事务的处理主要是通过@Transactional 注解进行处理,而处理的位置则是service 层,及在service 层调用的方法中若爆出异常则会进行事务回滚,事务也就影响不到数据库。

开启事务注解

applicationContext.xml

    
开启spring注解扫描
    
service层

@Transactional 注解即可以注解到方法上,则本方法开启事务管理,也可以注解到类或接口上,则类和接口里的方法都开启事务管理

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685560.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号