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

Spring整合MyBatis

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

Spring整合MyBatis

说明 :Spring整合MyBatis部分:参考网上课程,自己写的总结,如有侵权,联系删除。

目录

Spring整合MyBatis_搭建环境

Spring整合MyBatis_编写配置文件

 Spring整合MyBatis_准备数据库和实体类

Spring整合MyBatis_编写持久层接口和service类

Spring整合MyBatis_Spring整合Junit进行单元测试 

 Spring整合MyBatis_自动创建代理对象


Spring整合MyBatis_搭建环境

我们知道使用MyBatis时需要写大量创建 SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession等对象的代码,而Spring的作用是帮助我们创建和管理对象,所以我们可以使用Spring整合MyBatis,简化MyBatis开发。

创建maven项目,引入依赖。


    
    
        org.mybatis
        mybatis
        3.5.7
    
    
    
        mysql
        mysql-connector-java
        8.0.26
    
    
    
        com.alibaba
        druid
        1.2.5
    
    
    
        org.springframework
        spring-context
        5.3.13
    
    
        org.springframework
        spring-tx
        5.3.13
    
    
       org.springframework
        spring-jdbc
        5.3.13
    
    
    
        org.mybatis
        mybatis-spring
        2.0.6
    


Spring整合MyBatis_编写配置文件
  1. 编写数据库配置文件db.properties
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///student
    jdbc.username=root
    jdbc.password=123456
  2. 创建MyBatis配置文件SqlMapConfig.xml,数据源、扫描接口都 交由Spring管理,不需要在MyBatis配置文件中设置。
    
    
    
    
  3. 创建Spring配置文件applicationContext.xml
    
    
    
        
        
        
        
        
        
            
            
            
            
        
        
        
            
        
        
        
            
        
    

 Spring整合MyBatis_准备数据库和实体类

准备数据库

CREATE DATABASE `student`;
USE `student`;
DROp TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(10) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert  into `student`(`id`,`name`,`sex`,`address`) values (1001,'dds','男','北京'),(2,
'xxs','女','天津');

准备实体类

public class Student {
    private int id;
    private String name;
    private String sex;
    private String address;
    // 省略构造方法/getter/setter/tostring
}

Spring整合MyBatis_编写持久层接口和service类

编写持久层接口

@Repository
public interface StudentDao {
    // 查询所有学生
    @Select("select * from student")
    List findAll();
    // 添加学生
    @Insert("insert into student values(null,#{name},#{sex},#{address})")
    void add(Student student);
}

编写service类

@Service
public class StudentService {
    // SqlSession对象
    @Autowired
    private SqlSessionTemplate sqlSession;
    // 使用SqlSession获取代理对象
    public List findAllStudent(){
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        return studentDao.findAll();
   }
}

Spring整合MyBatis_Spring整合Junit进行单元测试 

之前进行单元测试时都需要手动创建Spring容器,能否在测试时让 Spring自动创建容器呢?

  1. 引入Junit和Spring整合Junit依赖
    
    
        junit
        junit
        4.12
        test
    
    
    
        org.springframework
        spring-test
        5.3.13
    
    
  2. 编写测试类
    // JUnit使用Spring方式运行代码,即自动创建spring容器。
    @RunWith(SpringJUnit4ClassRunner.class)
    // 告知创建spring容器时读取哪个配置类或配置文件
    // 配置类写法:
    @ContextConfiguration(classes=配置类.class)
    @ContextConfiguration(locations="classpath:applicationContext.xml")
    public class StudentServiceTest {
        @Autowired
        private StudentService studentService;
        @Test
        public void testFindAll(){
            List allStudent = studentService.findAllStudent();
            allStudent.forEach(System.out::println);
       }
    }
    

    注:使用SqlSessionTemplate创建代理对象还是需要注册接口 或者映射文件的。

    1.在MyBatis配置文件注册接口

    
        
            
        
    
    

    2.创建sqlSessionFactory时指定MyBatis配置文件

    
    
        
        
    
    

 Spring整合MyBatis_自动创建代理对象

Spring提供了MapperScannerConfigurer对象,该对象可以自动扫 描包创建代理对象,并将代理对象放入容器中,此时不需要使用 SqlSession手动创建代理对象。

  1. 创建MapperScannerConfigurer对象
    
    
        
        
    
  2. Service类直接使用代理对象即可
    @Service
    public class StudentService {
        // 直接注入代理对象
        @Autowired
        private StudentDao studentDao;  
        // 直接使用代理对象
        public void addStudent(Student student){
            studentDao.add(student);
       }
    }
    

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

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

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