一、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连接池

  

      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> <!-- 取别名,或者别的其他全局配置信息,就在这里编写 -->
<typeAliases>
<!-- 别名为类名首字母大小写都可以 -->
<package name="com.wuhao.ms.domain"/>
</typeAliases> <!-- 加载mapper映射文件 -->
<mappers>
<mapper resource="classpath:Student.xml"/>
</mappers>
</configuration>
<?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>

      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"> <select id="findStudentById" parameterType="int" resultType="com.wuhao.ms.domain.Student">
SELECT * FROM student WHERE sid = #{id}
</select> </mapper>
<?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>

      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" /> <!-- 配置数据源 -->
<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>
<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>

  

      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

            

三、开发原始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就一个配置 --> <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> -->
    <!-- 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> -->

      测试:

            

              

五、总结

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

Mybatis学习(六)————— Spring整合mybatis的更多相关文章

  1. Mybatis(六) Spring整合mybatis

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

  2. spring学习 六 spring与mybatis整合

    在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...

  3. Java学习笔记-spring整合mybatis

    这个项目就是一个例子,只有添加图书的功能: 项目架构: resource: 整合流程: 1.pom文件节点,这两个是整合用的,其他节点不再赘述: <!-- https://mvnreposito ...

  4. mybatis学习笔记 spring与mybatis整合

    转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...

  5. Spring boot 学习六 spring 继承 mybatis (基于注解)

    MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建立动态语言和让MyBatis执 ...

  6. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  7. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

  8. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  9. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二

    接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...

随机推荐

  1. 彻底卸载Windows Service

    前言,我使用Quartz.net + quartz.config + quartz_jobs.xml 写了个Windows Service,使用如下bat脚本执行服务的安装,启动,暂停,卸载 @ech ...

  2. vue调用 Highcharts 实现多个数据可视化展示

    一创建一个 options.js 代码为: export const option1 = { bar: { title: { text: '珠海猪场' // 指定图表标题 }, credits: { ...

  3. OpenCV常用头文件介绍

    转载:https://www.cnblogs.com/wangguchao/p/7244483.html 1.OpenCV包含的模块 cv – 核心函数库 cvaux – 辅助函数库 cxcore – ...

  4. node.js 递归复制文件夹(附带文件过滤功能)

    1.简介: 很简单,写了一个node操作文件的小脚本,主要实现对目标文件夹中内容的复制.还顺带一个按照文件夹或者文件名过滤的功能. 2.应用场景 适合基于 node 环境的项目,项目打包的时候,配合 ...

  5. 样式布局与 BFC

    一.几类视图 内联视图:inline 单行 块级视图:block 换行,有高度 行内块级视图:inline-block 单行,有高度 二.几类布局 块级布局 换行,通过设置 margin 水平居中 & ...

  6. 小甲鱼Python3笔记

    000-愉快的开始 入门容易,学习难,代码量少. 跨平台: Windows, Mac OS, Linux, UNIX. 应用领域: 操作系统 WEB 3D动画 企业应用 云计算等等. 001-我和Py ...

  7. SimpleRpc-客户端与服务端工作模型探讨

    前言 本篇文章讲述客户端与服务端的具体设计细节.有细心的小伙伴发现,客户端和服务端的工作方式不一样:服务端是多线程计算模型,利用工作线程完成数据的读取,而客户端是单线程(利用Reactor线程完成数据 ...

  8. 在ASP.NET Core中使用brotli压缩

    Brotli是一种全新的数据格式,可以提供比Zopfli高20-26%的压缩比.据谷歌研究,Brotli压缩速度同zlib的Deflate实现大致相同,而在Canterbury语料库上的压缩密度比LZ ...

  9. 超有料丨小白如何成功逆袭为年薪30万的Web安全工程师

    今天的文章是一篇超实用的学习指南,尤其是对于即将毕业的学生,新入职场的菜鸟,对Web安全感兴趣的小白,真的非常nice,希望大家能够好好阅读,真的可以让你少走很多弯路,至少年薪30万so easy! ...

  10. [Swift]LeetCode64. 最小路径和 | Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...