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千题计划180:bzoj4411: [Usaco2016 Feb]Load balancing
http://www.lydsy.com/JudgeOnline/problem.php?id=4411 用树状数组维护扫描线 一个树状数组维护扫描线之上的y<=i点,另一个维护扫描线之下y&l ...
- css中实现ul两端的li对齐外面边缘
其实就是设置ul的宽度大一些就好
- html5 canvas 多个填充渐变形状
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hadoop - hdfs 基础操作
hdfs --help # 所有参数 hdfs dfs -help # 运行文件系统命令在Hadoop文件系统 hdfs dfs -ls /logs # 查看 hdfs dfs -ls /user/ ...
- 【ARTS】01_05_左耳听风-20181210~1216
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- 【Python】Flask系列-模板笔记
Jinja2模板 Jinja2模板传参 如何渲染模板: 模板放在templates文件夹下 从flask中导入render_template函数. 在视图函数中,使用render_template函数 ...
- python模块分析之hashlib加密(二)
前言 hashlib模块是py3.+用来对字符串进行hash加密的模块,核心算法是md5,明文与密文是一一对应不变的关系:用于注册.登录时用户名.密码等加密使用. 模块分析 hashlib模块有多种加 ...
- 解决getJSON跨域登录Session丢失的问题
最近在做项目中发现,我用下面的代码异步请求到login.ashx: var memberUrl = rooturl + 'member.ashx?r=' + Math.random() + '& ...
- RHEL7 -- 使用Chrony设置时间与时钟服务器同步
Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确. 它由两个程序组成:chronyd和chronyc. chronyd是一个后台运行的守护进程,用于调整内核 ...
- javascript-词法分析解析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...