Mybatis整合Spring的原理以及整合一个案例

Mybatis整合Spring的原理以及整合一个案例,第1张

Mybatis整合Spring的原理以及整合一个案例

目录

一.Mybatis整合Spring的好处

注册会话工厂(SqlSessionFactory),不用手动创建

自动创建线程安全的会话,自动获取mapper接口的代理类并放入spring容器

二.依赖、配置与实现

1.加入相关依赖

2.编写配置文件

3.代码流程

总结:


一.Mybatis整合Spring的好处

Mybatis整合Spring给我们带来的好处

Spring可以帮我们管理对象简化方法的调用

Mybatis利用Spring的扩展点,把自己与Spring进行了整合

注册会话工厂(SqlSessionFactory),不用手动创建


        

        
    

原理:

org.mybatis.spring.SqlSessionFactoryBean这个类实现了FactoryBean、InitializingBean、ApplicationListener

熟悉Spring流程的应该知道FactoryBean是Spring注册bean的一种特殊的方式,他的getObject方法可以自定义的创建对象,他会解析我们的Mybitis的配置文件,最终把SqlSessionFactory注册到Spring的容器

自动创建线程安全的会话,自动获取mapper接口的代理类并放入spring容器

DefaultSqlSession是线程不安全的,所以我们并不可以直接用DefaultSqlSession。
SqlSessionTemplete 是他的替代品,他采用jdk动态代理 每次创建一个新的DefSqlSession
 


    
    
    

原理:

org.mybatis.spring.mapper.MapperScannerConfigurer这个类实现了BeanDefinitionRegistryPostProcessor接口,他也是注册bean的一种方式,实现他的方法可以直接

注册beanDifinition.他会扫描我们注册的mapper接口,把扫描到的接口name注册成beanDefinition的beanName,setBeanClass时,他传入了 MapperFactoryBean对象,MapperFactoryBean是又实现了FactoryBean的类,他的getObject方法就是获取SqlSession,调用getMapper方法,所以Mybatis会帮他动态代理生成mapper接口的代理对象。

所以就不用我们手动的创建SqlSession,也不用手动调用getMapper.

他会先获取SqlSessionTemplete

二.依赖、配置与实现 1.加入相关依赖

    
      junit
      junit
      4.11
      test
    

    
      org.springframework
      spring-context
      5.2.8.RELEASE
    


    
      org.mybatis
      mybatis
      3.5.1
    

    
      org.mybatis
      mybatis-spring
      2.0.6
    

    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    


    
      org.springframework
      spring-jdbc
      5.1.5.RELEASE
    

    
    
      com.alibaba
      druid
      1.1.22
    

    
    
      org.projectlombok
      lombok
      1.18.16
    

    
    
      mysql
      mysql-connector-java
      8.0.11
    

  
2.编写配置文件

spring主配置文件




    
    
    
    
        
        
        
        

    

    
    

        

        
    

    
    
        
        
        
    

    




Mybatis配置文件





    
        
        

        

        
        

        
        
        
        
        
        

    

    
        
    


    





    
        
        
    



3.代码流程

实体类

@Data
public class Student implements Serializable {
    private Integer id;

    private String name;

    private Integer age;

    private Integer clazzNo;

    public Student(Integer id, String name, Integer age, Integer clazzNo) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.clazzNo = clazzNo;
    }

}

mapper接口

public interface StudentMapper {
    
//    List queryAllStudents(RowBounds rowBounds);
    List queryAllStudents();

    
    Student querySyudentById(Integer id);

    
    int addStudent(Student student);

    
    int updateStudentById(Student student);

    
    int deletStudentById(Integer id);

//    
//    StudentAndClazz queryStudentAndeClazzById(Integer id);

}

mapper.xml





    

    
        select id, name, age, clazz_no from student where id=#{id}
    

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

    
        update  student set name=#{name}, age=#{age}, clazz_no=#{clazzNo} where id=#{id}
    

    
        delete from student where id = #{id}
    

    

















Service

@Service
public class StudentService {
    @Autowired
    public StudentMapper studentMapper;

    public List queryAllStudents() {
        return studentMapper.queryAllStudents();
    }
}

Test

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = applicationContext.getBean(StudentService.class);
        List studentList = studentService.queryAllStudents();
        System.out.println(Arrays.toString(studentList.toArray())
                .replaceAll("\[|\]","")
                .replaceAll("\)\,","\)n"));
//        System.out.println(applicationContext.getBeanDefinitionNames());
    }
}

总结:

         Mybatis整合Spring实际上就是利用了Spring的扩展,所以我们要清楚Spring留给我们的钩子方法。一定要知道Spring注册bean的那几种方式。点击学习注册bean的方式

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5712323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存