Spring boot JPA实现分页和枚举转换代码示例

时间:2022-06-25 22:52:54

1.实现分页

方法一:使用Pageable

使用Pageable作为入参传入Repository,返回值用Page<T>接收

UserRepository

?
1
2
3
4
5
6
7
package com.kinglead.demo.dao;
 
 import com.kinglead.demo.entity.User;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 public interface UserRepository extends JpaRepository<User, Long> {
 }

UserServiceImpl

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.kinglead.demo.service.impl;
 
 import com.kinglead.demo.dao.UserRepository;
 import com.kinglead.demo.entity.User;
 import com.kinglead.demo.service.UserService;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 
 @Service
 public class UserServiceImpl implements UserService {
 
   @Resource
   private UserRepository userRepository;
 
   /**
   * 查询用户列表
   */
   @Override
   public Page<User> queryAll(Pageable pageable) {
     return userRepository.findAll(pageable);
   }
 
 }

UserService

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.kinglead.demo.service;
 
 import com.kinglead.demo.entity.User;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 
 import java.util.List;
 import java.util.Map;
 
 public interface UserService {
   /**
   * 查询用户列表
   */
   Page<User> queryAll(Pageable pageable);
 }

Controller

?
1
2
3
4
5
6
7
8
9
10
/**
 * 查询用户列表
 */
 @GetMapping("/userList")
 public Page<User> queryAll(){
   //注意,前端页面的页面是从1开始,而JPA是从0开始
   Pageable pageable = PageRequest.of(0,5);
   //查询用户列表
   return userService.queryAll(pageable);
 }

方法二:以元模型概念为基础的Criteria 查询方法

UserRepository额外继承JpaSpecificationExecutor<>

?
1
2
3
4
5
6
7
8
package com.kinglead.demo.dao;
 
 import com.kinglead.demo.entity.User;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 
 public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
 }

UserServiceImpl

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
   * 查询用户列表
   */
   @Override
   public Page<User> queryAll(String name,String age,Pageable pageable) {
       Specification<User> specification = (Specification<User>)(root, query, criteriaBuilder) ->{
         List<Predicate> list = new ArrayList<>();
         // 第一个name为User实体对象中的字段,第二个name为参数
         Predicate p1 = criteriaBuilder.equal(root.get("name"),name);
         list.add(p1);
 //        if (!age.equals(null)) {
 //          // 此处为查询serverName中含有age的数据
 //          Predicate p2 = criteriaBuilder.like(root.get("age"),"%"+age+"%" );
 //          list.add(p2);
 //        }
         return criteriaBuilder.and(list.toArray(new Predicate[0]));
       };
       return userRepository.findAll(specification,pageable);
   }

其它代码如方法一,不用动。

2.枚举转换

**方法一:实体类加@Enumerated注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.kinglead.demo.enums;
 
import com.fasterxml.jackson.annotation.JsonValue;
 
public interface BaseEnum<K> {
 
  /**
   * 真正与数据库进行映射的值
   *
   * @return
   */
  K getCode();
 
  /**
   * 显示的信息
   *
   * @return
   */
  @JsonValue //jackson返回报文response的设置
  String getDisplayName();
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.kinglead.demo.enums;
 
 public enum GenderEnum implements BaseEnum<String> {
 
   MALE("MALE","男"),
   FEMALE("FEMALE","女");
 
 
   private final String code;
   private final String displayName;
 
   GenderEnum(String code, String displayName) {
     this.code = code;
     this.displayName = displayName;
   }
 
   @Override
   public String getCode() {
     return code;
   }
 
   @Override
   public String getDisplayName() {
     return displayName;
   }}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.kinglead.demo.entity;
 
 import com.kinglead.demo.enums.GenderEnum;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import javax.persistence.*;
 import java.io.Serializable;
 
 @Data //添加getter、setter方法
 @NoArgsConstructor  //无参构造函数
 @AllArgsConstructor //所以参数构造函数
 @Entity //声明为JPA实体
 @Table(name = "t_user") //该标注与@Entity标注并列使用,用于指明数据库的表名
 public class User implements Serializable {
   private static final long serialVersionUID = -21070736985722463L;
 
   @Id //指明主键
   @GeneratedValue(strategy= GenerationType.AUTO)
   private Long id;
 
   @Column(name = "name", columnDefinition = "姓名") //指明字段
   private String name;
 
   @Column(name = "age", columnDefinition = "年龄") //指明字段
   private Long age;
 
   @Column(name = "email", columnDefinition = "邮箱") //指明字段
   private String email;
 
   @Column(name = "gender", columnDefinition = "性别") //指明字段
   @Enumerated(EnumType.STRING)
   private GenderEnum gender;
 
 }

方法二:使用jpa2.1规范里面的属性转换器

将方法一中实体对象User的gender成员变量上的注解@Enumerated(EnumType.STRING)去掉

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.kinglead.demo.config;
 
 import com.kinglead.demo.enums.GenderEnum;
 
 import javax.persistence.AttributeConverter;
 import javax.persistence.Converter;
 
 @Converter(autoApply = true)
 public class ColorConverter implements AttributeConverter<GenderEnum,String> {
 
   @Override
   public String convertToDatabaseColumn(GenderEnum attribute) {
     return attribute.getCode();
   }
 
   @Override
   public GenderEnum convertToEntityAttribute(String dbData) {
     return GenderEnum.valueOf(dbData);
   }
 }

上面的转换器只是针对某一种枚举进行转换,如果写成通用的转换器呢,后续研究

源码地址:https://github.com/kinglead2012/myblog

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/kinglead/p/13748214.html