Springboot+jpa simply builds the environment

1. Packages that may be needed:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>2.2.3</version>
        </dependency>
<!-- jap configuration -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

2. Environment preparation

  • jdk 8

  • IDEA 2020

  • mysql 8

3. Simple use

1. Sorting of springboot

JPA built-in sorting and paging:

Sort sort =Sort.by(Sort.Direction.ASC,"id");
//Due to version issues, the previous version was used
//Sort sort =new Sort(Sort.Direction.ASC,"id");
//The first parameter indicates whether the order is descending or ascending (here it indicates ascending order)
//The second parameter indicates that you want to press your entity
//(remember that it is a variable declared in the entity, not the field corresponding to the table in the database) to sort by that variable

PageRequest pageRequest = new PageRequest(index, num, sort);
//index offset num query number sort sort
2. Spection of springboot combined with jpa

Specification built-in statement:

public class SpecificationFactory {
    // time zone object
    private static final ZoneOffset ZONE_OFFSET = ZoneOffset.of(" + 8");
    // Date and time formatting object
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * Fuzzy matching header, like %?1
     *
     * @param fieldName field name in the entity
     * @param value fixed value
     * @return encapsulated object of query conditions
     */
    public static Specification likeStart(String fieldName, String value) {
        return (root, query, cb) -> cb.like(root.get(fieldName), "%" + value);
    }

    /**
     * Fuzzy matching tail, like ?1%
     *
     * @param fieldName field name in the entity
     * @param value fixed value
     * @return encapsulated object of query conditions
     */
    public static Specification likeEnd(String fieldName, String value) {
        return (root, query, cb) -> cb.like(root.get(fieldName), value + "%");
    }

    /**
     * Complete fuzzy matching, like %?1%
     *
     * @param fieldName field name in the entity
     * @param value fixed value
     * @return encapsulated object of query conditions
     */
    private static Specification like(String fieldName, String value) {
        return likeBuild(fieldName, "%" + value + "%");
    }

    private static Specification likeBuild(String fieldName, String value) {
        return (root, query, cb) -> cb.like(root.get(fieldName), "%" + value + "%");
    }

    /**
     * Equality comparison of any values
     *
     * @param fieldName field name in the entity
     * @param value comparison value
     * @return encapsulated object of query conditions
     */
    public static <T> Specification eq(String fieldName, T value) {
        return (root, query, cb) -> cb.equal(root.get(fieldName), value);
    }

    /**
     * Compare date ranges
     *
     * @param fieldName field name in the entity
     * @param min minimum date value
     * @param max maximum date value
     * @return encapsulated object of query conditions
     */
    public static Specification betweenDate(String fieldName, Date min, Date max) {
        LocalDateTime lmin = LocalDateTime.ofInstant(min.toInstant(), ZONE_OFFSET);
        LocalDateTime lmax = LocalDateTime.ofInstant(max.toInstant(), ZONE_OFFSET);
        return (root, query, cb) -> cb.between(root.get(fieldName).as(String.class), DATE_TIME_FORMATTER.format(lmin), DATE_TIME_FORMATTER.format(lmax));
    }

    /**
     * Compare intervals of arbitrary values
     *
     * @param fieldName field name in the entity
     * @param min minimum value
     * @param max maximum value
     * @param <T>
     * @return encapsulated object of query conditions
     */
    public static <T extends Comparable> Specification between(String fieldName, T min, T max) {
        return (root, query, cb) -> cb.between(root.get(fieldName), min, max);
    }


    /**
     * Value is greater than comparison
     *
     * @param fieldName field name in the entity
     * @param value comparison value
     * @param <T>
     * @return encapsulated object of query conditions
     */
    public static <T extends Number> Specification gt(String fieldName, T value) {
        return (root, query, cb) -> cb.gt(root.get(fieldName).as(Number.class), value);
    }


    /**
     * Value greater than or equal to comparison
     *
     * @param fieldName field name in the entity
     * @param value comparison value
     * @param <T>
     * @return encapsulated object of query conditions
     */
    public static <T extends Comparable> Specification gte(String fieldName, T value) {
        return (root, query, cb) -> cb.greaterThanOrEqualTo(root.get(fieldName), value);
    }

    /**
     * Value is less than comparison
     *
     * @param fieldName field name in the entity
     * @param value comparison value
     * @param <T>
     * @return encapsulated object of query conditions
     */
    public static <T extends Number> Specification lt(String fieldName, T value) {
        return (root, query, cb) -> cb.lt(root.get(fieldName).as(Number.class), value);
    }

    /**
     * Value less than or equal to comparison
     *
     * @param fieldName field name in the entity
     * @param value comparison value
     * @param <T>
     * @return encapsulated object of query conditions
     */
    public static <T extends Comparable> Specification lte(String fieldName, T value) {
        return (root, query, cb) -> cb.lessThanOrEqualTo(root.get(fieldName), value);
    }

    /**
     * Field is null condition
     * @param fieldName field name in the entity
     * @return encapsulated object of query conditions
     */
    public static Specification isNull(String fieldName){
        return (root, query, cb) -> cb.isNull(root.get(fieldName));
    }

    /**
     * Field is not null condition
     * @param fieldName field name in the entity
     * @return encapsulated object of query conditions
     */
    public static Specification isNotNull(String fieldName){
        return (root, query, cb) -> cb.isNotNull(root.get(fieldName));
    }

    /**
     * in condition
     * @param fieldName
     * @param values
     * @return
     */
    public static Specification in(String fieldName,Object...values){
        return (root, query, cb) -> root.get(fieldName).in(values);
    }
}