SpringBoot+JPA 分页查询指定列并返回指定实体方式
作者:斯沃乐。 时间:2021-08-26 11:54:57
SpringBoot JPA分页查询指定列并返回指定实体
用习惯Mybatis,没用过jpa 真是各种踩坑了
脑壳疼,一个分页弄老半天,原来就一句话的事情,唉
先来说说正常的JPA如何操作
实体类对应表来创建,举个例子
@Entity
@Table(name = "td_user")
public class TdUser extends BaseModel {
private static final long serialVersionUID = 8659266017517096998L;
/**
* id
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, name = "id", length = 10)
private Long id;
/**
* 用户所属平台
*/
@Column(nullable = false, name = "partner_id", length = 11)
private Integer partnerId;
/**
* 用户名
*/
@Column(nullable = false, name = "username", length = 32, unique = true)
private String username;
/**
* 用户昵称
*/
@Column(name = "nickname", length = 64)
private String nickname;
/**
* 密码
*/
@JsonIgnore
@Column(nullable = false, name = "password", length = 16)
private String password;
/**
* id
*
* getter setter方法省略
*/
相对应的建立操作接口
@Repository
public interface TdUserRepository extends JpaRepository<TdUser, Long>, JpaSpecificationExecutor<TdUser> {
}
分页查询的时候只要一句话
// Partner partner 外部传入的分页信息
Page<TdUser> allPage = TdUserRepository.findAll(pageable);
但是有时候可能不需要返回所有字段,只要返回一部分而已,经过各种尝试,有一种最简单的方法
就是把想要返回的字段再构建成一个实体,实体的属性需要和数据库字段进行映射,然后单独写一个Repository就可以了
比如
@Entity
@Table(name = "td_user")
public class UserVO extends BaseModel {
private static final long serialVersionUID = 8659266017517096998L;
/**
* id
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, name = "id", length = 10)
private Long id;
/**
* 用户名
*/
@Column(nullable = false, name = "username", length = 32, unique = true)
private String username;
/**
* id
*
* getter setter方法省略
*/
@Repository
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
}
调用的时候
// Partner partner 外部传入的分页信息
Page<User> allPage = UserRepository.findAll(pageable);
我一开始也尝试过写sql,但是我发现返回数据会变成Page<Object[]>,经过json数列化以后数据会变成 [ ["1":"string"],...]这种样子而不是key-value的形式
改了n遍后发现这样是最简单的。。。
SpringBoot JPA实现自定义语句分页查询
例:
1.JPA持久层 InvoiceRepository.java
@Repository
public interface InvoiceRepository extends JpaRepository<Invoice, Integer> {
@Query(
value =
"SELECT * from invoice_apply where company_id=?1 and IF (?2 is null ,1=1,status = ?2)",
countQuery =
"select count(*) from invoice_apply where company_id=?1 and IF (?2 is null ,1=1,status = ?2)",
nativeQuery = true)
Page<Map> findInvoice(int companyID, String status, Pageable pageable);
}
2.服务层
@Override
public Map findInvoice(int companyID, String status, Integer page, Integer pageSize) {
Double amount = companyFinanceRepository.findDCompanyFinance(companyID);
//分页查询
Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.ASC, "id");
Page<Map> invoiceList = invoiceRepository.findInvoice(companyID, status, pageable);
//重组返回结果
Map map = new HashMap();
map.put("invoice_amount", amount);
map.put("list", invoiceList.getContent());//数据列表
map.put("total", invoiceList.getTotalElements());//记录总条数
map.put("current_page", invoiceList.getNumber());//当前页码
return map;
}
来源:https://blog.csdn.net/FRYAN28/article/details/109134794
标签:SpringBoot,JPA,分页查询,指定列,指定实体
0
投稿
猜你喜欢
SpringCloudConfig之client端报错Could not resolve placeholder问题
2023-11-23 11:19:17
eclipse maven 插件的安装和配置详解
2023-08-24 16:57:01
Java利用反射对list对象做过滤
2023-12-23 07:39:17
Mybatis关联查询结果集对象嵌套的具体使用
2021-07-12 22:09:18
SPRING IOC注入方式过程解析
2023-10-11 12:59:13
SpringBoot v2.2以上重复读取Request Body内容的解决方案
2022-06-27 11:00:16
Java中List.of()和Arrays.asList()的区别及原因分析
2023-08-06 00:33:16
常见的java面试题
2023-11-26 18:03:44
Java maven三种仓库,本地仓库,私服,中央仓库的配置
2023-04-13 12:05:17
一文带你了解SpringBoot的启动原理
2023-11-28 20:44:42
C#使用Objects Comparer进行对象比较
2022-06-03 05:40:30
细说C#中的枚举:转换、标志和属性
2021-07-10 06:01:05
图解Java线程的生命周期
2022-04-12 15:16:10
@Autowired注解在抽象类中失效的原因及解决
2021-07-15 06:12:04
Unity实现俄罗斯方块
2021-05-28 13:37:08
AQS加锁机制Synchronized相似点详解
2023-08-04 22:36:55
浅析C# 基础语法的使用
2023-05-16 13:24:50
Java面向对象基础知识之委托和lambda
2022-07-28 16:51:11
C#中应用程序集的装载过程详解
2023-03-30 19:06:05
Java 数据库连接池详解及简单实例
2023-08-10 15:54:07