论坛首页 Java企业应用论坛

mybatis-3.1.0版本的SqlSession接口发生变化了

浏览 7981 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-02-01   最后修改:2012-02-01
一直挺反感MyBatis每次更新不写更新文档的,也没有一个开发的路线图。但我项目一直都在用ibatis及现在的mybatis,所以对功能的变更很关注,每次都只能从不同版本的源代码比较才能发现变动情况,最近又看了一下mybatis-3.1.0-SNAPSHOT-bundle.zip的源代码,发现org.apache.ibatis.session.SqlSession接口较3.0.X版本发生了变动,以前的几个selectOne方法及selectList方法的返回值变成了泛型。应该说这样改变后,调用就方便一些了,不用我们每次都做一次强制类型转换。
老org.apache.ibatis.session.SqlSession接口方法
  
  Object selectOne(String statement);

  Object selectOne(String statement, Object parameter);

  List selectList(String statement);

  List selectList(String statement, Object parameter);

  List selectList(String statement, Object parameter, RowBounds rowBounds);


新org.apache.ibatis.session.SqlSession接口方法
  /**
   * Retrieve a single row mapped from the statement key
   * @param <T> the returned object type
   * @param statement
   * @return Mapped object
   */
  <T> T selectOne(String statement);

  /**
   * Retrieve a single row mapped from the statement key and parameter.
   * @param <T> the returned object type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @return Mapped object
   */
  <T> T selectOne(String statement, Object parameter);

  /**
   * Retrieve a list of mapped objects from the statement key and parameter.
   * @param <E> the returned list element type
   * @param statement Unique identifier matching the statement to use.
   * @return List of mapped object
   */
  <E> List<E> selectList(String statement);

  /**
   * Retrieve a list of mapped objects from the statement key and parameter.
   * @param <E> the returned list element type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @return List of mapped object
   */
  <E> List<E> selectList(String statement, Object parameter);

  /**
   * Retrieve a list of mapped objects from the statement key and parameter,
   * within the specified row bounds.
   * @param <E> the returned list element type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @param rowBounds  Bounds to limit object retrieval
   * @return List of mapped object
   */
  <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);

对比了一下,这种返回方式在mybatis的spring支持包mybatis-spring-1.0.0-RC2-bundle中出现过,当时,这个包中的SqlSessionTemplate是这么定义的:
SqlSessionTemplate extends JdbcAccessor implements SqlSessionOperations

再看看SqlSessionOperations接口
package org.mybatis.spring;

import java.util.List;

import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;


public interface SqlSessionOperations {

    /**
     * @see org.apache.ibatis.session.SqlSession#selectOne(String)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    Object selectOne(String statement);

    /**
     * @see org.apache.ibatis.session.SqlSession#selectOne(String, Object)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    Object selectOne(String statement, Object parameter);

    /**
     * @see org.apache.ibatis.session.SqlSession#selectList(String, Object)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    <T> List<T> selectList(String statement);

    /**
     * @see org.apache.ibatis.session.SqlSession#selectList(String, Object)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    <T> List<T> selectList(String statement, Object parameter);

    /**
     * @see org.apache.ibatis.session.SqlSession#selectList(String, Object, org.apache.ibatis.session.RowBounds)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    <T> List<T> selectList(String statement, Object parameter, RowBounds rowBounds);

//    /**
//     * @see org.apache.ibatis.session.SqlSession#selectMap(String statement, String mapKey)
//     * @throws org.springframework.dao.DataAccessException in case of errors
//     */
//    <K, T> Map<K, T> selectMap(String statement, String mapKey);
//
//    /**
//     * @see org.apache.ibatis.session.SqlSession#selectMap(String statement, Object parameter, String mapKey)
//     * @throws org.springframework.dao.DataAccessException in case of errors
//     */
//    <K, T> Map<K, T> selectMap(String statement, Object parameter, String mapKey);
//
//    /**
//     * @see org.apache.ibatis.session.SqlSession#selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds)
//     * @throws org.springframework.dao.DataAccessException in case of errors
//     */
//    <K, T> Map<K, T> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);

    /**
     * @see org.apache.ibatis.session.SqlSession#select(String, Object, org.apache.ibatis.session.ResultHandler)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    void select(String statement, Object parameter, ResultHandler handler);

    /**
     * @see org.apache.ibatis.session.SqlSession#select(String, org.apache.ibatis.session.ResultHandler)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    void select(String statement, ResultHandler handler);

    /**
     * @see org.apache.ibatis.session.SqlSession#select(String, Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);

    /**
     * @see org.apache.ibatis.session.SqlSession#insert(String)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    int insert(String statement);

    /**
     * @see org.apache.ibatis.session.SqlSession#insert(String, Object)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    int insert(String statement, Object parameter);

    /**
     * @see org.apache.ibatis.session.SqlSession#update(String)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    int update(String statement);

    /**
     * @see org.apache.ibatis.session.SqlSession#update(String, Object)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    int update(String statement, Object parameter);

    /**
     * @see org.apache.ibatis.session.SqlSession#delete(String)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    int delete(String statement);

    /**
     * @see org.apache.ibatis.session.SqlSession#delete(String, Object)
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    int delete(String statement, Object parameter);

    /**
     * @see org.apache.ibatis.session.SqlSession#getMapper(Class) 
     * @throws org.springframework.dao.DataAccessException in case of errors
     */
    <T> T getMapper(Class<T> type);

}


可惜后来的mybatis-spring版本中的SqlSessionTemplate类改为实现SqlSession接口,因为Mybatis3.0.X的SqlSession接口selectList方法返回的是原始的List,导至每次调用都要强制转换,现在Mybatis3.1将SqlSession接口的selectList方法改为泛型,自然再调用SqlSessionTemplate将不再需要做强制转换了。期待release版本早点出来。
   发表时间:2012-02-02  
挺用心的
自从接触了模型驱动的思想后,dao这层的设计,已经想转用hibernate了
0 请登录后投票
   发表时间:2012-02-02  
感谢楼主的分享,这个帖子很有用。
0 请登录后投票
   发表时间:2012-02-08  
总体来说,我觉得相对于ibatis ,mybatis用起来好像跟方便了一些,个人觉得!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics