源码地址:https://gitee.com/yk001/base.git
在之前单表查询的基础上
1,定义一个VO
@Data
public class BaseEntityVO {
private Long id;
private String name1;
private String name2;
private JSONObject remark1;
private JSONObject remark2;
}
2,一个简单的关联查询
@GetMapping("/baseentityvo")
public Page<BaseEntityVO> getBaseEntityVOs(
@RequestParam(name="createdTime",required = false) Timestamp createdTime,
@RequestParam(name="page",defaultValue = "0") Integer page,
@RequestParam(name="size",defaultValue = "10") Integer size) {
QBaseEntity qBaseEntity1 = QBaseEntity.baseEntity;
QBaseEntity qBaseEntity2 = QBaseEntity.baseEntity;
QBean<BaseEntityVO> qBean = Projections.bean(BaseEntityVO.class,
qBaseEntity1.id,qBaseEntity1.name.as("name1"),qBaseEntity1.remark.as("remark1"),
qBaseEntity2.name.as("name2"),qBaseEntity2.remark.as("remark2"));
JPAQuery<BaseEntityVO> query = jpaQueryFactory.select(qBean)
.from(qBaseEntity1)
.leftJoin(qBaseEntity2).on(qBaseEntity1.id.eq(qBaseEntity2.id));
query.orderBy(qBaseEntity1.id.asc()).offset(page*size).limit(size);
if(createdTime != null){
query.where(qBaseEntity1.createdTime.goe(createdTime));
}
QueryResults<BaseEntityVO> queryResults = query.fetchResults();
PageRequest pageRequest = PageRequest.of(page,size);
return new PageImpl<BaseEntityVO>(queryResults.getResults(),pageRequest,queryResults.getTotal());
}
3,验证
curl -X GET "http://localhost:9001/baseentityvo?page=0&size=10" -H "accept: */*"
{
"content": [
{
"id": 1,
"name1": "string",
"name2": "string",
"remark1": {
"additionalProp1": {},
"additionalProp3": {},
"additionalProp2": {}
},
"remark2": {
"additionalProp1": {},
"additionalProp3": {},
"additionalProp2": {}
}
},
{
"id": 2,
"name1": "string",
"name2": "string",
"remark1": {
"additionalProp1": {},
"additionalProp3": {},
"additionalProp2": {}
},
"remark2": {
"additionalProp1": {},
"additionalProp3": {},
"additionalProp2": {}
}
},
{
"id": 3,
"name1": "string",
"name2": "string",
"remark1": {
"additionalProp1": {},
"additionalProp3": {},
"additionalProp2": {}
},
"remark2": {
"additionalProp1": {},
"additionalProp3": {},
"additionalProp2": {}
}
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"pageNumber": 0,
"pageSize": 10,
"offset": 0,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 3,
"last": true,
"numberOfElements": 3,
"number": 0,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"first": true,
"size": 10,
"empty": false
}