- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要创建一个端点来返回各州的人口普查数据和城市列表,我目前使用两个端点来获取它。
当前响应:
自定义查询一:censusByState
[
{
"id": 1,
"code": 11,
"name": "Rondônia",
"statePopulation": 1815278,
"countCities": 52
},
{
"id": 2,
"code": 12,
"name": "Acre",
"statePopulation": 906876,
"countCities": 22
},
{...
},
{
"id": 27,
"code": 53,
"name": "Distrito Federal",
"statePopulation": 3094325,
"countCities": 1
}
]
自定义查询二:censusCitiesByState
[
{
"code": 1100015,
"name": "Alta Floresta d Oeste",
"cityPopulation": 22516
},
{
"code": 1100023,
"name": "Ariquemes",
"cityPopulation": 111148
},
{...
},
{
"code": 1101807,
"name": "Vale do Paraíso",
"cityPopulation": 6490
}
]
预期响应:
[
{
"id": 1,
"code": 11,
"name": "Rondônia",
"statePopulation": 1815278,
"countCities": 52,
"cities": [
{
"code": 1100015,
"name": "Alta Floresta d Oeste",
"cityPopulation": 22516
},
{...
},
{
"code": 1101807,
"name": "Vale do Paraíso",
"cityPopulation": 6490
}
]
},
{...
},
{
"id": 2,
"code": 12,
"name": "Acre",
"statePopulation": 906876,
"countCities": 22,
"cities":[
{
"code": 1200013,
"name": "Acrelândia",
"cityPopulation": 15721
},
{...
},
{
"code": 1200807,
"name": "Porto Acre",
"cityPopulation": 19141
}
]
}
]
你能帮帮我吗?
我的类(class):
型号
状态
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "states")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class State {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer code;
private String name;
@Column(length = 2)
private String uf;
}
城市
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "cities")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "state_id", nullable = false, foreignKey = @ForeignKey(name = "fk_cities_states1"))
private State state;
private String code;
private String name;
}
人口普查
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "census_population")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class CensusPopulation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "census_id", nullable = false, foreignKey = @ForeignKey(name = "fk_census_population_census1"))
private Census census;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "city_id", nullable = false, foreignKey = @ForeignKey(name = "fk_census_population_cities1"))
private City city;
private Long population;
}
界面
CensusStateStats
package com.example.demo.dto;
public interface CensusStateStats {
Long getId();
Integer getCode();
String getName();
Long getStatePopulation();
Long getCountCities();
}
CensusStateCitiesStats
package com.example.demo.dto;
public interface CensusStateCitiesStats {
Integer getCode();
String getName();
Long getCityPopulation();
}
DTO
CensusStateStatsDto
package com.example.demo.dto;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class CensusStateStatsDto {
private Long id;
private Integer code;
private String name;
private Long statePopulation;
private long countCities;
public CensusStateStatsDto(CensusStateStats censusStateStatsDto) {
this.id = censusStateStatsDto.getId();
this.code = censusStateStatsDto.getCode();
this.name = censusStateStatsDto.getName();
this.statePopulation = censusStateStatsDto.getStatePopulation();
this.countCities = censusStateStatsDto.getCountCities();
}
}
CensusStateCitiesStatsDto
package com.example.demo.dto;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class CensusStateCitiesStatsDto {
private Integer code;
private String name;
private Long cityPopulation;
public CensusStateCitiesStatsDto(CensusStateCitiesStats censusStateCitiesStats) {
this.code = censusStateCitiesStats.getCode();
this.name = censusStateCitiesStats.getName();
this.cityPopulation = censusStateCitiesStats.getCityPopulation();
}
}
存储库
CensusPopulationRep
package com.example.demo.repository;
import com.example.demo.dto.CensusStateCitiesStats;
import com.example.demo.dto.CensusStateStats;
import com.example.demo.model.CensusPopulation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface CensusPopulationRep extends JpaRepository<CensusPopulation, Long> {
@Query(value = "SELECT s.id, s.code, s.name, s.uf, " +
"SUM(cp.population) AS statePopulation, " +
"COUNT(cp.id) AS countCities, " +
"FROM census_population cp " +
"INNER JOIN cities c ON c.id = cp.city_id " +
"INNER JOIN states s ON s.id = c.state_id " +
"GROUP BY s.code, s.name, s.uf"
, nativeQuery = true)
List<CensusStateStats> censusByState();
@Query(value = "SELECT c.code, c.name, " +
"SUM(cp.population) AS cityPopulation, " +
"FROM census_population cp " +
"INNER JOIN cities c ON c.id = cp.city_id " +
"WHERE c.state_id = :state " +
"GROUP BY c.code, c.name "
, nativeQuery = true)
List<CensusStateCitiesStats> censusCitiesByState(@Param("state") Long state);
}
服务
CensusPopulationService
package com.example.demo.service;
import com.example.demo.dto.CensusStateCitiesStats;
import com.example.demo.dto.CensusStateStats;
import com.example.demo.model.CensusPopulation;
import com.example.demo.repository.CensusPopulationRep;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class CensusPopulationService {
private final CensusPopulationRep censusPopulationRep;
public List<CensusPopulation> findAll() {
return censusPopulationRep.findAll();
}
public List<CensusStateStats> censusByState() {
return censusPopulationRep.censusByState();
}
public List<CensusStateCitiesStats> censusCitiesByState(Long state) {
return censusPopulationRep.censusCitiesByState(state);
}
}
Controller
CensusPopulationController
package com.example.demo.controller;
import com.example.demo.dto.CensusStateCitiesStatsDto;
import com.example.demo.dto.CensusStateStatsDto;
import com.example.demo.model.CensusPopulation;
import com.example.demo.service.CensusPopulationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("api/v1/census-population")
@RequiredArgsConstructor
public class CensusPopulationController {
private final CensusPopulationService censusPopulationService;
@GetMapping
public ResponseEntity<List<CensusPopulation>> findAll() {
return ResponseEntity.ok(censusPopulationService.findAll());
}
@GetMapping("/state-stats")
public ResponseEntity<List<CensusStateStatsDto>> censusByState() {
return ResponseEntity.ok(censusPopulationService.censusByState().stream()
.map(CensusStateStatsDto::new)
.collect(Collectors.toList()));
}
@GetMapping("/state-cities-stats")
public ResponseEntity<List<CensusStateCitiesStatsDto>> censusCitiesByState(@RequestParam(required = false) Long state) {
return ResponseEntity.ok(censusPopulationService.censusCitiesByState(state).stream()
.map(CensusStateCitiesStatsDto::new)
.collect(Collectors.toList()));
}
}
最佳答案
您可以创建一个新类 StateCityCensusDto
,然后在您对 censusCitiesByState
的查询中返回 stateId
@Data
public class StateCityCensusDto {
private StateCensus stateCensus;
private List<CityCensus> cities;
}
Integer getCode();
String getName();
Long getCityPopulation();
//add stateId retrieve from query
Long getStateId();
}
然后返回城市旁边的州。
List<CensusStateStats> stateCensusList = //retrieve from the db;
List<CensusStateCitiesStats> //retrieve from the db;
List<StateCityCensusDto> stateCityCensusList = new ArrayList<>();
stateCensusList.forEach(stateCensus -> {
StateCityCensusDto stateCityCensus = new StateCityCensusDto();
stateCityCensus.setStateCensus(stateCensus);
stateCityCensus.setCities(cityCensusList.stream()
.filter(c -> c.getStateId() == stateCensus.getId())
.collect(Collectors.toList()));
stateCityCensusList.add(stateCityCensus);
});
关于java - spring boot如何加入自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70637324/
我想对 JOIN 进行特定的排序 SELECT * FROM (lives_in as t1 NATURAL JOIN preferences p1) l1 JOIN (lives_in t2 NAT
我正在努力解决一个查询。并想知道是否有人可以提供帮助。 我有一个标签表(服务请求票)和序列号表 从我的标签中我正在这样做 Select * from tag where tag.created BET
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我有两个表 tbl_user 和 tbl_lastchangepassword,如下所示 表 tbl_user id| name --------- 1 | user1 2 | user2 3 |
我有下一个问题 SELECT i.*, gu.* vs.* FROM common.global_users gu LEFT JOIN common.global_users_perms gup ON
我有一个电影表和一个投票表。用户为他们喜欢的电影投票。我需要显示按电影总票数降序排列的电影列表。我现在所拥有的有点作品。唯一的问题是它不显示 0 票的电影。 SELECT m.name, m.imdb
我有一个由这样的表组成的 mySql 数据库: 我如何(如果可能的话)使用 JOINS 从名称/周期表中获取结果?简单来说,它是如何工作的?我向菜鸟问题道歉。我对此很陌生。任何帮助将不胜感激。 最佳答
我需要查询单元先决条件的自引用关系。 我知道您需要使用两个联接,我是否选择我的列然后将其联接到自身? SELECT u.unit_code, u.name + ' is a prerequisi
我有两个实体,用户和友谊,它们看起来像: public class User { public int UserId { get; set; } (..
假设我有两个表: Table A ProdID | PartNumber | Data... 1 | ABC-a | "Data A" 2 | (null) |
说我有这个数据, (df <- data.frame( col1 = c('My','Your','His','Thir'), col2 = c('Cat','Dog','Fish','Dog')))
我有两个这样的数组,实际上这是从两个不同的服务器检索的 mysql 数据: $array1 = array ( 0 => array ( 'id' => 1, 'n
我的数据库中有以下表格 CREATE TABLE [author_details] ( [_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [name
我正在努力使用一个相当简单的 sql select 语句的 join/where 子句。 我正在尝试从 tb1 中检索产品信息列表,其中 where 条件位于 tbl2 中,但这必须由三个不同的列连接
我正在寻找以下功能: Applicative f => f (f a) -> f a Hoogle给我看join : >:t join join :: Monad m => m (m a) -> m
我有两个“表”,分别是 USER 和 CONGE。在表“CONGE”中,我插入了用户的 ID。但是我不知道如何根据用户的id显示用户的休假。 我想根据id发布“Congé”。 { "conge"
我们有一个具有(简化)结构的文档,如Elasticsearch所示: { _id: ..., patientId: 4711, text: "blue" } { _id: ..., patientId
这两个sql语句有什么区别 a) 从 T1,T2 中选择 *,其中 T1.A=T2.A ; b) 从 T1,T2 中选择 *,其中 T2.A=T1.A ; 在这两种情况下我得到相同的输出,这两种语句之
我想做一个简单的连接,只是比较两个表中的 ID.. 我有我的组表,包含; 身份证 姓名 等.. 我的 GroupMap 表包含; 身份证 组号 元素编号 我的查询采用 GroupMap.ItemID
所以我有一组主要数据,如下所示: value_num code value_letter 1 CDX A 2 DEF B
我是一名优秀的程序员,十分优秀!