我们公司的项目使用spring+mybatis组合。所以就必须得使用mybatis-spring了。所以此处就昨日mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题,做了一个总结。

我们可以先来看看mybatis-spring框架的1.1.1版本中关于SqlSessionDaoSupport的代码吧:

package org.mybatis.spring.support;

import static org.springframework.util.Assert.*;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport; /**
* Convenient super class for MyBatis SqlSession data access objects.
* It gives you access to the template which can then be used to execute SQL methods.
* <p>
* This class needs a SqlSessionTemplate or a SqlSessionFactory.
* If both are set the SqlSessionFactory will be ignored.
*
* @see #setSqlSessionFactory
* @see #setSqlSessionTemplate
* @see SqlSessionTemplate
* @version $Id: SqlSessionDaoSupport.java 4885 2012-03-12 09:58:54Z simone.tripodi $
*/
public abstract class SqlSessionDaoSupport extends DaoSupport { private SqlSession sqlSession; private boolean externalSqlSession; @Autowired(required = false)
public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (!this.externalSqlSession) {
this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
}
} @Autowired(required = false)
public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSession = sqlSessionTemplate;
this.externalSqlSession = true;
} /**
* Users should use this method to get a SqlSession to call its statement methods
* This is SqlSession is managed by spring. Users should not commit/rollback/close it
* because it will be automatically done.
*
* @return Spring managed thread safe SqlSession
*/
public final SqlSession getSqlSession() {
return this.sqlSession;
} /**
* {@inheritDoc}
*/
protected void checkDaoConfig() {
notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
} }

  从上面的源码可以看出:在方法setSqlSessionFactory和setSqlSessionTemplate方法上面都标注有:“@Autowired(required = false)”这样的注解。

所以我们在编写dao层级代码的时候只需要dao直接继承SqlSessionDaoSupport,并标注注解@Repository,然后就可以使用类似的getSqlSession().selectList("User.selectUsers");这样的方法来使用它了,而且在spring的配置文件中的配置也比较少:

 <tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true"/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

  

  但是升级到1.2之后,我们看看SqlSessionDaoSupport的源代码:

public abstract class SqlSessionDaoSupport extends DaoSupport {

  private SqlSession sqlSession;

  private boolean externalSqlSession;

  public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (!this.externalSqlSession) {
this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
}
} public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSession = sqlSessionTemplate;
this.externalSqlSession = true;
} /**
* Users should use this method to get a SqlSession to call its statement methods
* This is SqlSession is managed by spring. Users should not commit/rollback/close it
* because it will be automatically done.
*
* @return Spring managed thread safe SqlSession
*/
public SqlSession getSqlSession() {
return this.sqlSession;
} /**
* {@inheritDoc}
*/
protected void checkDaoConfig() {
notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
} }

  

  从上面的源码可以看出:在方法setSqlSessionFactory和setSqlSessionTemplate方法上面现在都没有标注有:“@Autowired(required = false)”这样的注解。

如果一些系统直接从mybatis-spring1.1.1升级到1.2版本的时候,就会出现问题。

在1.2版本下面有几种方式来使用:

第一种,基于注解:

@Repository
public class UserDao extends SqlSessionDaoSupport{
public List<User> userList() {
return getSqlSession().selectList("User.selectUsers");
} @Override
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
}

  

  我们自己重写set方法就可以了。在这种情况下spring的配置文件不需要修改。这个实例是随意写的,如果你的工程中dao类很多(绝大多数情况都是),这样你就可以编写一个BaseDao,然后在这个BaseDao中重写这个方法,其他的dao只需要继承这个BaseDao就可以了。

第二章基于xml文件配置:

public class UserDao extends SqlSessionDaoSupport {
public List<User> userList() {
return getSqlSession().selectList("User.selectUsers");
}
}

  

  但是需要在spring的配置文件中增加这个UserDao的配置:

    <bean id="userDao" class="com.xxx.paginator.dao.UserDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

  

  第一种基于注解的配置,好处是不需要编写xml,但是这种比较容易侵入业务逻辑。

第二种基于xml配置,好处是不侵入业务逻辑,但是当dao的数量很多的时候,需要在xml中配置好多。

所以最后具体选择哪种,大家可以结合自己的情况。

mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题的更多相关文章

  1. 多个mapper location时, mybatis spring的自动扫描配置

    1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔 2. SqlSessionFactoryBean里面的mapperLocations ...

  2. 疑惑的 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L

    在MAVEN项目里面,在整合spring和mybatis在执行数据库操作的时候报出了: java.lang.AbstractMethodError: org.mybatis.spring.transa ...

  3. Spring Boot 2.0 升级指南

    Spring Boot 2.0 升级指南 前言 Spring Boot已经发布2.0有5个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把项目中Spring Boot 版本做了升级,顺便整理下 ...

  4. spring3.0+mybatis+spring快速入门

    一.首先奉上项目目录结构: 说明: dao,mapping,model包下的所有内容可以使用Generator工具自助生成. 具体用法,可以网上学习一下,比较简单,主要做以下工作: 1.提供相关的数据 ...

  5. springMVC+mybatis+spring整合案例

    1.web.xml a:配置spring监听,使web容器在启动时加载spring的applicationContext.xml <listener> <listener-class ...

  6. SpringMVC+Mybatis+Spring整合

    Maven引入需要的JAR包 pom.xml <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEA ...

  7. MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql

    一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...

  8. myBatis,Spring,SpringMVC三大框架ssm整合模板

    整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration>    <!-- 批量别名的设置 -- ...

  9. MyBatis详解 与配置MyBatis+Spring+MySql

    MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML 和注解来配置和映射基本 ...

随机推荐

  1. iOS开发如何提高(from 唐巧的博客)

    http://blog.devtang.com/blog/2014/07/27/ios-levelup-tips/

  2. javascript中的hasOwnProperty和isPrototypeOf

    hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.isPrototypeOf ...

  3. JavaScript基于时间的动画算法

    转自:https://segmentfault.com/a/1190000002416071 前言 前段时间无聊或有聊地做了几个移动端的HTML5游戏.放在不同的移动端平台上进行测试后有了诡异的发现, ...

  4. final-----finalize----finally---区别

    一.性质不同 (1)final为关键字: (2)finalize()为方法: (3)finally为为区块标志,用于try语句中: 二.作用 (1)final为用于标识常量的关键字,final标识的关 ...

  5. 哎呀,发现自己不会用模块的方式用kprobe啊,弱爆了

    在内核外面编译模块,会报warning函数名undefined的错误,解决方法是把函数给export出来:EXPORT_SYMBOL 一直以来,用kprobe比较多的是kprobe event的用法, ...

  6. 继续Wcf记录点滴

    之前说wcf以tcp协议作为通信方式的话会出现很多奇怪的bug,今天我把自己遇到的比较特殊的一个exception和解决方案列出来.主要是自己记录一下,顺便方便遇到这个问题的有缘人吧!废话不多说直接上 ...

  7. vim常用命令汇总

    vim常用命令汇总: http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html 定位 本行第一个字符 ctrl+$ ...

  8. 事务的四个特性-ACID

    事务是恢复和并发控制的基本单位.   事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.   原子性(atomicity):一个事务是一个不可分割的工作单位,事务中 ...

  9. 构架高性能WEB网站的几点知识

    前言: 对于构架高性能的web网站大家都很感兴趣,本文从几点粗谈高性能web网站需要考虑的问题. HTML静态化 什么是html静态化? 说得简单点,就是把所有不是.htm或者.html的页面改为.h ...

  10. ASP.NET Web API路由规则(二)

    默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...