Mybatis(六) Spring整合mybatis

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

                --WZYmysql

1、Spring整合mybatis思路    spring

      很是简单,这里先回顾一下mybatis最基础的根基,sql

      mybatis,有两个配置文件数据库

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

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

      mybatis配置好以后的使用步骤session

        一、获取sqlMapConfig.xml的位置而后进行加载mybatis

        二、经过sqlMapConfig.xml中的内容建立出sqlsessionFactory对象mvc

        三、而后经过sqlsessionFactory对象建立出sqlsession对象

        四、有了sqlsession对象就能够进行相应的操做了。

      集成思路

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

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

        二、让spring经过单例方式管理SqlSessionFactory,只须要一个SqlSessionFactory帮咱们生成sqlsession便可。也就是须要sqlsession对象就让sqlsessionfactory生成。因此是单例方式。

        三、让spring建立sqlsession bean。也就是经过SqlSessionFactory建立SqlSession,

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

 

2、工程搭建

      2.一、建立java工程

                  

      2.二、添加jar包

        Mybatis的核心和依赖包

        数据库驱动包

        spring的包

        junit包

        spring和mybatis整合包

        dbcp链接池

        mybatis和spring整合所须要的jar包

  

      2.三、

 

      2.四、添加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>

    <!-- 取别名,或者别的其余全局配置信息,就在这里编写 -->
    <typeAliases>
        <!-- 别名为类名首字母大小写均可以 -->
        <package name="com.wuhao.ms.domain"/>
    </typeAliases>

    <!-- 加载mapper映射文件 -->    
<mappers>
    <mapper resource="classpath:Student.xml"/>
</mappers>
</configuration>
sqlMapConfig.xml

 

      2.五、映射配置文件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">

     <select id="findStudentById" parameterType="int" resultType="com.wuhao.ms.domain.Student">
         SELECT * FROM student WHERE sid = #{id}
     </select>
     
</mapper>
Student.xml

 

      2.六、整合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" />

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>
    
    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis的全局配置文件的路径 -->
        <property name="configLocation" value="SqlMapConfig.xml"></property>
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    
</beans>
    
applicationContext.xml

  

      2.七、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.八、总观,就编写了四个配置文件,和一个javabean

            

 

3、开发原始dao

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

        StudengDao:接口

            

        StudentDaoImpl:实现类

            

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

        spring中配置StudentDao

            

      

        不少人这里会有疑问,以为在spring中配置了StudengDao这个bean,可是咱们根本没用在StudengDaoImpl中用set方法让其自动注入呀?缘由就在StudengDaoImpl中继承了sqlSessionDaoSupport这个类,这个类帮咱们作了,因此,咱们直接经过this.getSqlSession()获取对象便可。

 

        测试:

            

 

        

 

4、mapper方式开发

      编写mapper接口,StudentMapper.java

            

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

            

      spring中生成mapper代理对象

              

    <!-- spring建立mapper代理两种方式 -->
    <!-- 第一种,单个配置,一个mapper就一个配置 -->
    
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 指定要映射的mapper接口的全限定名 -->
        <property name="mapperInterface" value="com.wuhao.ms.mapper.StudentMapper"></property>
        <!-- 指定sqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    
    <!-- 批量配置,这里是批量配置mapper代理,那么下面就不用配置id了。
        咱们想要获取哪一个mapper代理用这个格式:类名首字母小写
     -->
<!--     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wuhao.ms.mapper"></property>
    </bean> -->
View Code

 

      测试:

            

              

5、总结

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

相关文章
相关标签/搜索