feat: 优化Global返回值
This commit is contained in:
@@ -25,161 +25,161 @@ import org.springframework.web.client.RestTemplate;
|
||||
@EnableFenix
|
||||
@EnableJpaAuditing
|
||||
public class TestApplication {
|
||||
private static final Logger log = LoggerFactory.getLogger(TestApplication.class);
|
||||
private static final String BASE_URL = "http://localhost:2490";
|
||||
private static final RestTemplate REST_CLIENT = new RestTemplate();
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final Logger log = LoggerFactory.getLogger(TestApplication.class);
|
||||
private static final String BASE_URL = "http://localhost:2490";
|
||||
private static final RestTemplate REST_CLIENT = new RestTemplate();
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runTests() throws JsonProcessingException {
|
||||
// 增
|
||||
var cid1 = saveItem("company", "{\"name\": \"Apple\",\"members\": 10}").get("data").asLong();
|
||||
var cid2 = saveItem("company", "{\"name\": \"Banana\",\"members\": 20}").get("data").asLong();
|
||||
var cid3 = saveItem("company", "{\"name\": \"Cheery\",\"members\": 20}").get("data").asLong();
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runTests() throws JsonProcessingException {
|
||||
// 增
|
||||
var cid1 = saveItem("company", "{\"name\": \"Apple\",\"members\": 10}").get("data").asLong();
|
||||
var cid2 = saveItem("company", "{\"name\": \"Banana\",\"members\": 20}").get("data").asLong();
|
||||
var cid3 = saveItem("company", "{\"name\": \"Cheery\",\"members\": 20}").get("data").asLong();
|
||||
|
||||
// 查
|
||||
var companies = listItems("company");
|
||||
Assert.isTrue(companies.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(companies.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
// 查
|
||||
var companies = listItems("company");
|
||||
Assert.isTrue(companies.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(companies.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
|
||||
// language=JSON
|
||||
var companies2 = listItems("company", "{\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies2.at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(companies2.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
// language=JSON
|
||||
var companies3 = listItems("company", "{\n" +
|
||||
" \"query\": {\n" +
|
||||
" \"notNullEqual\": [\n" +
|
||||
" \"name\"\n" +
|
||||
" ],\n" +
|
||||
" \"equal\": {\n" +
|
||||
" \"name\": \"Apple\"\n" +
|
||||
" },\n" +
|
||||
" \"like\": {\n" +
|
||||
" \"name\": \"Appl%\"\n" +
|
||||
" },\n" +
|
||||
" \"less\": {\n" +
|
||||
" \"members\": 50\n" +
|
||||
" },\n" +
|
||||
" \"greatEqual\": {\n" +
|
||||
" \"members\": 0\n" +
|
||||
" },\n" +
|
||||
" \"in\": {\n" +
|
||||
" \"name\": [\n" +
|
||||
" \"Apple\",\n" +
|
||||
" \"Banana\"\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"between\": {\n" +
|
||||
" \"members\": {\n" +
|
||||
" \"start\": 0,\n" +
|
||||
" \"end\": 50\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies3.at("/data/items").size() == 1, "数量错误");
|
||||
Assert.isTrue(companies3.at("/data/total").asLong() == 1, "返回数量错误");
|
||||
// language=JSON
|
||||
var companies2 = listItems("company", "{\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies2.at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(companies2.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
// language=JSON
|
||||
var companies3 = listItems("company", "{\n" +
|
||||
" \"query\": {\n" +
|
||||
" \"notNullEqual\": [\n" +
|
||||
" \"name\"\n" +
|
||||
" ],\n" +
|
||||
" \"equal\": {\n" +
|
||||
" \"name\": \"Apple\"\n" +
|
||||
" },\n" +
|
||||
" \"like\": {\n" +
|
||||
" \"name\": \"Appl%\"\n" +
|
||||
" },\n" +
|
||||
" \"less\": {\n" +
|
||||
" \"members\": 50\n" +
|
||||
" },\n" +
|
||||
" \"greatEqual\": {\n" +
|
||||
" \"members\": 0\n" +
|
||||
" },\n" +
|
||||
" \"in\": {\n" +
|
||||
" \"name\": [\n" +
|
||||
" \"Apple\",\n" +
|
||||
" \"Banana\"\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"between\": {\n" +
|
||||
" \"members\": {\n" +
|
||||
" \"start\": 0,\n" +
|
||||
" \"end\": 50\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies3.at("/data/items").size() == 1, "数量错误");
|
||||
Assert.isTrue(companies3.at("/data/total").asLong() == 1, "返回数量错误");
|
||||
|
||||
var company1 = detailItem("company", cid1);
|
||||
Assert.isTrue(cid1 == company1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Apple".equals(company1.at("/data/name").asText()), "name错误");
|
||||
var company1 = detailItem("company", cid1);
|
||||
Assert.isTrue(cid1 == company1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Apple".equals(company1.at("/data/name").asText()), "name错误");
|
||||
|
||||
// 改
|
||||
var cid4 = saveItem("company", "{\"id\": %d, \"name\": \"Dog\"}".formatted(cid2)).get("data").asLong();
|
||||
Assert.isTrue(cid2 == cid4, "id错误");
|
||||
var company2 = detailItem("company", cid2);
|
||||
Assert.isTrue("Dog".equals(company2.at("/data/name").asText()), "name错误");
|
||||
// 改
|
||||
var cid4 = saveItem("company", "{\"id\": %d, \"name\": \"Dog\"}".formatted(cid2)).get("data").asLong();
|
||||
Assert.isTrue(cid2 == cid4, "id错误");
|
||||
var company2 = detailItem("company", cid2);
|
||||
Assert.isTrue("Dog".equals(company2.at("/data/name").asText()), "name错误");
|
||||
|
||||
// 删
|
||||
removeItem("company", cid3);
|
||||
Assert.isTrue(listItems("company").at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(listItems("company").at("/data/total").asLong() == 2, "返回数量错误");
|
||||
// 删
|
||||
removeItem("company", cid3);
|
||||
Assert.isTrue(listItems("company").at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(listItems("company").at("/data/total").asLong() == 2, "返回数量错误");
|
||||
|
||||
log.info(listItems("company").toPrettyString());
|
||||
log.info(listItems("company").toPrettyString());
|
||||
|
||||
var eid1 = saveItem("employee", "{\"name\": \"Tom\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid2 = saveItem("employee", "{\"name\": \"Jerry\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid3 = saveItem("employee", "{\"name\": \"Mike\",\"age\": 18, \"companyId\": %d}".formatted(cid2)).get("data").asLong();
|
||||
var eid1 = saveItem("employee", "{\"name\": \"Tom\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid2 = saveItem("employee", "{\"name\": \"Jerry\",\"age\": 18, \"companyId\": %d}".formatted(cid1)).get("data").asLong();
|
||||
var eid3 = saveItem("employee", "{\"name\": \"Mike\",\"age\": 18, \"companyId\": %d}".formatted(cid2)).get("data").asLong();
|
||||
|
||||
var employees = listItems("employee");
|
||||
Assert.isTrue(employees.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(employees.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
var employees = listItems("employee");
|
||||
Assert.isTrue(employees.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(employees.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
|
||||
var employee1 = detailItem("employee", eid1);
|
||||
Assert.isTrue(eid1 == employee1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Tom".equals(employee1.at("/data/name").asText()), "name错误");
|
||||
Assert.isTrue(18 == employee1.at("/data/age").asInt(), "age错误");
|
||||
var employee1 = detailItem("employee", eid1);
|
||||
Assert.isTrue(eid1 == employee1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Tom".equals(employee1.at("/data/name").asText()), "name错误");
|
||||
Assert.isTrue(18 == employee1.at("/data/age").asInt(), "age错误");
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private HttpHeaders headers() {
|
||||
var headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return headers;
|
||||
}
|
||||
private HttpHeaders headers() {
|
||||
var headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return headers;
|
||||
}
|
||||
|
||||
private JsonNode saveItem(String path, String body) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/save".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(body, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
private JsonNode saveItem(String path, String body) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/save".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(body, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode listItems(String path) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
private JsonNode listItems(String path) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode listItems(String path, String query) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(query, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
private JsonNode listItems(String path, String query) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(query, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode detailItem(String path, Long id) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/detail/%d".formatted(BASE_URL, path, id),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
private JsonNode detailItem(String path, Long id) throws JsonProcessingException {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/detail/%d".formatted(BASE_URL, path, id),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private void removeItem(String path, Long id) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/remove/%d".formatted(BASE_URL, path, id),
|
||||
Void.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
}
|
||||
private void removeItem(String path, Long id) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/remove/%d".formatted(BASE_URL, path, id),
|
||||
Void.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,73 +15,73 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
@RequestMapping("company")
|
||||
public class CompanyController extends SimpleControllerSupport<Company, CompanyController.SaveItem, CompanyController.ListItem, CompanyController.DetailItem> {
|
||||
public CompanyController(CompanyService service) {
|
||||
super(service);
|
||||
}
|
||||
public CompanyController(CompanyService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Company> saveItemMapper() {
|
||||
return item -> {
|
||||
var company = new Company();
|
||||
company.setId(item.getId());
|
||||
company.setName(item.getName());
|
||||
company.setMembers(item.getMembers());
|
||||
return company;
|
||||
};
|
||||
}
|
||||
@Override
|
||||
protected Function<SaveItem, Company> saveItemMapper() {
|
||||
return item -> {
|
||||
var company = new Company();
|
||||
company.setId(item.getId());
|
||||
company.setName(item.getName());
|
||||
company.setMembers(item.getMembers());
|
||||
return company;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Company, ListItem> listItemMapper() {
|
||||
return company -> new ListItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers()
|
||||
);
|
||||
}
|
||||
@Override
|
||||
protected Function<Company, ListItem> listItemMapper() {
|
||||
return company -> new ListItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Company, DetailItem> detailItemMapper() {
|
||||
return company -> new DetailItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers(),
|
||||
company.getCreatedTime(),
|
||||
company.getModifiedTime()
|
||||
);
|
||||
}
|
||||
@Override
|
||||
protected Function<Company, DetailItem> detailItemMapper() {
|
||||
return company -> new DetailItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers(),
|
||||
company.getCreatedTime(),
|
||||
company.getModifiedTime()
|
||||
);
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class SaveItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
}
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class SaveItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class ListItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
}
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class ListItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class DetailItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
private LocalDateTime createdTime;
|
||||
private LocalDateTime modifiedTime;
|
||||
}
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class DetailItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
private LocalDateTime createdTime;
|
||||
private LocalDateTime modifiedTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,85 +16,85 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
@RequestMapping("employee")
|
||||
public class EmployeeController extends SimpleControllerSupport<Employee, EmployeeController.SaveItem, EmployeeController.ListItem, EmployeeController.DetailItem> {
|
||||
private final CompanyService companyService;
|
||||
private final CompanyService companyService;
|
||||
|
||||
public EmployeeController(EmployeeService service, CompanyService companyService) {
|
||||
super(service);
|
||||
this.companyService = companyService;
|
||||
}
|
||||
public EmployeeController(EmployeeService service, CompanyService companyService) {
|
||||
super(service);
|
||||
this.companyService = companyService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Employee> saveItemMapper() {
|
||||
return item -> {
|
||||
var employee = new Employee();
|
||||
employee.setId(item.getId());
|
||||
employee.setName(item.getName());
|
||||
employee.setAge(item.getAge());
|
||||
employee.setRole(Employee.Role.USER);
|
||||
employee.setCompany(companyService.detailOrThrow(item.getCompanyId()));
|
||||
return employee;
|
||||
};
|
||||
}
|
||||
@Override
|
||||
protected Function<SaveItem, Employee> saveItemMapper() {
|
||||
return item -> {
|
||||
var employee = new Employee();
|
||||
employee.setId(item.getId());
|
||||
employee.setName(item.getName());
|
||||
employee.setAge(item.getAge());
|
||||
employee.setRole(Employee.Role.USER);
|
||||
employee.setCompany(companyService.detailOrThrow(item.getCompanyId()));
|
||||
return employee;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, ListItem> listItemMapper() {
|
||||
return employee -> new ListItem(
|
||||
employee.getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole()
|
||||
);
|
||||
}
|
||||
@Override
|
||||
protected Function<Employee, ListItem> listItemMapper() {
|
||||
return employee -> new ListItem(
|
||||
employee.getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Employee, DetailItem> detailItemMapper() {
|
||||
return employee -> new DetailItem(
|
||||
employee.getId(),
|
||||
employee.getCompany().getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole(),
|
||||
employee.getCreatedTime(),
|
||||
employee.getModifiedTime()
|
||||
);
|
||||
}
|
||||
@Override
|
||||
protected Function<Employee, DetailItem> detailItemMapper() {
|
||||
return employee -> new DetailItem(
|
||||
employee.getId(),
|
||||
employee.getCompany().getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole(),
|
||||
employee.getCreatedTime(),
|
||||
employee.getModifiedTime()
|
||||
);
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class SaveItem {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class SaveItem {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class ListItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Employee.Role role;
|
||||
}
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class ListItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Employee.Role role;
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class DetailItem {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Employee.Role role;
|
||||
private LocalDateTime createdTime;
|
||||
private LocalDateTime modifiedTime;
|
||||
}
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class DetailItem {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Employee.Role role;
|
||||
private LocalDateTime createdTime;
|
||||
private LocalDateTime modifiedTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,14 +22,14 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Comment("企业")
|
||||
public class Company extends SimpleEntity {
|
||||
@Column(nullable = false)
|
||||
@Comment("名称")
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
@Comment("成员数")
|
||||
private Integer members;
|
||||
@Column(nullable = false)
|
||||
@Comment("名称")
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
@Comment("成员数")
|
||||
private Integer members;
|
||||
|
||||
@OneToMany(mappedBy = "company")
|
||||
@ToString.Exclude
|
||||
private Set<Employee> employees;
|
||||
@OneToMany(mappedBy = "company")
|
||||
@ToString.Exclude
|
||||
private Set<Employee> employees;
|
||||
}
|
||||
|
||||
@@ -28,28 +28,28 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
@DynamicUpdate
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@NamedEntityGraph(name = "employee.detail", attributeNodes = {
|
||||
@NamedAttributeNode("company")
|
||||
@NamedAttributeNode("company")
|
||||
})
|
||||
@Comment("员工")
|
||||
public class Employee extends SimpleEntity {
|
||||
@Column(nullable = false)
|
||||
@Comment("名称")
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
@Comment("年龄")
|
||||
private Integer age;
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Comment("角色")
|
||||
private Role role;
|
||||
@Column(nullable = false)
|
||||
@Comment("名称")
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
@Comment("年龄")
|
||||
private Integer age;
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Comment("角色")
|
||||
private Role role;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@ToString.Exclude
|
||||
private Company company;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@ToString.Exclude
|
||||
private Company company;
|
||||
|
||||
public enum Role {
|
||||
USER,
|
||||
ADMIN,
|
||||
}
|
||||
public enum Role {
|
||||
USER,
|
||||
ADMIN,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.stereotype.Repository;
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Repository
|
||||
public interface EmployeeRepository extends SimpleRepository<Employee, Long> {
|
||||
@EntityGraph(value = "employee.detail", type = EntityGraph.EntityGraphType.FETCH)
|
||||
@Override
|
||||
Optional<Employee> findOne(Specification<Employee> specification);
|
||||
@EntityGraph(value = "employee.detail", type = EntityGraph.EntityGraphType.FETCH)
|
||||
@Override
|
||||
Optional<Employee> findOne(Specification<Employee> specification);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CompanyService extends SimpleServiceSupport<Company> {
|
||||
public CompanyService(CompanyRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
public CompanyService(CompanyRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user