Mybatis学习(六)————— Spring整合mybatis

一、Spring整合mybatis思路    

      非常简单,这里先回顾一下mybatis最基础的根基,

      mybatis,有两个配置文件

        全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映射文件等东西)

        映射文件xxxMapper.xml,用来对输入参数输出参数,数据库语句做配置的。

      mybatis配置好之后的使用步骤

        1、获取sqlMapConfig.xml的位置然后进行加载

        2、通过sqlMapConfig.xml中的内容创建出sqlsessionFactory对象

        3、然后通过sqlsessionFactory对象创建出sqlsession对象

        4、有了sqlsession对象就可以进行相应的操作了。

      集成思路

        有了上面的一些知识回顾,那么就有思路让spring继承mabatis了。

        1、让spring来管理数据源信息,sqlMapConfig.xml中就不需要加载数据源了。交给spring管理

        2、让spring通过单例方式管理SqlSessionFactory,只需要一个SqlSessionFactory帮我们生成sqlsession即可。也就是需要sqlsession对象就让sqlsessionfactory生成。所以是单例方式。

        3、让spring创建sqlsession bean。也就是通过SqlSessionFactory创建SqlSession,

        4、如果是使用mapper代理开发的方式,那么持久层的mapper都需要由spring进行管理,spring和mybatis整合生成mapper代理对象。

 

二、工程搭建

      2.1、创建java工程

            Mybatis学习(六)————— Spring整合mybatis_第1张图片      

      2.2、添加jar包

        Mybatis的核心和依赖包

        数据库驱动包

        spring的包

        junit包

        spring和mybatis整合包

        dbcp连接池

  

      2.3、

 

      2.4、添加SqlMapConfig.xml

            Mybatis学习(六)————— Spring整合mybatis_第2张图片





    
    
        
        
    

        

    

复制代码




    
    
        
        
    

        

    

复制代码

 

      2.5、映射配置文件Student.xml

              







     
     
复制代码






     
     
复制代码

 

      2.6、整合spring的配置文件applicationContext.xml

            Mybatis学习(六)————— Spring整合mybatis_第3张图片


    
    

    
    
        
        
        
        
        
        
    
    
    
    
        
        
        
        
    
    
    
复制代码

    
    

    
    
        
        
        
        
        
        
    
    
    
    
        
        
        
        
    
    
    

    
复制代码

  

      2.7、db.properties

                        

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=root
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=root

 

      2.8、总观,就编写了四个配置文件,和一个javabean

            Mybatis学习(六)————— Spring整合mybatis_第4张图片

 

三、开发原始dao

      上面是一个通用的配置,我们在使用mybatis时,有两个模式,一种就是原始dao的方式,一种就是使用mapper代理的方式,这里就介绍原始dao是如何集成spring的

        StudengDao:接口

            Mybatis学习(六)————— Spring整合mybatis_第5张图片

        StudentDaoImpl:实现类

            Mybatis学习(六)————— Spring整合mybatis_第6张图片

          使用原始dao开发的缺点就是只能通过selectOne或者selectList等操作,而不是直接调用映射配置文件中的方法,不能一目了然。

        spring中配置StudentDao

            Mybatis学习(六)————— Spring整合mybatis_第7张图片

      

        很多人这里会有疑问,觉得在spring中配置了StudengDao这个bean,但是我们根本没用在StudengDaoImpl中用set方法让其自动注入呀?原因就在StudengDaoImpl中继承了sqlSessionDaoSupport这个类,这个类帮我们做了,所以,我们直接通过this.getSqlSession()获取对象即可。

 

        测试:

            Mybatis学习(六)————— Spring整合mybatis_第8张图片

 

        

 

四、mapper方式开发

      编写mapper接口,StudentMapper.java

            Mybatis学习(六)————— Spring整合mybatis_第9张图片

      编写mapper映射文件 StudengMapper.xml,注意两个要放在一起。名称要相同

            Mybatis学习(六)————— Spring整合mybatis_第10张图片

      spring中生成mapper代理对象

             Mybatis学习(六)————— Spring整合mybatis_第11张图片 

    
    
    
    
        
        
        
        
    
    
    
复制代码
    
    
    
    
        
        
        
        
    
    
    
复制代码

 

      测试:

            Mybatis学习(六)————— Spring整合mybatis_第12张图片

              

五、总结

      就这样结束了,spring集成Mybatis,很简单,有什么对象都由spring来创建即可。注意原始dao和mapper这两种开发方式的不同。结果都市一样的,方式不同而已。这个在第一节讲Mybatis就已经讲解过了,这里不在陈述,下一章节讲解一下Mybatis的逆向工程

转载于:https://www.cnblogs.com/surfcater/p/10225795.html

你可能感兴趣的:(Mybatis学习(六)————— Spring整合mybatis)