mybatis-spring的集成

  源码下载(数据库使用derby,具体数据库结构参考这里

  在src下新建applicationContext.xml

  配置内容:数据源、SqlSessionFactory、Mapper、事务处理

一、配置SqlSessionFactory

  需要两个参数:数据源和Mybatis的配置文件路径

  这样SpringIOC容器就会初始化这个SqlSessionFactoryBean,解析Mybatis配置文件连同数据源一同保存在SpringBean里

    <!-- 配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
</bean> <!-- 配置SQLSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

  jdbc.properties

driver=org.apache.derby.jdbc.ClientDriver
url=jdbc:derby://localhost:1527/E:/my/derby/mydb

  在spring中已经初始化数据源,所以在mybatis配置文件中就无需再进行配置

  mybatis.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>
<settings>
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 每种属性按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>

  有些书中介绍了配置SqlSessionTemplate,当同时设置了SqlSessionFactory和SqlSessionTemplate,系统就会用SqlSessionTemplate覆盖掉SqlSessionFactory,相比较而言,笔者还是喜欢采用Mapper接口的编程方式。

二、配置mapper(这里采用自动扫描的形式)

  采用MapperScannerConfigurer配置,属性:

    basePackage:指定让Spring自动扫描的包

    annotationClass:表示如果类被这个注解标识的时候才进行扫描

    sqlSessionFactoryBeanName:指定在spring中定义sqlSessionFactory的bean名称。如果被定义,sqlSessionFactory将不起作用

    sqlSessionTemplateBeanName:指定在spring中定义sqlSessionTemplate的bean名称。如果被定义,sqlSessionFactoryBeanName将不起作用

    markerInterface:指定是实现了什么接口就认为它是mapper

 <!-- 接口方式 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
//在sqlSessionFactory中加载映射文件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 加载映射文件 -->
<property name="mapperLocations" value="classpath*:/com/example/mapper/*Mapper.xml"></property>
</bean>

三、配置事务

    <!--  配置jdbc事务管理器,完成数据的完整性和一致性 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用声明式事务管理方式 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

  配置service层

    <!-- 启动spring注解 -->
<context:annotation-config/>
<!-- 扫描注解所在的包 -->
<context:component-scan base-package="com.example"/>

  在service业务层中使用@Transaction注解便可开启事务,事务使用见上篇

附:别人写的SqlSessionFactoryBean

-------end-------

(五)mybatis-spring的集成的更多相关文章

  1. SSM框架开发web项目系列(五) Spring集成MyBatis

    前言 在前面的MyBatis部分内容中,我们已经可以独立的基于MyBatis构建一个数据库访问层应用,但是在实际的项目开发中,我们的程序不会这么简单,层次也更加复杂,除了这里说到的持久层,还有业务逻辑 ...

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

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...

  3. Spring同时集成JPA与Mybatis

    @ 目录 ORM Spring ORM Spring ORM 同时集成JPA与Mybatis 一.创建一个SpringBoot项目 二.建立用户信息登记表 三.Web应用项目集成mysql 四.添加S ...

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

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

  5. Spring Boot 集成 Mybatis

    原文:https://github.com/x113773/testall/issues/9 方式一:mybatis-spring-boot-starter---这种方式比较简单,具体步骤如下:1. ...

  6. SSM学习(二)mybatis和spring的集成

    上一篇文章大概搭建了一下ssm的框架,其实还是不完整,我们往项目中添加了spring和mybatis的配置文件,还差一个spring mvc的配置文件,在resource中在新建一个Applicati ...

  7. Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件

    上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...

  8. Spring Boot集成MyBatis的2种方式

    目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...

  9. Spring Boot集成MyBatis开发Web项目

    1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...

  10. Spring Boot 集成 MyBatis和 SQL Server实践

    概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...

随机推荐

  1. EL表达式获取属性值的原理

    EL表达式获取对象属性的原理是这样的:以表达式${user.name}为例EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找到与 ...

  2. fastDFS 文件系统搭建

    1.单机版 https://www.cnblogs.com/chiangchou/p/fastdfs.html#_label3_0 2.集群版

  3. Myeclipse 10/2014 配置插件(svn、maven、properties、velocity)的方法

    一.配置SVN详细图解 什么是SVN? 管理软件开发过程中的版本控制工具. 下面会以两种方式来介绍怎么安装svn,myeclipse安装SVN插件步骤,以myeclipse 2014为例,第一种是最常 ...

  4. python 3.6

    安装了最新版anaconda3-4.3 发现jupyter-notebook 少了一些东西.需要手工安装 https://github.com/Anaconda-Platform/nbpresent

  5. 吴裕雄--天生自然 PYTHON3开发学习:正则表达式

    import re print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配 print(re.match('com', 'www.runoo ...

  6. LeetCode No.157,158,159

    No.157 Read 用 Read4 读取 N 个字符 题目 给你一个文件,并且该文件只能通过给定的 read4 方法来读取,请实现一个方法使其能够读取 n 个字符. read4 方法: API r ...

  7. sqlserver 时间格式 取年月日时分

    select substring( convert(varchar,getdate(),120),1,16)

  8. windows下pip的安装

    安装地址:https://pypi.python.org/pypi/pip#downloads 下载完成后,找到文件并进行解压,找到下面路径. 打开cmd,cd到当前目录下,然后执行下面命令: pyt ...

  9. [LC] 543. Diameter of Binary Tree

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  10. 吴裕雄--天生自然python学习笔记:python 用 Open CV抓取摄像头视频图像

    Open CV 除了可以读取.显示静态图片外 , 还可 以加载及播放动态影片, 以 及 读取内置或外接摄像头的图像信息 . 很多笔记本电脑都具有摄像头 , OpenCV 可通过 VideoC aptu ...