当前位置: 首页 > news >正文

mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题

我们公司的项目使用spring+mybatis组合。

所以就必须得使用mybatis-spring了。

所以此处就昨日mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题,做了一个总结。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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的配置文件里的配置也比較少:

1
2
3
4
5
6
7
8
9
10
11
<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的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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版本号以下有几种方式来使用:

第一种,基于注解:

1
2
3
4
5
6
7
8
9
10
11
12
@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的配置文件不须要改动。这个实例是任意写的,假设你的project中dao类非常多(绝大多数情况都是),这样你就能够编写一个BaseDao,然后在这个BaseDao中重写这种方法,其它的dao仅仅须要继承这个BaseDao就能够了。

第二章基于xml文件配置:

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

  

  可是须要在spring的配置文件里添加这个UserDao的配置:

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

  

  第一种基于注解的配置,优点是不须要编写xml,可是这样的比較easy侵入业务逻辑。

     另外一种基于xml配置。优点是不侵入业务逻辑,可是当dao的数量非常多的时候,须要在xml中配置好多。

     所以最后详细选择哪种,大家能够结合自己的情况。

相关文章:

  • 802.11无线网络部署方案对比分析
  • RHEL中FQDN解析顺序。
  • @Autowired标签与 @Resource标签 的区别
  • 《唐之韵》解说词及古诗词
  • Java导出freemarker实现下载word文档格式功能
  • Insus Meta Utility
  • 什么是多态?为什么用多态?有什么好处?[转]
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • phpmyadmin中数据显示奇怪字符解决办法
  • Windows 7 公共文件夹对话框
  • NVIDIA发布首个基于AI的癌症分布式学习环境的框架——CANDLE
  • 谈谈创业和梦想
  • 无人驾驶背后的技术 - PostGIS点云(pointcloud)应用 - 2
  • C语言封送结构体数组
  • vue双向绑定原理
  • 4. 路由到控制器 - Laravel从零开始教程
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • ES6系统学习----从Apollo Client看解构赋值
  • express.js的介绍及使用
  • HashMap剖析之内部结构
  • HTTP 简介
  • Java编程基础24——递归练习
  • Java的Interrupt与线程中断
  • Java反射-动态类加载和重新加载
  • leetcode46 Permutation 排列组合
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • Vue实战(四)登录/注册页的实现
  • 编写符合Python风格的对象
  • 两列自适应布局方案整理
  • 聊聊redis的数据结构的应用
  • 一个SAP顾问在美国的这些年
  • 优化 Vue 项目编译文件大小
  • ​HTTP与HTTPS:网络通信的安全卫士
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • (c语言)strcpy函数用法
  • (八)Spring源码解析:Spring MVC
  • (二)斐波那契Fabonacci函数
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (算法)Travel Information Center
  • (新)网络工程师考点串讲与真题详解
  • ******之网络***——物理***
  • .libPaths()设置包加载目录
  • .NET开源的一个小而快并且功能强大的 Windows 动态桌面软件 - DreamScene2
  • .NET中GET与SET的用法
  • .net专家(张羿专栏)
  • @KafkaListener注解详解(一)| 常用参数详解
  • @SentinelResource详解
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [20180129]bash显示path环境变量.txt
  • [3300万人的聊天室] 作为产品的上游公司该如何?
  • [Android]使用Git将项目提交到GitHub
  • [AutoSAR 存储] 汽车智能座舱的存储需求