从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&amp;characterEncoding=UTF-8&amp;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平滑过渡的更多相关文章

  1. OpenWrt(LEDE)2020.4.29更新 UPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成+软件包

    交流群:QQ 1030484865 电报:  t_homelede   固件说明 基于Lede OpenWrt R2020.4.8版本(源码截止2020.4.29)Lienol Feed及若干自行维护 ...

  2. OpenWrt(LEDE)2020.4.12编译 UnPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成

    固件说明 基于Lede OpenWrt R2020.4.8版本(源码截止2020.4.12)Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及软件 ...

  3. HomeLede 2020.5.27更新 UPnP+NAS+多拨+网盘+DNS优化+帕斯沃/Clash 无缝集成+软件包

    交流群:QQ 1030484865 电报 t.me/t_homelede   固件说明 基于Lede OpenWrt R2020.5.20版本(源码截止2020.5.27)及若干自行维护的软件包 结合 ...

  4. Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试。

    Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...

  5. 易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试

    Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...

  6. Zeplin(for Windows)无缝集成到了 Adobe XD

    Zeplin(for Windows)无缝集成到了 Adobe XD 大约6个月前,推出了 Zeplin 的新Adobe XD CC集成.从那时起,数十万个设计从Adobe XD导出到Zeplin.Z ...

  7. K2 BPM_【解决方案】K2+SAP:端到端无缝集成,为企业全面赋能提速_十年专注业务流程管理系统

    企业数字化转型离不开信息技术的支撑,大部分企业的各项业务都会有专业的系统,比如ERP.BI.CRM等.但这些系统往往由于无法融合,造成信息孤岛.数据断层等问题,这阻碍了企业推动数字化转型的进程.如何实 ...

  8. 【x64软路由】OpenWrt(LEDE) 20200329编译 反追踪 抗污染 加速 PSW 无缝集成 UPnP NAS

    固件说明 基于Lede OpenWrt R2020.3.19版本(源码更新截止20200329)Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及 ...

  9. OpenWrt R2020.3.19 反追踪 抗污染 加速 PSW 无缝集成 UnPnP NAS

    固件说明 基于Lede OpenWrt R2020.3.19版本Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及软件进行测试,通过后发布 设计目 ...

随机推荐

  1. python---补充django中文报错(1),Django2.7使用sys.setdefaultencoding('utf-8'),以及使用reload(sys)原因

    SyntaxError at /blog/ news/story Non-ASCII character , but no encoding declared; see http://python.o ...

  2. Zephir入门教程一

    一.如何安装 zephir-安装和初体验:http://blog.csdn.net/u011142688/article/details/51619811 二.如何使用 需要切到工作目录下,也就是co ...

  3. Spring RedisTemplate操作-发布订阅操作(8)

    @Component("sub") public class Sub implements MessageListener{ @Autowired private StringRe ...

  4. 20155303 2016-2017-2 《Java程序设计》第六周学习总结

    20155303 2016-2017-2 <Java程序设计>第六周学习总结 课堂笔记 高效学习法推荐 看视频学习(2h)→ 以代码为中心看课本,思考运行结果并验证(3h)→ 课后作业验证 ...

  5. [转] 解决RegexKitLite编译报错

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/3984254.html ,转载请注明出处. 在编译RegexKitLite的时候,报错如下: Undefined ...

  6. HDU 4472 Count DP题

    解题报告:题目大意,给你n个球,要将这n个球从下到上按层次排列,要求同一个层次的的每一个分支的数量都必须相等,问有多少种排列的方法. 此题的一个DP题,假设现在有n个球,要将这n个球排列好,我们就必须 ...

  7. 第9月第15天 设计模式 adapter mvc

    1. 有一道iOS面试题,iOS中都有什么设计模式?很少有答案说包括adapter. gof 书中adapter模式有以下内容: 实现: ... b ) 使 用 代 理 对 象 在这种方法中, T r ...

  8. 第7月第25天 xcode bundle calayer动画

    1. http://www.jianshu.com/p/3755c35ac87f 2.calayer http://www.jianshu.com/p/aadfce72bf74

  9. lucene查询索引之Query子类查询——(七)

    0.文档名字:(根据名字索引查询文档)

  10. php的递归函数示例

    递归函数太难理解了,写了一个示例放在这里方便没事的时候看一下. <?php /** *php递归函数示例 *(从1到100的累加和计算) * */ function summation($num ...