Mybatis(六) Spring整合mybatis

      心莫浮躁 ~ 踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些 ~ so,努力啥的都是强迫自己做自以为努力正确的事情,想干嘛就干嘛把!    

                --WZY

一、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 工程

                  

      2.2、添加 jar 包

        Mybatis 的核心和依赖包

        数据库驱动包

        spring 的包

        junit 包

        spring 和 mybatis 整合包

        dbcp 连接池

        mybatis 和 spring 整合所需要的 jar 包

  

      2.3、

 

      2.4、添加 SqlMapConfig.xml

            
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
&lt;!-- 取别名,或者别的其他全局配置信息,就在这里编写 --&gt;
&lt;typeAliases&gt;
    &lt;!-- 别名为类名首字母大小写都可以 --&gt;
    &lt;<span style="color: rgba(0, 0, 255, 1)">package</span> name="com.wuhao.ms.domain"/&gt;
&lt;/typeAliases&gt;

&lt;!-- 加载mapper映射文件 --&gt;    

<mappers>
<mapper resource="classpath:Student.xml"/>
</mappers>
</configuration>

sqlMapConfig.xml

 

      2.5、映射配置文件 Student.xml

              
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper    
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间,对 sql 进行一个分类管理 -->
<!-- 注意:namespace 在 mapper 代理时,具有重要且特殊的作用 
    对应 mapper 接口的全限定名
-->
<mapper namespace="test">
 &lt;select id="findStudentById" parameterType="int" resultType="com.wuhao.ms.domain.Student"&gt;<span style="color: rgba(0, 0, 0, 1)">
     SELECT </span>* FROM student WHERE sid =<span style="color: rgba(0, 0, 0, 1)"> #{id}
 </span>&lt;/select&gt;

</mapper>

Student.xml

 

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

            
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!-- 引用 java 配置文件 -->
    <context:property-placeholder location="db.properties" />
&lt;!-- 配置数据源 --&gt;
&lt;bean id="dataSource" <span style="color: rgba(0, 0, 255, 1)">class</span>="org.apache.commons.dbcp.BasicDataSource"&gt;
    &lt;property name="driverClassName" value="${db.driver}" /&gt;
    &lt;property name="url" value="${db.url}" /&gt;
    &lt;property name="username" value="${db.username}" /&gt;
    &lt;property name="password" value="${db.password}" /&gt;
    &lt;property name="maxActive" value="10" /&gt;
    &lt;property name="maxIdle" value="5" /&gt;
&lt;/bean&gt;

&lt;!-- 配置SqlSessionFactory --&gt;
&lt;bean id="sqlSessionFactory" <span style="color: rgba(0, 0, 255, 1)">class</span>="org.mybatis.spring.SqlSessionFactoryBean"&gt;
    &lt;!-- 指定mybatis的全局配置文件的路径 --&gt;
    &lt;property name="configLocation" value="SqlMapConfig.xml"&gt;&lt;/property&gt;
    &lt;!-- 数据源 --&gt;
    &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

applicationContext.xml

  

      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.properties

 

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

            

 

三、开发原始 dao

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

        StudengDao:接口

            

        StudentDaoImpl: 实现类

            

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

        spring 中配置 StudentDao

            

      

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

 

        测试:

            

 

        

 

四、mapper 方式开发

      编写 mapper 接口,StudentMapper.java

            

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

            

      spring 中生成 mapper 代理对象

              
    <!-- spring 创建 mapper 代理两种方式 -->
    <!-- 第一种,单个配置,一个 mapper 就一个配置 -->
&lt;bean id="studentMapper" <span style="color: rgba(0, 0, 255, 1)">class</span>="org.mybatis.spring.mapper.MapperFactoryBean"&gt;
    &lt;!-- 指定要映射的mapper接口的全限定名 --&gt;
    &lt;property name="mapperInterface" value="com.wuhao.ms.mapper.StudentMapper"&gt;&lt;/property&gt;
    &lt;!-- 指定sqlSessionFactory --&gt;
    &lt;property name="sqlSessionFactory" ref="sqlSessionFactory"&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;!--<span style="color: rgba(0, 0, 0, 1)"> 批量配置,这里是批量配置mapper代理,那么下面就不用配置id了。
    我们想要获取哪个mapper代理用这个格式:类名首字母小写
 </span>--&gt;

<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wuhao.ms.mapper"></property>
</bean> -->

View Code

 

      测试:

            

              

五、总结

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