欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus自定義排序的實(shí)現(xiàn)

 更新時(shí)間:2023年01月09日 11:22:27   作者:Archie_java  
本文主要介紹了mybatis-plus自定義排序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

需求:

  • 先時(shí)間升序排序,相同的時(shí)間在按狀態(tài)排序,
  • 狀態(tài)的順序?yàn)? 在線 4 潛伏 2 隱身 3 離開,
  • 狀態(tài)相同在按姓名升序排序
  • 對(duì)排序好的數(shù)據(jù)進(jìn)行分頁(yè)
  • 運(yùn)用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ù)庫(kù)信息
    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)建分頁(yè)需要的緩存

@Configuration
@MapperScan("scan.your.mapper.package")
public class  MybatisPlusConfig {
    /**
     * 新的分頁(yè)插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問(wèn)題(該屬性會(huì)在舊插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
 
}

4.創(chuàng)建實(shí)體類

@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> {
}

在啟動(dòng)類上加了@MapperScan(“com.example.mapper”)

6.測(cè)試

  @Test
    void selectUser(){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        //時(shí)間升序排序
        wrapper.lambda().orderByAsc(User::getTime);
        //狀態(tài)的自定義排序,和姓名排序
        wrapper.orderByAsc(" field(state,1,4,2,3)");
        wrapper.lambda().orderByAsc(User::getName);
        //分頁(yè)
        Page page=new Page<>();
        page.setSize(3);//每頁(yè)的長(zhǎng)度
        page.setPages(1);//第幾頁(yè)
        userMapper.selectPage(page,wrapper);
    }

7.結(jié)果

img

 到此這篇關(guān)于mybatis-plus自定義排序的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mybatis-plus 自定義排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論