Be Extra Careful about Pitfalls of MyBatis-Plus 2.x

Posted ^_^肥仔John

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Be Extra Careful about Pitfalls of MyBatis-Plus 2.x相关的知识,希望对你有一定的参考价值。

What an Unreliable Lad You Are @TableField

Mybatis-Plus introduces many powerful annotations for us to indicate the mapping between entity properties and table fields. It\'s a great coding experience working with those annotations and IService relevant helper class. However, it\'s actually not true in Mybatis-Plus 2.x. Why? @TableField in 3.x could replace ResultMap fully, even applied in customized Mapper select statement like the example below.

package com.john.model;

@TableName("user")
public class User {
    @TableId
    private String id;
    @TableField("user_name")
    private String userName;
}
<mapper>
  <select id="getUsers" resultType="com.john.model.User">
    select * from user
  </select>
</mapper>

Believe me gentles, 2.x would let you down definitely. @TableField is out of work in above situation. What we have to do is declare the relations by ResultMap, or keep the entity property name as the same as the table field which is case-sensitive.

<mapper>
  <select id="getUsers" resultType="com.john.model.User">
    select id
           , user_name as userName
    from user
  </select>
</mapper>

The Pain I\'ve Suffered from Pagination Plugin

Pagination plugin is another effective helper, but there are many differences between 3.x and 2.x. Listed as below.

  1. TooManyResultExpression throwed when declare Page as return type of Mapper methods. List is preference.

  2. The current property of Page which is used to indicate the current page to fetch starts from 1, not 0. What the heck:(

  3. Fill in the records property of Page by manual, while the total property would be set automatically by Mybatis-Plus.

     public void selectUserPaging(Page<User> page) {
     List<User> users = dao.selectUserPaging(page);
     page.setRecords(users);
     return page;
     }
    

Conclusion

If this post could give you a favor, it\'s my pleasure

以上是关于Be Extra Careful about Pitfalls of MyBatis-Plus 2.x的主要内容,如果未能解决你的问题,请参考以下文章

be careful with "bash -ex"

2021牛客多校8K Yet Another Problem About Pi

连词词组|relax|brings about a rise in|Chance are (high)that|Have no clue|Be passionate about|Tedious|ove

牛客多校8.K.Yet Another Problem About Pi 计算几何/思维

Will vs Be Going To vs Present Continuous: Talk About the Future in English

How can you catch a process that is about to be launched, if you don’t know the PID yet?