SpringBoot+JPA 分页查询指定列并返回指定实体方式

网友投稿 1327 2022-11-17

SpringBoot+JPA 分页查询指定列并返回指定实体方式

SpringBoot+JPA 分页查询指定列并返回指定实体方式

目录SpringBoot JPA分页查询指定列并返回指定实体实体类对应表来创建,举个例子SpringBoot JPA实现自定义语句分页查询1.JPA持久层 InvoiceRepository.java2.服务层

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, JpaSpecificationExecutor {

}

分页查询的时候只要一句话

// Partner partner 外部传入的分页信息

Page 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, JpaSpecificationExecutor {

}

调用的时候

// Partner partner 外部传入的分页信息

Page allPage = UserRepository.findAll(pageable);

我一开始也尝试过写sql,但是我发现返回数据会变成Page,经过json数列化以后数据会变成 [ ["1":"string"],...]这种样子而不是key-value的形式

改了n遍后发现这样是最简单的。。。

SpringBoot JPA实现自定义语句分页查询

例:

1.JPA持久层 InvoiceRepository.java

@Repository

public interface InvoiceRepository extends JpaRepository {

@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 findInvoice(int companyID, String status, Pageable pageable);

}

2.服务层

@Override

public Map findInvoice(int companyID, String status, Integer page, Integer phttp://ageSize) {

Double amount = companyFinanceRepository.findDCompanyFinance(companyID);

//分页查询

Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.ASC, "id");

Page 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;

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:洛谷T160512 G - 森林(并查集)
下一篇:linux容器技术和Docker
相关文章

 发表评论

暂时没有评论,来抢沙发吧~