您好,登錄后才能下訂單哦!
本篇內容主要講解“MyBatis-Plus中怎么使用EntityWrappe進行列表數據倒序設置”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MyBatis-Plus中怎么使用EntityWrappe進行列表數據倒序設置”吧!
項目中使用MyBatis-Plus的EntityWrapper,默認是升序,但實際項目中需要倒序顯示,就查看了一下相關資料,在這里記錄一下。
EntityWrapper是個很強大的玩意,支持多條件查詢 EntityWrapper<CompanyInfoEntity>() wrapper= new EntityWrapper<CompanyInfoEntity>(); wrapper.between(column, val1, val2) wrapper.groupBy(columns) //對應sql中分組 wrapper.eq(column, params) //相當于where條件 wrapper.in(column, value) //sql中in wrapper.notIn(column, value) //sql中 not in wrapper.orderBy(columns, isAsc) //排序 wrapper.exists(value) //相對于sql中exists查詢 wrapper.notExists(value) //相當于sql中not exists查詢 wrapper.notBetween(column, val1, val2) //相當于sql中在某個范圍內使用的between wrapper.ge(column, params) //大于等于 wrapper.le(column, params) //小于等于 wrapper.like(column, value) //模糊查詢 wrapper.having(sqlHaving, params) //條件過濾
使用.orderBy("create_time",false)就可以實現倒序排列查詢
@Override public PageUtils queryPage(Map<String, Object> params) { //EntityWrapper 可以封裝bean參數 String name=(String)params.get("name"); Page<CompanyInfoEntity> page = this.selectPage( new Query<CompanyInfoEntity>(params).getPage(), new EntityWrapper<CompanyInfoEntity>() .like(StringUtils.isNotBlank(name),"name", name)//根據公司名進行查詢 .orderBy("create_time",false)//按創建時間進行倒序查詢 ); return new PageUtils(page); }
根據上面的可以進行很多條件查詢,有興趣的可以自己試一試哦
1、 MybatisPlus 通過 EntityWrapper(簡稱 EW,MybatisPlus 封裝的一個查詢條件構造器)或者 Condition(與 EW 類似) 來讓用戶自由的構建查詢條件,簡單便捷,沒有額外的負擔, 能夠有效提高開發效率。
2、實體包裝器,主要用于處理 sql 拼接,排序,實體參數查詢等。
3、注意: 使用的是數據庫字段,不是 Java 屬性。
4、條件參數說明
測試查詢:
@Test public void testEntityWrapperSelect() { //我們需要分頁查詢tbl_employee表中,年齡在18~50之間且性別為男且姓名為Tom的所有用戶 List<Employee> emps =employeeMapper.selectPage(new Page<Employee>(1, 2), new EntityWrapper<Employee>() .between("age", 18, 50) .eq("gender", 1) .eq("last_name", "Tom") ); System.out.println(emps); //我們需要分頁查詢tbl_employee表中,年齡在18~50之間且性別為男且姓名為Tom的所有用戶 List<Employee > emps3 = employeeMapper.selectPage( new Page<Employee>(1,2), Condition.create() .between("age", 18, 50) .eq("gender", "1") .eq("last_name", "Tom") ); System.out.println(emps3); // 查詢tbl_employee表中, 性別為女并且名字中帶有"老師" 或者 郵箱中帶有"a" List<Employee> emps1 = employeeMapper.selectList( new EntityWrapper<Employee>() .eq("gender", 0) .like("last_name", "老師") //.or() // SQL: (gender = ? AND last_name LIKE ? OR email LIKE ?) .orNew() // SQL: (gender = ? AND last_name LIKE ?) OR (email LIKE ?) .like("email", "a") ); System.out.println(emps1); //查詢性別為女的, 根據age進行排序(asc/desc), 簡單分頁 List<Employee> emps2 = employeeMapper.selectList( new EntityWrapper<Employee>() .eq("gender", 0) .orderBy("age") //默認是升序 //.orderDesc(Arrays.asList(new String [] {"age"})) .last("desc limit 1,3") //在sql最后拼接 ); System.out.println(emps2); }
控制臺輸出:
selectPage方法:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)
Parameters: 18(Integer), 50(Integer), 1(Integer), Tom(String)
Total: 1
[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22]]
selectPage使用Condition:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)
Parameters: 18(Integer), 50(Integer), 1(String), Tom(String)
Total: 1
[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22]]
selectList使用or:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ? AND last_name LIKE ? OR email LIKE ?)
Parameters: 0(Integer), %老師%(String), %a%(String)
Total: 3
[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25], Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]
selectList使用orNew:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
Parameters: 0(Integer), %老師%(String), %a%(String)
Total: 3
[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25], Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]
selectList使用orderBy:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ?) ORDER BY age
Parameters: 0(Integer)
Total: 2
[Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25]]
selectList使用orderDesc:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ?) ORDER BY age DESC
Parameters: 0(Integer)
Total: 2
[Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25], Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]
selectList使用last:
Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ?) ORDER BY age desc limit 1,3
Parameters: 0(Integer)
Total: 1
[Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]
測試修改:
@Test public void testEntityWrapperUpdate() { Employee employee = new Employee(); employee.setLastName("王五"); employee.setEmail("cls@sina.com"); employee.setGender(0); employeeMapper.update(employee, new EntityWrapper<Employee>() .eq("last_name", "Tom") .eq("age", 44) ); }
控制臺輸出:
Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE (last_name = ? AND age = ?)
Parameters: 王五(String), cls@sina.com(String), 0(Integer), Tom(String), 44(Integer)
Updates: 0
測試刪除:
@Test public void testEntityWrapperDelete() { employeeMapper.delete( new EntityWrapper<Employee>() .eq("last_name", "Tom") .eq("age", 22) ); }
控制臺輸出:
Preparing: DELETE FROM tbl_employee WHERE (last_name = ? AND age = ?)
Parameters: Tom(String), 22(Integer)
Updates: 1
到此,相信大家對“MyBatis-Plus中怎么使用EntityWrappe進行列表數據倒序設置”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。