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软路由场景需要定制 按照家庭应用场景对固件及软件进行测试,通过后发布 设计目 ...
随机推荐
- centos6.x下安装maven
转自:http://www.centoscn.com/image-text/install/2014/0507/2923.html 1.下载maven包首先从官网上 http://maven.apac ...
- js拾遗: replace 替换参数
先来看一个简单的例子吧. var str = "123X321".replace("X", "$'"); 替换后的 str 是什么呢?是 & ...
- keepalived vrrp_script脚本不执行解决办法
首先打开日志观察: tail -f /var/log/messages 然后新开一个客户端重启keepalived , systemctl restart keepalived.service 看日志 ...
- RabbitMQ Queue一些常见模式
懒队列:lazy Queue,即用到的时候才会加载,3.6.0及之后新添加的.当新添加数据后,不会将其放入到内存中,而是将其放入到磁盘中. 普通队列:1).in-memory,数据直接放入到内存中. ...
- 大数据系列之分布式计算批处理引擎MapReduce实践-排序
清明刚过,该来学习点新的知识点了. 上次说到关于MapReduce对于文本中词频的统计使用WordCount.如果还有同学不熟悉的可以参考博文大数据系列之分布式计算批处理引擎MapReduce实践. ...
- 一个无锁消息队列引发的血案(三)——地:q3.h 与 RingBuffer
目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...
- react-router 4 路由的嵌套
1.在component组件内部需要嵌套的位置直接嵌套Route标签 这个方法会使得路由标签比较分散,子组件我们必须直接将Route标签写入到父组件之中,而且路由必须包含根路径. // Dashboa ...
- Kali Linux没有声音的解决方法
Kali Linux系统默认状态下,root用户是无法使用声卡的,也就没有声音.启用的方法如下: (1)在终端执行命令:systemctl --user enable pulseaud ...
- oracle创建表空间并赋予权限
CREATE TEMPORARY TABLESPACE 表空间 TEMPFILE 数据存储路径('D://oracle//NEW_NAMESPACE.DBF') SIZE 32M AUTOEXTEN ...
- java & android 开发规范手册
阿里巴巴Java开发手册(终极版)https://pan.baidu.com/s/1c1UQM7Q 阿里巴巴Java开发规约插件p3cGitHub:https://github.com/alibaba ...