(五)mybatis-spring的集成
mybatis-spring的集成
在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注解便可开启事务,事务使用见上篇
-------end-------
(五)mybatis-spring的集成的更多相关文章
- SSM框架开发web项目系列(五) Spring集成MyBatis
前言 在前面的MyBatis部分内容中,我们已经可以独立的基于MyBatis构建一个数据库访问层应用,但是在实际的项目开发中,我们的程序不会这么简单,层次也更加复杂,除了这里说到的持久层,还有业务逻辑 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一
MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...
- Spring同时集成JPA与Mybatis
@ 目录 ORM Spring ORM Spring ORM 同时集成JPA与Mybatis 一.创建一个SpringBoot项目 二.建立用户信息登记表 三.Web应用项目集成mysql 四.添加S ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- Spring Boot 集成 Mybatis
原文:https://github.com/x113773/testall/issues/9 方式一:mybatis-spring-boot-starter---这种方式比较简单,具体步骤如下:1. ...
- SSM学习(二)mybatis和spring的集成
上一篇文章大概搭建了一下ssm的框架,其实还是不完整,我们往项目中添加了spring和mybatis的配置文件,还差一个spring mvc的配置文件,在resource中在新建一个Applicati ...
- Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件
上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...
- Spring Boot集成MyBatis的2种方式
目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- Spring Boot 集成 MyBatis和 SQL Server实践
概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...
随机推荐
- 2018.11.2JavaScript随笔
构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型
- textarea高度自适应解决方法
引入autosize.js <script src="./autosize.js"></script> autosize(document.getEleme ...
- PAT Basic 插⼊与归并(25) [two pointers]
题目 根据维基百科的定义: 插⼊排序是迭代算法,逐⼀获得输⼊数据,逐步产⽣有序的输出序列.每步迭代中,算法从输⼊序列中取出⼀元素,将之插⼊有序序列中正确的位置.如此迭代直到全部元素有序.归并排序进⾏如 ...
- ssh登录脚本
#!/usr/bin/expect set timeout 100 set passwd "your password" spawn shell expect "key& ...
- linux 查看链接库的版本
我们编译可执行文件的时候,会链接各种依赖库, 但是怎么知道依赖库的版本正确呢? 下面有几种办法: ldd 这是比较差的,因为打印结果更与位置相关 dpkg -l | grep libprotobuf ...
- ES6 find()
Array.prototype.find() 返回数组中满足提供测试函数的第一个元素的值,否则返回undefined let b = blogs.find(function(e) => { re ...
- sol - 0x60,61,62
[例题]走廊泼水节 设当前扫描到边x,y,长度为z,x所处的并查集为Sx,y所处的并查集为Sy: 对于任意u属于Sx,v属于Sy,我们可以知道u,v之间必连一条边 但是我们要在保证x,y之间的边属于唯 ...
- Python——Pandas 时间序列数据处理
介绍 Pandas 是非常著名的开源数据处理库,我们可以通过它完成对数据集进行快速读取.转换.过滤.分析等一系列操作.同样,Pandas 已经被证明为是非常强大的用于处理时间序列数据的工具.本节将介绍 ...
- 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- c++ 装饰器模式/包装模式
理解 使用两个隔离又继承自统一接口类的对象:方法对象(抽象/具体), 包装器对象(抽象/具体)实现多种组合只需要 n + m种实现, 而对比直接继承,则需要n*m 种实现,因此在面对多种具体类和多种额 ...