整合原理

  • MyBatis操作数据库,对数据库进行CRUD(增、删、改、查)操作时,实际原理是通过SqlSessionFactory对象---->产生SqlSession---->利用SqlSession产生的对象生成Mapper对象---->实现对数据库的CRUD操作。
  • 当利用Spring来整合MyBatis时,实际原理是将SqlSessionFactory对象交由Spring管理,从而实现两个框架的整合,达到操作数据库的目的。
  • 当使用spring整合mybatis时,数据库配置文件不再放入mybatis的conf.xml中,数据库的所有配置信息应放在spring的applicationContext.xml配置文件中。

需要使用的JAR包

  • mybatis-spring.jar-----spring整合mybatis使用
  • spring-tx.jar----spring对事务的支持
  • spring-jdbc.jar----spring访问数据库使用
  • spring-expression.jar-----spring表达式支持
  • spring-context-support.jar----spring应用上下文支持
  • spring-core.jar----spring核心代码支持
  • spring-context-----spring应用上线文
  • spring-beans.jar-----spring组件支持
  • spring-aop.jar-----spring对切面支持
  • spring-web.jar----spring对web项目支持
  • commons-logging.jar----日志支持
  • commons-dbcp.jar----数据源支持
  • ojdbc.jar----Oracle数据库驱动
  • mybatis.jar----mybatis核心支持
  • log4j.jar----日志支持
  • commons-pool.jar----数据库连接池

数据库相关配置信息

  • applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 数据库配置信息,详细配置文件引入 --><bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="locations"><array><value>classpath:db.properties</value></array>     </property></bean><!-- 配置数据库信息(调用db.properties配置文件中的配置信息) -->  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${driver}"></property><property name="url" value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property><property name="maxIdle" value="${maxIdle}"></property><property name="maxActive" value="${maxActive}"></property></bean><bean id="studentMapper" class="org.dsl.dao.impl.StudentDaoImpl"><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><bean id="studentService" class="org.dsl.service.impl.StudentServiceImpl"><property name="studentMapper" ref="studentMapper"></property></bean><!-- 在springIOC容器中创建Mybatis的核心类sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据库信息 --><property name="dataSource" ref="dataSource"></property> <!-- 读取mybatis配置文件信息 --><property name="configLocation" value="classpath:conf.xml"></property>  </bean>
</beans>
  • 其中dataSource中的信息可以直接配置到applicationContext.xml中,但是大多数采用配置文件db.properties的形式引入,方便维护管理。
  • db.properties配置信息:
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORACLEDSL
username=scott
password=tiger
maxIdle=1000
maxActive=500
  • 其中配置dataSource时,需要使用commons-dbcp.jar中的org.apache.commons.dbcp.BasicDataSource类,这个类中包括了数据库driverClassName、URL、username、password、maxIdle和maxActive等等属性,根据具体需要进行配置即可。
  • 在加载配置文件db.properties时,需要使用org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer类所在父类PropertyPlaceholderConfigurer–父类PlaceholderConfigurerSupport–父类PropertyResourceConfigurer–父类PropertiesLoaderSupport中的private Resource[] locations属性,该属性是数组类型,可以配置多个。
  • 在springIOC容器中创建Mybatis的核心类sqlSessionFactory时需要用到mybatis-spring.jar中org.mybatis.spring.SqlSessionFactoryBean类,可以利用这个类中dataSource属性读取数据库配置信息,利用configLocation属性配置读取mybatis配置文件信息。

spring产生动态mapper对象的三种方法

  • 如上在springIOC容器中创建Mybatis的核心类sqlSessionFactory时,实现了spring利用mybatis中sqlSessionFactory对象操作数据库的权限,但是spring实现操作数据库的增删改查,必须要利用相应的数据库操作语句(sql语句),这时就需要spring产生mybatis最终操作需要的动态mapper对象,例如:StudentMapper对象,实际为StudentDao的接口类。
  • 第一种,通过DAO实现层继承SqlSessionDaoSupport类
    层级结构如下:

    通过继承mybatis-spring.jar中的org.mybatis.spring.support.SqlSessionDaoSupport类,通过该类中提供的sqlSession对象来产生动态mapper对象。
