MyBatis-generator使用,Example缺少分页问题解决
生活随笔
收集整理的這篇文章主要介紹了
MyBatis-generator使用,Example缺少分页问题解决
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、分頁問題
MyBatis自動生成代碼沒有分頁功能。
如:
public class User {private Integer userId;private String name;public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public String getName() {return name;}public void setName(String name) {this.name = name;} }pubilc class Test{public static void main(String[] args){UserExample user= new UserExample();user.createCriteria().andNameLike("%" + "zhansan" + "%");user.setOrderByClause("user_id asc");List<User> uList = userMapper.selectByExample(user);}}?
二、解決方案
1、改寫selectByExample方法和XML,增加分頁字段;具有侵入性,下一次自動生成代碼失效
2、新增分頁查詢@Select注解方法;需要額外的Mapper基接口
public interface UserMapper extends UserBaseMapper { //... generate method }public interface UserBaseMapper {@Select({"<script>","select u.user_id as userId, u.user_name as `name` ","from user u ","where 1=1 ","<if test=\"u!=null\"> "," <if test=\"u.userId!=null\"> "," and u.user_id=#{u.userId} "," </if>"," <if test=\"u.name!=null and u.name.length()>0\"> "," and u.`name`=#{u.name} "," </if> ","</if> "," limit #{index},#{limit}","</script>"})public List<User> select(@Param("u") User user, @Param("index") int index, @Param("limit") int limit); }3、改造Example的setOrderByClause()方法,使它支持分頁
利用Mapper.xml的orderByClause使用$插值的方式,進行注入分頁SQL limit
UserExample user= new UserExample(); user.createCriteria().andUserIdEqualTo(1); user.setOrderByClause("user_id asc limit 0,20");//分頁功能 List<User> uList = userMapper.selectByExample(user);?
?
總結
以上是生活随笔為你收集整理的MyBatis-generator使用,Example缺少分页问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度开源的分布式唯一ID生成器UidGe
- 下一篇: 小米14即将发布 采用极窄边框设计有望成