MyBatis3与Spring3无缝集成-从iBatis平滑过渡
从2010开始接触iBatis到现在,一直到现在把iBatis作为数据访问层ORM。为了演示一个Web应用,今天又搭了个SpringMVC应用,由于应用比较简单,Spring版本直接用最新版本3.2.4.RELEASE,结果惊讶的发现,Spring已经不推荐使用iBatis了,SqlMapClientDaoSupport类已经加上了@deprecated as of Spring 3.2, in favor of the native Spring support in the Mybatis follow-up project (http://code.google.com/p/mybatis/)注解。于是乎,简单的学习了下MyBatis3,在这里把学习笔记记录下来,希望能对和我一样,在从iBatis转入到MyBatis的网友一些参考。
1、增加Maven依赖
由于要整合Spring和MyBatis,所以至少需要spring/springmvc和mybatis-spring的Maven依赖,和Spring一样,直接使用最新版本:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
2、增加MyBatis的配置文件
和iBatis的SqlMapConfig配置一样,MyBatis也有一个总配置文件,本应用中为mybatis-config.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>
<typeAlias alias="UserDO" type="com.github.obullxl.jeesite.dal.dto.UserDO" />
</typeAliases> <mappers>
<mapper resource="mybatis/mappers/atom-user-mapper.xml" />
</mappers>
</configuration>
在该文件中,增加一个类型别名,类型别名的目的是避免多次输入过长的类路径;同时有一个映射文件,它和iBatis的sqlmapping文件一样,里面是动态SQL内容;熟悉iBatis的朋友会发现,其实MyBatis和iBatis的思路其实差不多的。
3、增加映射文件
从中配置文件和结合iBatis的SqlMap文件可以得出,SQL也是需要映射文件,本应用为atom-user-mapper.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">
<mapper namespace="ATOM-USER">
<resultMap id="UserMap" type="UserDO">
<id property="id" column="id" />
<result property="uname" column="uname" />
<result property="passwd" column="passwd" />
<result property="uemail" column="uemail" />
<result property="gmt_create" column="gmtCreate" />
<result property="gmt_modify" column="gmtModify" />
</resultMap> <select id="findAll" resultMap="UserMap" fetchSize="1">
SELECT * FROM atom_user
</select> <select id="count" resultType="int">
SELECT COUNT(*) FROM atom_user;
</select>
</mapper>
大家发现,和sqlmapping文件非常的相似,其中ResultMap的class用type代替,由于我们的中配置文件中,已经定义了UserDO的别名,所以这里直接用UserDO就行了,无需包含包名称。
4、增加Spring配置文件
在Spring配置文件中,定义MyBatis相关的Bean,本应用为mybatis-context.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" default-autowire="byName"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/osnode?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" />
<property name="username" value="osnode" />
<property name="password" value="site" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="true" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean> <bean id="abstractDAO" abstract="true">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="userDAO" parent="abstractDAO" class="com.github.obullxl.jeesite.dal.mybatis.MyBatisUserDAO" /> <bean id="userService" class="com.github.obullxl.jeesite.service.impl.UserServiceImpl" />
</beans>
5、DAO类
接口定义如下:
public interface UserDAO {
public int count();
public List<UserDO> findAll();
}
默认实现如下:
public class MyBatisUserDAO extends SqlSessionDaoSupport implements UserDAO {
/**
* @see com.github.obullxl.jeesite.dal.dao.UserDAO#count()
*/
public int count() {
return this.getSqlSession().selectOne("ATOM-USER.count");
}
/**
* @see com.github.obullxl.jeesite.dal.dao.UserDAO#findAll()
*/
public List<UserDO> findAll() {
return this.getSqlSession().selectList("ATOM-USER.findAll");
}
}
6、测试应用
到目前为止,我们的MyBatis和Spring就整合完成了,写一个简单的Java类看看:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/mybatis-context.xml");
UserService service = context.getBean(UserService.class);
int count = service.count();
logger.warn("Count value is:{}", count);
List<UserDO> users = service.findAll();
logger.warn("All users:\n{}", users);
}
哈哈,可以看到我们所希望的结果,有了基本的框架,接下来就是补充应用其他内容了……
7、源代码
本博客中,代码只涉及到部分重点代码,其他代码我就不在贴出来了,请移步到GitHub:https://github.com/obullxl/atom-jeesite
8、关于本博客
博客园地址:http://www.cnblogs.com/obullxl/p/mybatis-integration-with-spring.html
百度引擎地址:http://obullxl.duapp.com/topic-blog-17.html
红帽引擎地址:https://obullxl-osnode.rhcloud.com/topic-blog-4.html
MyBatis3与Spring3无缝集成-从iBatis平滑过渡的更多相关文章
- OpenWrt(LEDE)2020.4.29更新 UPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成+软件包
交流群:QQ 1030484865 电报: t_homelede 固件说明 基于Lede OpenWrt R2020.4.8版本(源码截止2020.4.29)Lienol Feed及若干自行维护 ...
- OpenWrt(LEDE)2020.4.12编译 UnPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成
固件说明 基于Lede OpenWrt R2020.4.8版本(源码截止2020.4.12)Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及软件 ...
- HomeLede 2020.5.27更新 UPnP+NAS+多拨+网盘+DNS优化+帕斯沃/Clash 无缝集成+软件包
交流群:QQ 1030484865 电报 t.me/t_homelede 固件说明 基于Lede OpenWrt R2020.5.20版本(源码截止2020.5.27)及若干自行维护的软件包 结合 ...
- Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试。
Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...
- 易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试
Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...
- Zeplin(for Windows)无缝集成到了 Adobe XD
Zeplin(for Windows)无缝集成到了 Adobe XD 大约6个月前,推出了 Zeplin 的新Adobe XD CC集成.从那时起,数十万个设计从Adobe XD导出到Zeplin.Z ...
- K2 BPM_【解决方案】K2+SAP:端到端无缝集成,为企业全面赋能提速_十年专注业务流程管理系统
企业数字化转型离不开信息技术的支撑,大部分企业的各项业务都会有专业的系统,比如ERP.BI.CRM等.但这些系统往往由于无法融合,造成信息孤岛.数据断层等问题,这阻碍了企业推动数字化转型的进程.如何实 ...
- 【x64软路由】OpenWrt(LEDE) 20200329编译 反追踪 抗污染 加速 PSW 无缝集成 UPnP NAS
固件说明 基于Lede OpenWrt R2020.3.19版本(源码更新截止20200329)Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及 ...
- OpenWrt R2020.3.19 反追踪 抗污染 加速 PSW 无缝集成 UnPnP NAS
固件说明 基于Lede OpenWrt R2020.3.19版本Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及软件进行测试,通过后发布 设计目 ...
随机推荐
- bzoj千题计划264:bzoj3884: 上帝与集合的正确用法
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...
- [转载]AngularJS 指令 用法
http://book.2cto.com/201312/37782.html http://www.itnose.net/detail/6144038.html http://www.cnblogs. ...
- 第11月第8天 ffmpeg ffplay
static int ffplay_video_thread(void *arg) { FFPlayer *ffp = arg; VideoState *is = ffp->is; AVFram ...
- 【转载】maven pom详解(2)
setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...
- Visual Studio 2013更新内容简介
前言 VS2013终于发布了,虽然之前自己使用VS2010和VS2012的时间也不长,尤其是VS2012这自己刚刚也没用多久,看到VS2013发布了,自己忍不住也下载了下来,官网肯定可以下载,不过自己 ...
- 如何计算 App 的启动时间
应用启动场景 事实上 Android 中一个 App 的启动时间可以准确计算的.但是要分场景.也就是说要分开游戏和应用. 大家都知道,在Android中,游戏开发和应用开发是两码事.所以我们需要分开来 ...
- jquery easyui:tab自动加载第一个tab内容
$('#resourceTabs').tabs('unselect', 0);$('#resourceTabs').tabs('select', 0);
- String,StringBuffer和StringBuilder的区别
(1)String类的API概述是这样的:String类代表字符串,Java程序中的所有字符串字面值都作为此类的实例体现.字符串是常量,它们的值在创建之后不能更改.可见,String是对象且为不可变对 ...
- C# Message类的属性Msg所关联的消息ID
C# Message类的属性Msg所关联的消息ID https://msdn.microsoft.com/en-us/library/windows/desktop/ms645606(v=vs.8 ...
- mysql innodb 行级锁升级
创建数据表test,表定义如下所示: CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NO ...