package org.dsl.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.dsl.entity.Student;
import org.dsl.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {@Overridepublic void addStudent(Student student) {// TODO Auto-generated method stubSqlSession session = super.getSqlSession();//获取动态mapper对象StudentMapper stuDao = (StudentMapper) session.getMapper(StudentMapper.class);stuDao.addStudent(student);}}

注:DAO的接口三层时的类名可以叫StudentDao,但是为了mapper接口和映射文件对应,所以建议将IStudentDao.java接口的名字改为StudentMapper与接口映射文件同名,接口映射文件的首字母小写。并且建议放在同一个类路径下,为了减少不必要配置。位置和文件如下:

1.Mapper 接口方法名和 Mapper.xml 中定义的每个 statement 的 id 同名。
2.Mapper 接口方法的输入参数类型和 rMapper.xml 中定义的 statement 的parameterType 类型相同。
3.Mapper 接口的返回类型和Mapper.xml 中定义的 statement 的 resultType 类型相同。

<?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为接口全限定类名 -->
<mapper namespace = "org.dsl.mapper.StudentMapper">
<!-- id值为DAO中的方法名 --><insert id="addStudent" parameterType="org.dsl.entity.Student">insert into student(stuNo,stuName,stuAge) values(#{stuNo},#{stuName},#{stuAge})   </insert>
</mapper>

加mybatis配置文件,改配置文件中放置载映射文件:

<?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><mappers><!-- 类位置用点(.)路径使用斜杠(/) --><!-- 加载studentMapper.xml映射文件 --><mapper resource="org/dsl/mapper/studentMapper.xml"/></mappers>
</configuration>

最后将Dao层与sqlSessionFactory对象建立联系,将已经注入的sqlSessionFactory对象以属性的方式配置给Dao层。

配置完成调用测试

package org.dsl.test;import org.dsl.entity.Student;
import org.dsl.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");StudentService studentService = (StudentService)context.getBean("studentService");Student student = new Student();student.setStuNo(2);student.setStuName("zds");student.setStuAge(20);studentService.addStudent(student);}}

可省略提醒:src路径下面的mybatis配置文件conf.xml在spring整合mybatis时,是可以不用写的,因为在配置SqlSessionFactory时org.mybatis.spring.SqlSessionFactoryBean类中,有获取mapper映射文件的属性,mapperLocations该属性是数组,可以配置多个,可以使用通配符进行全包检索即可。当没有conf.xml文件时,就不能配置configLocation属性查找conf.xml文件,否则会报错。

   <!-- 配置sqlSessionFactory对象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据库信息 --><property name="dataSource" ref="dataSource"></property> <!-- 查找mapper映射文件 该属性为数组类型--><property name="mapperLocations" value="org/dsl/mapper/*.xml"></property>  </bean>

主要步骤:在springIOC容器中配置数据库信息DataSource—>创建SqlSessionFactory对象(读取数据库信息、加载mybatis配置文件)—>将SqlSessionFactory对象与Dao层(StudentMapper)关联。

  • 第二种,利用mybatis-spring.jar中提供的org.mybatis.spring.mapper.MapperFactoryBean类实现动态mapper获取
    在第一种方法基础上删除StudentDaoImpl类和springIOC容器中的StudentDaoImpl的相关配置,利用mybatis-spring.jar中提供的org.mybatis.spring.mapper.MapperFactoryBean类中提供的属性指定mapper接口文件StudentMapper(即StudentDao的接口类)。

    第二种方式缺点:如果存在多个类似于StudentMapper需要使用时,就要定义配置多个bean,代码较为冗余。

  • 第三种,利用mybatis-spring.jar中提供的Mapper扫描器(org.mybatis.spring.mapper.MapperScannerConfigurer),扫描指定的包路径下的所有mapper.xml配置文件。其中配置sqlSessionFactory对象的属性与前两种不同,需要使用sqlSessionFactoryBeanName属性才可以,并且需要指定要扫描的包,在利用相应的mapper属性时,在中ref需要配置mapper.java的文件名称首字母小写。

  • 三种配置方法applicationContext.xml文件配置,需要时保留一种方式,否则会报错

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 数据库配置信息,详细配置文件引入 --><bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="locations"><array><value>classpath:db.properties</value></array>     </property></bean><!-- 配置数据库信息(调用db.properties配置文件中的配置信息) -->  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${driver}"></property><property name="url" value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property><property name="maxIdle" value="${maxIdle}"></property><property name="maxActive" value="${maxActive}"></property></bean><!-- 第一种配置 --><bean id="studentMapper" class="org.dsl.dao.impl.StudentDaoImpl"><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><!-- 第二种配置 --><bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="org.dsl.mapper.StudentMapper"></property><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><!-- 第三种配置 --><!-- 批量产生多个mapper对象(批量产生多个mapper)产生的mapper在springIOC中使用时的ID值就是其接口文件的命名(接口名=id值) --><bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.dsl.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><bean id="studentService" class="org.dsl.service.impl.StudentServiceImpl"><property name="studentMapper" ref="studentMapper"></property></bean><!-- 配置sqlSessionFactory对象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据库信息 --><property name="dataSource" ref="dataSource"></property> <!-- 查找mapper映射文件 该属性为数组类型--><property name="mapperLocations" value="org/dsl/mapper/*.xml"></property>  </bean>
</beans>
  • 整体流程

Spring整合MyBatis总结相关推荐

  1. spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试

    引子:spring整合mybatis.因为,我们看完(我就是这样的)spring和mybatis之后,本想自己写一个小小的项目,以便加深理解,但是我发现在spring中整合mybatis并不是一件容易 ...

  2. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  3. SSM之二(Spring整合Mybatis)

    项目与外界交互大概过程如下图: 一般过程是: 前端发送请求,查询数据.增加数据.修改数据.删除数据 中间件经过处理后,对数据发送请求 数据库返回数据,中间件再对数据处理 中间件响应前端请求 上一节关注 ...

  4. Spring整合Mybatis之注解方式,(注解整合Junit)

    Spring整合Mybatis之注解方式 我有一篇博客详细写了我自己使用xml的方法Spring整合MyBatis,现在我就把核心配置文件中的每个bean的配置使用注解的方式实现 注解整合MyBati ...

  5. spring整合mybatis基于注解

    数据库 /* Navicat MySQL Data Transfer Source Server         : mysql Source Server Version : 50549 Sourc ...

  6. spring整合mybatis基于xml配置

    数据库 /* Navicat MySQL Data Transfer Source Server         : mysql Source Server Version : 50549 Sourc ...

  7. spring 整合 mybatis 中数据源的几种配置方式

    因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...

  8. Spring整合mybatis中的sqlSession是如何做到线程隔离的?

    转载自  Spring整合mybatis中的sqlSession是如何做到线程隔离的? 项目中常常使用mybatis配合spring进行数据库操作,但是我们知道,数据的操作是要求做到线程安全的,而且按 ...

  9. Spring整合Mybatis和JUnit

    Spring整合Mybatis: 注解整合MyBatis分析 业务类使用注解形式声明bean,属性采用注解注入 建立独立的配置管理类,分类管理外部资源,根据功能进行分类,并提供对应的方法获取bean ...

  10. Spring 整合 Mybatis

    数据库环境 // 创建mybatis数据库 create database mybatis;use mybatis // 创建teacher表 create table teacher(id int ...

最新文章

  1. PTA基础编程题目集-7-1 厘米换算英尺英寸
  2. mysql5.5以上开启慢查询
  3. 第01章 初识Mysql
  4. 北斗导航 | 复杂环境下卫星导航算法(理论)
  5. 逻辑漏洞——业务逻辑问题
  6. C#正则表达式编程(四)转致周公
  7. C/C++ OpenCV之Scharr边缘检测
  8. Android 启动过程介绍【转】
  9. 8255控制交通灯c语言源码,单片机程序 8255控制交通灯程序
  10. 整点小浪漫~Python27行代码绘制一幅满天星
  11. thinkphp5.1 安装think-queue 2.0.4
  12. ET游戏框架之环境搭建与运行
  13. Unity3D流体插件FluidSim使用总结
  14. 昆仑通态和S7 1200在线模拟仿真通讯
  15. 计算机3c认证 标准,计算机3C认证怎么办理,检测标准是什么?
  16. 抖音 触摸精灵_触控精灵app下载-触控精灵手机版 v1.3.2 - 安下载
  17. Debian各个版本下载地址
  18. 游戏感:虚拟感觉的游戏设计师指南——第六章 输入的测量方法
  19. photoshop android 切图插件,摹客PS插件-摹客PS插件(PS切图插件)下载 v2.1.3官方版--pc6下载站...
  20. java 实现跳表(skiplist)及论文解读

热门文章

  1. linux bsp格式,bsp是什么格式文件?bsp文件如何打开?
  2. 基于gis三维可视化的智慧城市行业运用
  3. 2022中国眼博会,山东视力防控展会,近视镜,中国近视矫正展
  4. java梅森素数_C语言实现求梅森素数代码解析
  5. F1C200S挖坑日记(1)——前言
  6. 口腔内科学试题及答案
  7. Error:Can‘t find Python executable “/path/to/executable/python2.7“, you can set the PYTHON env varia
  8. IDF实验室·牛刀小试1-5
  9. 模块化编程设计原则:高内聚,低耦合
  10. 智能仓储:探索如何使用机器人技术来提高仓库效率和准确性