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的配置文件不需要修改。这个实例是随意写的,如果你的工程中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,但是这种比较容易侵入业务逻辑。

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

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

时间: 2024-08-03 19:56:55

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

mybatis spring 整合 junit测试。 事务不起作用,不提交。删除无效???

问题描述 mybatis spring 整合 junit测试. 事务不起作用,不提交.删除无效??? applicationContext.xml 中的数据库和 sessionFactory以及事务配置 Xml代码 <bean id="bssDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connec

mybatis spring-异常org.mybatis.spring.MyBatisSystemException:

问题描述 异常org.mybatis.spring.MyBatisSystemException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements

mybatis +spring 整合时候出现了问题

问题描述 mybatis +spring 整合时候出现了问题 异常:java.lang.ClassNotFoundException: org.apache.ibatis.session.SqlSessionFactory at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645) at org.apache.catalina.loader.WebappClassLoader.loa

springmvc+mybatis+spring框架处理checkbox

问题描述 springmvc+mybatis+spring框架处理checkbox springmvc+mybatis+spring框架,前台页面checkbox类型数据的value值如何传输到后台的控制器,大神求解,我在做一个系统超级管理员设置其他管理员权限的时候,数据库有张权限表,而且这张表对应的实体类数据类型是Boolean,我想在前台用checkbox勾选权限,然后 将value值传到后台进行修改,但是checkbox勾选后,值传不到控制器,大家有木有解决的方法啊~这是对应的实体类和前台

一、MyBatis简介与配置MyBatis+Spring+MySql

 一.MyBatis简介与配置MyBatis+Spring+MySql 2013-09-06 17:03:24 标签:动态 下载地址 MyBatis学习 之 一.MyBatis简介与配置MyBatis+Spring+MySql MyBatis学习 之 二.SQL语句映射文件(1)resultMap MyBatis学习 之 二.SQL语句映射文件(2)增删改查.参数.缓存 MyBatis学习 之 三.动态SQL语句 MyBatis学习 之 四.MyBatis配置文件 1.1MyBatis简介  

update无效-mybatis+spring+strust2 后update操作不报错但没起效果

问题描述 mybatis+spring+strust2 后update操作不报错但没起效果 日志如上图 sql语句文件如上图 有两个疑问: 1 做insert操作无任何问题,update 操作不报错,就是更新无效果,数据还是原来的数据. 2 sql语句的配置xml文件中判断了if xxx!=null 但是日志里打印出来的sql还是有null (如图2) 解决方案 你的if条件有问题,如果你的参数里面已经有title等参数,就应该写为title!=null而不是带#的 解决方案二: http://

mybatis ibatis-框架mybatis+spring+struts2

问题描述 框架mybatis+spring+struts2 getSqlSession().selectList("api.getReadFAQ"); getSqlSession().selectList("api.getReadFAQ"); 有时候突然在执行到这句代码的时候就卡主了 也不报错, 跪求原因,谢谢各位大神了,但是同样的语句 ,在别的主机上就可以执行,奇怪的很,为我是集群了2个tomcat,其中一台就好着,就这台每天偶尔都胡出现这个问题,愁死我了

并发-mybatis spring事务管理问题

问题描述 mybatis spring事务管理问题 mybatis把一个对象查询出来,让后在对这个对象做相关业务操作,然后再保存这个对象, 这样有个问题就是,在并发情况下,这个对象不是数据库最新的数据,有方法可以避免吗? 解决方案 在对象查询前面做一次对象状态更新,标记正要更新的状态.然后再查询出来,做业务操作,保存时把标记复原. 解决方案二: 可以加锁,或者线程,锁住对象就行了 解决方案三: 可以加锁,或者线程,锁住对象就行了

spring mvc-java maven+mybatis +spring MVC 项目添加问题

问题描述 java maven+mybatis +spring MVC 项目添加问题 最近做这个项目,新手111 解决方案 项目左上角有个红叉,说明工程配置有问题,有可能是你eclipse默认配置的JDK的版本和别人的不一样.或者是web项目版本问题 解决方案二: Maven+Spring+Spring MVC+Mybatis项目实战 解决方案三: 什么问题啊?没看出来要问什么问题 解决方案四: 仔细看看红叉是什么问题,是环境问题,还是js文件扫描不通过 解决方案五: 你这么问问题也是醉了 解决