当前位置: 移动技术网 > IT编程>开发语言>Java > 详细介绍MyBatis 3.4.0版本的功能

详细介绍MyBatis 3.4.0版本的功能

2019年07月22日  | 移动技术网IT编程  | 我要评论

新增功能

1. cursor 新增返回值类型为游标的方法

当查询大量(上百万)数据的时候,使用游标可以有效的减少内存使用,不需要一次性将所有数据得到,可以通过游标逐个或者分批(逐个获取一批后)处理。

sqlsession 中新增的 3 个游标方法:

/**
 * a cursor offers the same results as a list, except it fetches data lazily using an iterator.
 * @param <t> the returned cursor element type.
 * @param statement unique identifier matching the statement to use.
 * @return cursor of mapped objects
 */
<t> cursor<t> selectcursor(string statement);

/**
 * a cursor offers the same results as a list, except it fetches data lazily using an iterator.
 * @param <t> the returned cursor element type.
 * @param statement unique identifier matching the statement to use.
 * @param parameter a parameter object to pass to the statement.
 * @return cursor of mapped objects
 */
<t> cursor<t> selectcursor(string statement, object parameter);

/**
 * a cursor offers the same results as a list, except it fetches data lazily using an iterator.
 * @param <t> the returned cursor 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 cursor of mapped objects
 */
<t> cursor<t> selectcursor(string statement, object parameter, rowbounds rowbounds);

注意: 3.4.0 版本的游标方法目前有个 bug,因此不支持 @select 注解方式,在将来的 3.4.1 版本中会解决这个问题。

使用示例:

<select id="selectall" resulttype="tk.mybatis.springboot.model.city">
 select * from city
</select>

xml 里面没有任何改变,在获取值的地方有变化,例如使用接口:

cursor<city> selectall();

或者使用命名接口方式:

cursor<city> citylist = sqlsession.selectcursor("selectall");

得到结果后,使用方法如下:

iterator<city> iterator = citylist.iterator();
while(iterator.hasnext()){
 city c2 = iterator.next();
 assert.assertnotnull(c2);
 assert.assertnotnull(c2.getname());
 assert.assertnotnull(c2.getstate());
}

嵌套查询的情况

当使用嵌套查询时,还需要设置resultordered="true"属性,使用方法如下:

<select id="selectall" resultmap="xx.citymap" resultordered="true">

只有设置这个属性才能得到当前对象 id 所对应的所有嵌套结果。

对某一个嵌套查询,设置 resultordered="true" 的结果:

不设置的结果:

以上图为例,判断是否为同一个结果下的对象,使用 id 判断的,这个 id 必须是 <resultmap> 中的 <id>,另外为了结果完整,你还需要按照 <id> 配置的列进行排序,如果结果不是 <id> 对应列的顺序,嵌套的结果数量会出错。

2. 增加对 java 8 日期(jsr-310)的支持

添加以下依赖:

<dependency>
 <groupid>org.mybatis</groupid>
 <artifactid>mybatis-typehandlers-jsr310</artifactid>
 <version>1.0.0</version>
</dependency>

如果你使用的 3.4.0 版本,就不需要任何配置就可以直接用。

如果你使用的老版本,需要手动配置:

<typehandlers>
 <typehandler handler="org.apache.ibatis.type.instanttypehandler" />
 <typehandler handler="org.apache.ibatis.type.localdatetimetypehandler" />
 <typehandler handler="org.apache.ibatis.type.localdatetypehandler" />
 <typehandler handler="org.apache.ibatis.type.localtimetypehandler" />
 <typehandler handler="org.apache.ibatis.type.offsetdatetimetypehandler" />
 <typehandler handler="org.apache.ibatis.type.offsettimetypehandler" />
 <typehandler handler="org.apache.ibatis.type.zoneddatetimetypehandler" />
</typehandlers>

有关 mybatis-typehandlers-jsr310 项目的详细信息看

3. 新增 automappingunknowncolumnbehavior 参数

新增了一个 settings 配置的参数 automappingunknowncolumnbehavior ,当检测出未知列(或未知属性)时,如何处理,默认情况下没有任何提示,这在测试的时候很不方便,不容易找到错误。

可选值:

  • none : 不做任何处理 (默认值)
  • warning : 警告日志形式的详细信息
  • failing : 映射失败,抛出异常和详细信息

配置时,在 <settings> 里面添加:

<setting name="automappingunknowncolumnbehavior" value="warning"/>

4. sql provider 注解方式支持多个参数

例如:

@selectprovider(type = usersqlbuilder.class, method = "buildgetusersbyname")
list<user> getusersbyname(
 @param("name") string name,
 @param("orderbycolumn") string orderbycolumn); // multiple arguments

在写 usersqlbuilder 的时候,同样需要使用注解来指定参数(或者按顺序):

public string buildgetusersbyname(
 @param("name") final string name
 @param("orderbycolumn") final string orderbycolumn) { // allow multiple arguments
 return new sql(){{
 select("*");
 from("users");
 if (name != null) {
  where("name like #{name} || '%'");
 }
 order_by(orderbycolumn);
 }}.tostring();
}

解决的 bug

支持实体类中的泛型类型

例如 entity 基类:

public abstract class entity<k extends serializable> {
 private static final long serialversionuid = -1l;
 protected k id;

 public k getid() {
 return id;
 }

 public void setid(k id) {
 this.id = id;
 }

其中一个子类:

public class user extends entity<string>

在先前的版本中,mybatis 无法获取 id 的实际类型,导致找不到 typehandler 出错。

这里只列举部分重要的内容,详细内容看官方说明

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网