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

Spring教程03-Spring和MyBatis整合

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

Spring教程03-Spring和MyBatis整合

Spring-MyBatis整合 4.1 整合概述
  • 将 MyBatis 与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring 来管理。所以,只需要将 SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。

  • 使用技术:IOC

4.2 复习MyBatis步骤

mybatis使用步骤:

  1. 创建dao接口 StudentDao
  2. 定义mapper文件 StudentDao.xml
  3. 定义mybatis的主配置文件 mybatis.xml
  4. 创建dao的代理对象 StudentDao dao = SqlSession.getMapper(StudentDao.class)
  5. 测试 List students = dao.selectStudents()
4.3 MyBatis和Spring整合分析

要使用dao对象 需要使用getMapper()方法,如何使用getMapper()方法,需要哪些条件:

  1. 获取SqlSession对象,需要使用SqlSessionFactory的openSession()方法

  2. 创建SqlSessionFactory对象,通过读取mybatis的主配置文件,能创建SqlSessionFactory对象

​ 需要SqlSessionFactory对象,使用Factory能获取SqlSession, 有了SqlSession就能有dao,目的就是获取dao对象

​ Factory需要读取主配置文件

  1. 主配置文件包含:

    • 数据库信息
    
      
        
        
          
          
          
          
        
      
    
    • mapper文件位置
    
      
    
    
  2. 使用独立的连接池对象替换mybatis默认自带的,把连接池类也交给spring创建

通过以上的说明,我们需要让spring创建一下对象

  1. 独立的连接池对象,使用阿里云druid连接池
  2. SqlSessionFactory对象
  3. 创建dao对象

需要学习的就是以上三个对象的创建方法,使用xml的bean标签。

4.4 整合步骤

步骤:

  1. MySQL创建数据库,新建表
  2. 新建maven项目,加入依赖
    1. spring依赖
    2. mybatis依赖
    3. mysql驱动
    4. spring事务的依赖
    5. mybatis和spring集成的依赖:mybatis官方提供的,用来在spring创建mybatis的SqlSessionFactory,dao对象的
  3. 创建实体类
  4. 创建dao接口和mapper文件
  5. 创建mybatis主配置文件
  6. 创建Service的接口和实现类,属性是dao
  7. 创建spring的配置文件:声明mybatis的对象交给spring创建
    1. 数据源 DataSource
    2. SqlSessionFactory
    3. Dao对象
    4. 声明自定义的service
  8. 创建测试类,获取Service对象,通过service调用dao完成数据库的访问
4.4.1 MySQL新建表

4.4.2 新建maven项目,加入依赖
  • 添加spring以外的依赖

    • Spring事务依赖

    • jdbc

    • mybatis

    • mybatis和spring集成的依赖:mybatis官方提供的,用来在spring创建mybatis的SqlSessionFactory,dao对象

    • 数据库驱动

    • druid连接池


  org.springframework
  spring-tx
  5.3.20


  org.springframework
  spring-jdbc
  5.3.20



  org.mybatis
  mybatis
  3.5.10



  org.mybatis
  mybatis-spring
  2.0.7


  mysql
  mysql-connector-java
  8.0.29



  com.alibaba
  druid
  1.2.11




  
     src/main/java
      
        ***.xml  false
    
  

4.4.3 创建实体类Student
@Data
@ToString
public class Student {
    private Integer id;
    private String name;
    private String email;
    private Integer age;

    public Student() {
    }

    public Student(Integer id, String name, String email, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }
}
4.4.4 创建dao接口和mapper文件

​ 创建StudentDao接口:

public interface StudentDao {
    int insertStudent(Student student);
    List selectStudents();
}

​ 创建StudentDao对应的mapper文件:





    
        insert into student values (#{id}, #{name}, #{email}, #{age})
    

    

4.4.5 创建mybatis主配置文件

​ 不需要在配置文件中定义DataSource等,只需将mapper文件配置好即可,mybatis.xml





    
        
    

    
        
        
    

4.4.6 创建Service的接口和实现类,属性是dao

调用service --> 调用dao --> 实现持久化

​ StudentService接口:

public interface StudentService {
    int addStudent(Student student);
    List queryStudent();
}

​ 接口实现类,StudentServiceImpl:

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;
  
    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }

    @Override
    public int addStudent(Student student) {
        int nums = studentDao.insertStudent(student);
        return nums;
    }

    @Override
    public List queryStudent() {
        List students = studentDao.selectStudents();
        return  students;
    }
}

4.4.7 创建spring的配置文件

创建spring的配置文件:声明mybatis的对象交给spring创建

  1. 数据源 DataSource,作用是连接数据库的
  2. SqlSessionFactoryBean,SqlSessionFactory获取
    • dataSource属性,set注入数据库连接池赋值给datasource
    • configLocation属性,mybatis主配置文件的位置 ,configLocation是Resource类型,用来读取配置文件,它的赋值使用value指定文件的路径,使用classpath表示文件的位置
  3. MapperScannerConfigurer, 创建dao对象,使用SqlSession的getMapper,
    • sqlSessionFactoryBeanName属性,指定sqlSessionFactory的id
    • basePackage属性,指定包名,包名是dao接口所在的包名,MapperScannerConfigurer会扫描这个包中的所有接口,把每个接口都执行一次getMapper方法,得到每个接口的dao对象 ,创建好的dao对象放入到spring的容器中,名字是:接口名的首字母小写
  4. 声明自定义的service

​ applicatiaonContext.xml:



		
    	
    

    
    
        
        
        
        
    

    
    
        
        
    

    
    
         
        
    

    
    
        
    

4.4.8 创建测试类

​ 创建测试类,获取Service对象,通过service调用dao完成数据库的访问。MyTest代码:

public class MyTest {

    @Test
    public void test01(){
        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        String[] names = ctx.getBeanDefinitionNames();
        for(String na:names){
            System.out.println("容器中对象名称:" + na + "|" + ctx.getBean(na));
        }
    }

    @Test
    public void testServiceInsert(){
        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        StudentService service = (StudentService) ctx.getBean("studentService");
        Student student = new Student();
        student.setId(2003);
        student.setName("testService");
        student.setEmail("testService");
        student.setAge(25);
        //spring和mybatis整合在一起时,事务是自动提交的 不必手动提交事务SqlSession.commit()
        int nums = service.addStudent(student);
        System.out.println(nums);
    }

    @Test
    public void testServiceSelect(){
        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        StudentService service = (StudentService) ctx.getBean("studentService");
        List students = service.queryStudent();
        students.forEach(student -> System.out.println(student));
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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