Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use EntityWrappe to set the reverse order of list data in MyBatis-Plus

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use EntityWrappe to set the reverse order of list data in MyBatis-Plus". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use EntityWrappe to set the reverse order of list data in MyBatis-Plus".

Setting scenarios in reverse order of list data using EntityWrappe

The EntityWrapper of MyBatis-Plus is used in the project, which is in ascending order by default, but it needs to be displayed in reverse order in the actual project, so check the relevant information and record it here.

EntityWrapper is a very powerful thing that supports multi-conditional query EntityWrapper () wrapper= new EntityWrapper () Wrapper.between (column, val1, val2) wrapper.groupBy (columns) / / grouping wrapper.eq (column, params) / / equivalent to in wrapper.notIn (column, value) in where condition wrapper.in (column, value) / / not in wrapper.orderBy (columns, isAsc) / / sort wrapper.exists (value) / / relative to exists query wrapper.notExists (value) in sql / / equivalent to not exists wrapper.notBetween query in sql (column, val1) Val2) / / equivalent to between wrapper.ge (column, params) / greater than or equal to wrapper.le (column, params) / less than or equal to wrapper.like (column, value) / / Fuzzy query wrapper.having (sqlHaving, params) / / conditional filtering in a certain range in sql

You can use .orderBy ("create_time", false) to sort queries in reverse order.

@ Override public PageUtils queryPage (Map params) {/ / EntityWrapper can encapsulate the bean parameter String name= (String) params.get ("name") Page page = this.selectPage (new Query (params). GetPage (), new EntityWrapper (). Like (StringUtils.isNotBlank (name), "name", name) / / query by company name. OrderBy ("create_time", false) / / query in reverse order by creation time) Return new PageUtils (page);}

According to the above, you can query a lot of conditions, if you are interested, you can try it yourself.

Introduction to conditional constructor EntityWrapper usage EntityWrapper

1. MybatisPlus allows users to build query conditions freely through EntityWrapper (a query condition constructor encapsulated by EW,MybatisPlus) or Condition (similar to EW). It is simple and convenient, has no additional burden, and can effectively improve development efficiency.

2, entity wrapper, mainly used to deal with sql splicing, sorting, entity parameter query and so on.

3. Note: database fields are used, not Java attributes.

4. Description of condition parameters

Test query:

@ Testpublic void testEntityWrapperSelect () {/ / We need to query the tbl_employee table by page. All users aged between 18 and 50 who are male and named Tom List emps = employeeMapper.selectPage (new Page (1,2), new EntityWrapper () .between ("age", 18,50) .eq ("gender", 1) .eq ("last_name", "Tom")) System.out.println (emps) / / We need to query the tbl_employee table by page. All users between the ages of 18 and 50 and whose gender is male and whose name is Tom List emps3 = employeeMapper.selectPage (new Page (1Power2), Condition.create () .between ("age", 18,50) .eq ("gender", "1") .eq ("last_name") "Tom")) System.out.println (emps3) / / query tbl_employee table, gender is female and the name is "teacher" or mailbox with "a" List emps1 = employeeMapper.selectList (new EntityWrapper () .eq ("gender", 0) .like ("last_name", "teacher") / / .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) / / query female gender, sort according to age (asc/desc), simple paging List emps2 = employeeMapper.selectList (new EntityWrapper () .eq ("gender") 0) .orderBy ("age") / / default is ascending order / / .orderDesc (Arrays.asList (new String [] {"age"})) .last ("desc limit 1mai 3") / / spliced at the end of sql) System.out.println (emps2);}

Console output:

SelectPage method:

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 uses 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 uses 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),% teacher% (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= Wang Wu, email=xz@sina.com, gender=0, age=null]]

SelectList uses 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),% teacher% (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= Wang Wu, email=xz@sina.com, gender=0, age=null]]

SelectList uses 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= Wang Wu, email=xz@sina.com, gender=0, age=null], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25]]

SelectList uses 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= Wang Wu, email=xz@sina.com, gender=0, age=null]]

SelectList uses 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= Wang Wu, email=xz@sina.com, gender=0, age=null]]

Test modifications:

Testpublic void testEntityWrapperUpdate () {Employee employee = new Employee (); employee.setLastName ("Wang Wu"); employee.setEmail ("cls@sina.com"); employee.setGender (0); employeeMapper.update (employee, new EntityWrapper () .eq ("last_name", "Tom") .eq ("age", 44);}

Console output:

Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE (last_name =? AND age =?)

Parameters: Wang Wu (String), cls@sina.com (String), 0 (Integer), Tom (String), 44 (Integer)

Updates: 0

Test deletion:

@ Testpublic void testEntityWrapperDelete () {employeeMapper.delete (new EntityWrapper () .eq ("last_name", "Tom") .eq ("age", 22));}

Console output:

Preparing: DELETE FROM tbl_employee WHERE (last_name =? AND age =?)

Parameters: Tom (String), 22 (Integer)

Updates: 1

At this point, I believe you have a deeper understanding of "how to use EntityWrappe to set the reverse order of list data in MyBatis-Plus". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report