mybatis-plus自定義排序的實現(xiàn)
更新時間:2023年01月09日 11:22:27 作者:Archie_java
本文主要介紹了mybatis-plus自定義排序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
需求:
- 先時間升序排序,相同的時間在按狀態(tài)排序,
- 狀態(tài)的順序為1 在線 4 潛伏 2 隱身 3 離開,
- 狀態(tài)相同在按姓名升序排序
- 對排序好的數(shù)據(jù)進行分頁
- 運用mybatis-plus中QueryWrapper
1.導(dǎo)入依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>
2.配置文件
spring: # 配置數(shù)據(jù)源信息 datasource: # 配置數(shù)據(jù)源類型 type: com.zaxxer.hikari.HikariDataSource # 配置連接數(shù)據(jù)庫信息 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false username: root password: 123456 mybatis-plus: configuration: # 配置MyBatis日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.創(chuàng)建分頁需要的緩存
@Configuration @MapperScan("scan.your.mapper.package") public class MybatisPlusConfig { /** * 新的分頁插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問題(該屬性會在舊插件移除后一同移除) */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); return interceptor; } }
4.創(chuàng)建實體類
@Data public class User { private Integer id; private String name; private Integer age; private String email; private Integer state;//1 在線 4 潛伏 2 隱身 3 離開 private Date time; }
5.mapper
public interface UserMapper extends BaseMapper<User> { }
在啟動類上加了@MapperScan(“com.example.mapper”)
6.測試
@Test void selectUser(){ QueryWrapper<User> wrapper=new QueryWrapper<>(); //時間升序排序 wrapper.lambda().orderByAsc(User::getTime); //狀態(tài)的自定義排序,和姓名排序 wrapper.orderByAsc(" field(state,1,4,2,3)"); wrapper.lambda().orderByAsc(User::getName); //分頁 Page page=new Page<>(); page.setSize(3);//每頁的長度 page.setPages(1);//第幾頁 userMapper.selectPage(page,wrapper); }
7.結(jié)果
到此這篇關(guān)于mybatis-plus自定義排序的實現(xiàn)的文章就介紹到這了,更多相關(guān)mybatis-plus 自定義排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql+spring+mybatis實現(xiàn)數(shù)據(jù)庫讀寫分離的代碼配置
今天小編就為大家分享一篇關(guān)于mysql+spring+mybatis實現(xiàn)數(shù)據(jù)庫讀寫分離的代碼配置,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Java Builder模式實現(xiàn)原理及優(yōu)缺點解析
這篇文章主要介紹了Java Builder模式實現(xiàn)原理及優(yōu)缺點解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10SpringCloud Edgware.SR3版本中Ribbon的timeout設(shè)置方法
今天小編就為大家分享一篇關(guān)于SpringCloud Edgware.SR3版本中Ribbon的timeout設(shè)置方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12