/*
* Copyright 2010-2012 The MyBatis Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.junit.After;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.mockrunner.mock.ejb.MockUserTransaction;
import com.mockrunner.mock.jdbc.MockConnection;
import com.mockrunner.mock.jdbc.MockDataSource;
import com.mockrunner.mock.jdbc.MockPreparedStatement;
/**
* @version $Id$
*/
public final class MyBatisSpringTest extends AbstractMyBatisSpringTest {
private SqlSession session;
@After
public void validateSessionClose() {
// assume if the Executor is closed, the Session is too
if ((session != null) && !executorInterceptor.isExecutorClosed()) {
session = null;
fail("SqlSession is not closed");
} else {
session = null;
}
}
// ensure MyBatis API still works with SpringManagedTransaction
@Test
public void testMyBatisAPI() {
session = sqlSessionFactory.openSession();
session.getMapper(TestMapper.class).findTest();
session.close();
assertNoCommit();
assertSingleConnection();
assertExecuteCount(1);
}
@Test
public void testMyBatisAPIWithCommit() {
session = sqlSessionFactory.openSession();
session.getMapper(TestMapper.class).findTest();
session.commit(true);
session.close();
assertCommit();
assertSingleConnection();
assertExecuteCount(1);
}
@Test
public void testMyBatisAPIWithRollback() {
session = sqlSessionFactory.openSession();
session.getMapper(TestMapper.class).findTest();
session.rollback(true);
session.close();
assertRollback();
assertSingleConnection();
assertExecuteCount(1);
}
// basic tests using SqlSessionUtils instead of using the MyBatis API directly
@Test
public void testSpringAPI() {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertNoCommit();
assertSingleConnection();
assertExecuteCount(1);
}
@Test
public void testSpringAPIWithCommit() {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
session.commit(true);
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertCommit();
assertSingleConnection();
}
@Test
public void testSpringAPIWithRollback() {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
session.rollback(true);
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
assertRollback();
assertSingleConnection();
}
@Test
public void testSpringAPIWithMyBatisClose() {
// This is a programming error and could lead to connection leak if there is a transaction
// in progress. But, the API allows it, so make sure it at least works without a tx.
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
session.close();
assertNoCommit();
assertSingleConnection();
}
// Spring API should work with a MyBatis TransactionFactories
@Test
public void testWithNonSpringTransactionFactory() {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
try {
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.getMapper(TestMapper.class).findTest();
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
// users need to manually call commit, rollback and close, just like with normal MyBatis
// API usage
assertNoCommit();
assertSingleConnection();
} finally {
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
// Spring TX, non-Spring TransactionFactory, Spring managed DataSource
// this should not work since the DS will be out of sync with MyBatis
@Test(expected = TransientDataAccessResourceException.class)
public void testNonSpringTxFactoryWithTx() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
TransactionStatus status = null;
try {
status = txManager.getTransaction(new DefaultTransactionDefinition());
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
fail("should not be able to get an SqlSession using non-Spring tx manager when there is an active Spring tx");
} finally {
// rollback required to close connection
txManager.rollback(status);
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
// Spring TX, non-Spring TransactionFactory, MyBatis managed DataSource
// this should work since the DS is managed MyBatis
@Test
public void testNonSpringTxFactoryNonSpringDSWithTx() throws java.sql.SQLException {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
MockDataSource mockDataSource = new MockDataSource();
mockDataSource.setupConnection(createMockConnection());
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), mockDataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
TransactionStatus status = null;
try {
status = txManager.getTransaction(new DefaultTransactionDefinition());
session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
session.commit();
session.close();
txManager.commit(status);
// txManager still uses original connection
assertCommit();
assertSingleConnection();
// SqlSession uses its own connection
// that connection will not have commited since no SQL was executed by the session
MockConnection mockConnection = (MockConnection) mockDataSource.getConnection();
assertEquals("should call commit on Connection", 0, mockConnection.getNumberCommits());
assertEquals("should not call rollback on Connection", 0, mockConnection.getNumberRollbacks());
assertCommitSession();
} finally {
SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
@Test(expected = TransientDataAccessResourceException.class)
public void testChangeExecutorTypeInTx() throw
《Spring与MyBatis整合详解及1.2.0版本资源包下载指南》 在Java Web开发领域,Spring框架和MyBatis持久层框架的整合是常见且实用的技术栈。Spring以其强大的依赖注入和管理能力,搭配MyBatis的灵活数据库操作,能够提供高效、便捷的开发体验。本文将详细介绍Spring与MyBatis的整合过程,并指导如何下载并使用名为"spring-mybatis-spring-1.2.0.zip"的资源包。 一、Spring与MyBatis的整合原理 1. MyBatis-Spring插件:Spring与MyBatis的桥梁 MyBatis-Spring是一个连接Spring和MyBatis的轻量级库,它使得两个框架可以无缝集成。它提供了SqlSessionFactoryBean和SqlSessionTemplate等类,帮助我们配置MyBatis并管理SqlSession。 2. 数据源配置:Spring管理数据库连接 在Spring中,我们需要配置数据源(DataSource),它是数据库连接的源头。通过Spring的DataSourceTransactionManager,我们可以实现事务管理。 3. MyBatis配置:Spring注入Mapper接口 在Spring配置文件中,我们可以使用MapperScannerConfigurer来扫描并自动注册所有的Mapper接口,这样MyBatis的SqlSession就会自动代理这些接口。 4. 事务管理:Spring统一事务控制 Spring的声明式事务管理允许我们在不编写任何事务管理代码的情况下,通过配置控制事务的边界。 二、Spring与MyBatis整合步骤 1. 引入依赖:在项目pom.xml中添加MyBatis-Spring和MyBatis的依赖。 2. 配置数据源:在Spring配置文件中配置数据源,如Druid、HikariCP等。 3. 配置SqlSessionFactory:使用SqlSessionFactoryBean创建SqlSessionFactory实例。 4. 配置MapperScannerConfigurer:扫描Mapper接口并注册到Spring容器。 5. 编写Mapper接口和XML映射文件:定义SQL操作,关联Mapper接口。 6. 配置事务管理器:使用PlatformTransactionManager,通常为DataSourceTransactionManager。 7. 使用@Autowired注入Mapper接口:在需要操作数据库的类中,Spring会自动注入对应的Mapper接口。 三、"spring-mybatis-spring-1.2.0.zip"资源包详解 该压缩包包含的是Spring与MyBatis整合的1.2.0版本资源,适用于Windows各个版本。内容可能包括: 1. Spring与MyBatis的整合示例代码:展示了如何配置和使用整合后的框架。 2. Maven或Gradle配置文件:包含了项目构建所需的依赖关系。 3. 示例数据库脚本:用于创建测试数据库表。 4. README文件:详细说明了资源包的使用方法和注意事项。 下载该资源包后,开发者可以根据README中的指导,快速搭建一个基于Spring和MyBatis的开发环境,进行实践学习或项目开发。 总结,Spring与MyBatis的整合提供了强大的数据访问能力,而"spring-mybatis-spring-1.2.0.zip"资源包则为开发者提供了快速入门的平台。通过深入理解整合原理和实践操作,可以有效提升开发效率,更好地服务于实际项目开发。



























































































































- 1
- 2

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 4949
- 资源: 2745
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于深度学习算法的MATLAB人脸马赛克处理技术,MATLAB实现的自动人脸马赛克算法研究与应用,基于MATLAB的人脸马赛克 ,基于MATLAB的人脸马赛克;人面遮罩处理;图像处理技术;遮盖人脸;马
- 基于JavaScript及多语言技术的药知道项目实训设计源码
- 基于Matlab的FFT滤波技术实现波形谐波分析与自定义频段清除,无相位滞后且幅值可补偿,非实时滤波的缺陷与补偿(附图解说),基于Matlab的FFT滤波技术实现波形谐波分析与自定义频段清除,无相位滞
- 激励型负荷需求响应模型matlab+yalmip:时序性负荷转移的实现与图解读,基于Matlab和Yalmip编程语言的激励型负荷需求响应模型研究:时序负荷转移与目标函数解析,激励型负荷需求响应模型m
- 三相并联型有源电力滤波器APF仿真研究:PI控制下的电压外环与电流内环及ip-iq谐波检测与SVPWM调制方法应用,三相并联型有源电力滤波器APF仿真研究:PI控制下的电压外环与电流内环及ip-iq谐
- 西门子Smart200追剪程序及对应维纶屏监控功能,PLC无级调速算法实战例程与个性化定制指南,西门子Smart200追剪程序与维纶屏监控联动:无级调速算法实例及PLC应用,西门子Smart200 追
- 基于Cesium.js的地理数据可视化开源设计源码
- 基于粒子群算法的微电网优化调度研究:含分布式电源、负荷与可再生能源数据之MATLAB实现,基于粒子群算法的微电网优化调度:分布式电源与负荷数据研究,基于粒子群算法的微电网优化调度 含有的分布式电源:微
- 基于HTML5、CSS3和JavaScript的旅游攻略网站设计源码
- 基于价格型和替代型需求响应的综合能源系统低碳优化运行模型研究,基于价格弹性矩阵与替代型需求响应的综合能源系统低碳优化运行模型研究,Matlab代码 替代型 价格型需求响应 综合能源 优化运行
- 自动化视觉设备升级:修复BUG的机器学习框架源码,C#与Halcon混合编程,插件式开发带手眼标定与相机模式切换支持,自动化视觉设备:修复BUG的机器视觉框架源码,C#联合Halcon混合编程,插件式
- TPC编译码实现详解:硬判决与软判决译码源码及文档解析(matlab版),基于MATLAB的TPC编译码实现:源码与文档解析硬判决译码与软判决译码全过程,TPC编译码实现 matlab实现 源码加文
- 基于电热联合调度的区域并网型微电网运行优化模型研究:实现协调调度与成本降低的仿真分析,基于电热联合调度的区域并网型微电网运行优化模型研究:实现协调调度与成本降低的仿真分析,综合能源系统,提出基于电热联
- 基于MATLAB GUI的多功能数字滤波器设计工具:选择IIR和FIR类型,定制频谱,多种窗函数选择,基于MATLAB GUI的多功能数字滤波器设计工具:选择IIR与FIR,实时频谱显示,多种窗函数选
- 基于Vue和Java的流浪猫狗救助领养系统设计与实现源码
- 电机转子断条与匝间短路故障诊断:仿真数据获取与特征提取算法应用,电机故障诊断技术:转子断条与匝间短路的仿真分析与特征提取算法应用,电机 发电机 转子断条 匝间短路 故障诊断 首先在SIMULINK里搭


