feat: 引入lombok、querydsl
This commit is contained in:
63
pom.xml
63
pom.xml
@@ -32,14 +32,6 @@
|
||||
<artifactId>fenix-spring-boot-starter</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-apt</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
<scope>provided</scope>
|
||||
<classifier>jakarta</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
@@ -47,6 +39,11 @@
|
||||
<classifier>jakarta</classifier>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@@ -90,33 +87,33 @@
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.mysema.maven</groupId>
|
||||
<artifactId>apt-maven-plugin</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile-dsl</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.14.0</version>
|
||||
<configuration>
|
||||
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
<outputDirectory>target/generated-sources/java</outputDirectory>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.36</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-apt</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
<classifier>jakarta</classifier>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>jakarta.persistence</groupId>
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
<compilerArgs>
|
||||
<arg>-Aquerydsl.entityAccessors=true</arg>
|
||||
<arg>-Aquerydsl.createDefaultVariable=true</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile-dsl</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-process</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
<outputDirectory>target/generated-test-sources/java</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -2,45 +2,21 @@ package com.lanyuanxiaoyao.service.template.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
public class Query {
|
||||
private Queryable query;
|
||||
private List<Sortable> sort;
|
||||
private Pageable page;
|
||||
|
||||
public Queryable getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(Queryable query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public List<Sortable> getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
public void setSort(List<Sortable> sort) {
|
||||
this.sort = sort;
|
||||
}
|
||||
|
||||
public Pageable getPage() {
|
||||
return page;
|
||||
}
|
||||
|
||||
public void setPage(Pageable page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Query{" +
|
||||
"query=" + query +
|
||||
", sort=" + sort +
|
||||
", page=" + page +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
public static class Queryable {
|
||||
private List<String> nullEqual;
|
||||
private List<String> notNullEqual;
|
||||
@@ -59,246 +35,33 @@ public class Query {
|
||||
private Map<String, Between> between;
|
||||
private Map<String, Between> notBetween;
|
||||
|
||||
public List<String> getNullEqual() {
|
||||
return nullEqual;
|
||||
}
|
||||
|
||||
public void setNullEqual(List<String> nullEqual) {
|
||||
this.nullEqual = nullEqual;
|
||||
}
|
||||
|
||||
public List<String> getNotNullEqual() {
|
||||
return notNullEqual;
|
||||
}
|
||||
|
||||
public void setNotNullEqual(List<String> notNullEqual) {
|
||||
this.notNullEqual = notNullEqual;
|
||||
}
|
||||
|
||||
public List<String> getEmpty() {
|
||||
return empty;
|
||||
}
|
||||
|
||||
public void setEmpty(List<String> empty) {
|
||||
this.empty = empty;
|
||||
}
|
||||
|
||||
public List<String> getNotEmpty() {
|
||||
return notEmpty;
|
||||
}
|
||||
|
||||
public void setNotEmpty(List<String> notEmpty) {
|
||||
this.notEmpty = notEmpty;
|
||||
}
|
||||
|
||||
public Map<String, Object> getEqual() {
|
||||
return equal;
|
||||
}
|
||||
|
||||
public void setEqual(Map<String, Object> equal) {
|
||||
this.equal = equal;
|
||||
}
|
||||
|
||||
public Map<String, Object> getNotEqual() {
|
||||
return notEqual;
|
||||
}
|
||||
|
||||
public void setNotEqual(Map<String, Object> notEqual) {
|
||||
this.notEqual = notEqual;
|
||||
}
|
||||
|
||||
public Map<String, String> getLike() {
|
||||
return like;
|
||||
}
|
||||
|
||||
public void setLike(Map<String, String> like) {
|
||||
this.like = like;
|
||||
}
|
||||
|
||||
public Map<String, String> getNotLike() {
|
||||
return notLike;
|
||||
}
|
||||
|
||||
public void setNotLike(Map<String, String> notLike) {
|
||||
this.notLike = notLike;
|
||||
}
|
||||
|
||||
public Map<String, Object> getGreat() {
|
||||
return great;
|
||||
}
|
||||
|
||||
public void setGreat(Map<String, Object> great) {
|
||||
this.great = great;
|
||||
}
|
||||
|
||||
public Map<String, Object> getLess() {
|
||||
return less;
|
||||
}
|
||||
|
||||
public void setLess(Map<String, Object> less) {
|
||||
this.less = less;
|
||||
}
|
||||
|
||||
public Map<String, Object> getGreatEqual() {
|
||||
return greatEqual;
|
||||
}
|
||||
|
||||
public void setGreatEqual(Map<String, Object> greatEqual) {
|
||||
this.greatEqual = greatEqual;
|
||||
}
|
||||
|
||||
public Map<String, Object> getLessEqual() {
|
||||
return lessEqual;
|
||||
}
|
||||
|
||||
public void setLessEqual(Map<String, Object> lessEqual) {
|
||||
this.lessEqual = lessEqual;
|
||||
}
|
||||
|
||||
public Map<String, List<Object>> getIn() {
|
||||
return in;
|
||||
}
|
||||
|
||||
public void setIn(Map<String, List<Object>> in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
public Map<String, List<Object>> getNotIn() {
|
||||
return notIn;
|
||||
}
|
||||
|
||||
public void setNotIn(Map<String, List<Object>> notIn) {
|
||||
this.notIn = notIn;
|
||||
}
|
||||
|
||||
public Map<String, Between> getBetween() {
|
||||
return between;
|
||||
}
|
||||
|
||||
public void setBetween(Map<String, Between> between) {
|
||||
this.between = between;
|
||||
}
|
||||
|
||||
public Map<String, Between> getNotBetween() {
|
||||
return notBetween;
|
||||
}
|
||||
|
||||
public void setNotBetween(Map<String, Between> notBetween) {
|
||||
this.notBetween = notBetween;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Queryable{" +
|
||||
"nullEqual=" + nullEqual +
|
||||
", notNullEqual=" + notNullEqual +
|
||||
", empty=" + empty +
|
||||
", notEmpty=" + notEmpty +
|
||||
", equal=" + equal +
|
||||
", notEqual=" + notEqual +
|
||||
", like=" + like +
|
||||
", notLike=" + notLike +
|
||||
", great=" + great +
|
||||
", less=" + less +
|
||||
", greatEqual=" + greatEqual +
|
||||
", lessEqual=" + lessEqual +
|
||||
", in=" + in +
|
||||
", notIn=" + notIn +
|
||||
", between=" + between +
|
||||
", notBetween=" + notBetween +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
public static class Between {
|
||||
private Object start;
|
||||
private Object end;
|
||||
|
||||
public Object getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(Object start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public Object getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public void setEnd(Object end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Between{" +
|
||||
"start='" + start + '\'' +
|
||||
", end='" + end + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
public static class Sortable {
|
||||
private String column;
|
||||
private Direction direction;
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public Direction getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public void setDirection(Direction direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Sortable{" +
|
||||
"column='" + column + '\'' +
|
||||
", direction=" + direction +
|
||||
'}';
|
||||
}
|
||||
|
||||
public enum Direction {
|
||||
ASC,
|
||||
DESC,
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
public static class Pageable {
|
||||
private Integer index;
|
||||
private Integer size;
|
||||
|
||||
public Integer getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public void setIndex(Integer index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public Integer getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Integer size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pageable{" +
|
||||
"index=" + index +
|
||||
", size=" + size +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
@Slf4j
|
||||
public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> implements SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
|
||||
protected final SimpleServiceSupport<ENTITY> service;
|
||||
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package com.lanyuanxiaoyao.service.template.controller.response;
|
||||
|
||||
import java.util.Map;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
public class GlobalResponse<T> {
|
||||
private static final int SUCCESS_STATUS = 0;
|
||||
private static final int ERROR_STATUS = 500;
|
||||
@@ -96,37 +102,4 @@ public class GlobalResponse<T> {
|
||||
response.setDetail(detail);
|
||||
return response;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AmisResponse{" +
|
||||
"status=" + status +
|
||||
", message='" + message + '\'' +
|
||||
", data=" + data +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,15 @@ import com.blinkfox.fenix.id.SnowflakeId;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@MappedSuperclass
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
public class IdOnlyEntity {
|
||||
@@ -14,19 +20,4 @@ public class IdOnlyEntity {
|
||||
@SnowflakeId
|
||||
@Comment("记录唯一标记")
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IdOnlyEntity{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,17 @@ package com.lanyuanxiaoyao.service.template.entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString(callSuper = true)
|
||||
@MappedSuperclass
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
public class SimpleEntity extends IdOnlyEntity {
|
||||
@@ -17,28 +23,4 @@ public class SimpleEntity extends IdOnlyEntity {
|
||||
@LastModifiedDate
|
||||
@Comment("记录更新时间")
|
||||
private LocalDateTime modifiedTime;
|
||||
|
||||
public LocalDateTime getCreatedTime() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public void setCreatedTime(LocalDateTime createdTime) {
|
||||
this.createdTime = createdTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getModifiedTime() {
|
||||
return modifiedTime;
|
||||
}
|
||||
|
||||
public void setModifiedTime(LocalDateTime modifiedTime) {
|
||||
this.modifiedTime = modifiedTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleEntity{" +
|
||||
"createdTime=" + createdTime +
|
||||
", modifiedTime=" + modifiedTime +
|
||||
"} " + super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,13 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
@Slf4j
|
||||
public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implements SimpleService<ENTITY> {
|
||||
private static final Logger log = LoggerFactory.getLogger(SimpleServiceSupport.class);
|
||||
private static final Integer DEFAULT_PAGE_INDEX = 1;
|
||||
private static final Integer DEFAULT_PAGE_SIZE = 10;
|
||||
protected final SimpleRepository<ENTITY, Long> repository;
|
||||
@@ -35,13 +33,7 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@Override
|
||||
public Long save(ENTITY entity) {
|
||||
if (ObjectHelper.isNotNull(entity.getId())) {
|
||||
var id = entity.getId();
|
||||
var targetEntity = repository.findById(entity.getId()).orElseThrow(() -> new IdNotFoundException(id));
|
||||
BeanUtils.copyProperties(entity, targetEntity, "id", "createdTime", "modifiedTime");
|
||||
entity = targetEntity;
|
||||
}
|
||||
entity = repository.save(entity);
|
||||
entity = repository.saveOrUpdateByNotNullProperties(entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,19 @@ public class TestApplication {
|
||||
|
||||
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 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错误");
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,13 +3,14 @@ package com.lanyuanxiaoyao.service.template.controller;
|
||||
import com.lanyuanxiaoyao.service.template.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.service.CompanyService;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250814
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("company")
|
||||
public class CompanyController extends SimpleControllerSupport<Company, CompanyController.SaveItem, CompanyController.ListItem, CompanyController.DetailItem> {
|
||||
@@ -30,162 +31,56 @@ public class CompanyController extends SimpleControllerSupport<Company, CompanyC
|
||||
|
||||
@Override
|
||||
protected ListItemMapper<Company, ListItem> listItemMapper() {
|
||||
return company -> {
|
||||
var item = new ListItem();
|
||||
item.setId(company.getId());
|
||||
item.setName(company.getName());
|
||||
item.setMembers(company.getMembers());
|
||||
return item;
|
||||
};
|
||||
return company -> new ListItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DetailItemMapper<Company, DetailItem> detailItemMapper() {
|
||||
return company -> {
|
||||
var item = new DetailItem();
|
||||
item.setId(company.getId());
|
||||
item.setName(company.getName());
|
||||
item.setMembers(company.getMembers());
|
||||
item.setCreatedTime(company.getCreatedTime());
|
||||
item.setModifiedTime(company.getModifiedTime());
|
||||
return item;
|
||||
};
|
||||
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;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(Integer members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SaveItem{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", members=" + members +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class ListItem {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer members;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(Integer members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ListItem{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", members=" + 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;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(Integer members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedTime() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public void setCreatedTime(LocalDateTime createdTime) {
|
||||
this.createdTime = createdTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getModifiedTime() {
|
||||
return modifiedTime;
|
||||
}
|
||||
|
||||
public void setModifiedTime(LocalDateTime modifiedTime) {
|
||||
this.modifiedTime = modifiedTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DetailItem{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", members=" + members +
|
||||
", createdTime=" + createdTime +
|
||||
", modifiedTime=" + modifiedTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.lanyuanxiaoyao.service.template.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.service.CompanyService;
|
||||
import com.lanyuanxiaoyao.service.template.service.EmployeeService;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
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;
|
||||
|
||||
public EmployeeController(EmployeeService service, CompanyService companyService) {
|
||||
super(service);
|
||||
this.companyService = companyService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SaveItemMapper<Employee, SaveItem> 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 ListItemMapper<Employee, ListItem> listItemMapper() {
|
||||
return employee -> new ListItem(
|
||||
employee.getId(),
|
||||
employee.getName(),
|
||||
employee.getAge(),
|
||||
employee.getRole()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DetailItemMapper<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 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;
|
||||
}
|
||||
}
|
||||
@@ -1,42 +1,33 @@
|
||||
package com.lanyuanxiaoyao.service.template.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
@Entity
|
||||
@DynamicUpdate
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Comment("企业")
|
||||
public class Company extends SimpleEntity {
|
||||
@Column(nullable = false)
|
||||
@Comment("名称")
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
@Comment("成员数")
|
||||
private Integer members;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(Integer members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Company{" +
|
||||
"name='" + name + '\'' +
|
||||
", members=" + members +
|
||||
"} " + super.toString();
|
||||
}
|
||||
@OneToMany(mappedBy = "company")
|
||||
@ToString.Exclude
|
||||
private Set<Employee> employees;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.lanyuanxiaoyao.service.template.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ConstraintMode;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.ForeignKey;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.NamedAttributeNode;
|
||||
import jakarta.persistence.NamedEntityGraph;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
@Entity
|
||||
@DynamicUpdate
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@NamedEntityGraph(name = "employee.detail", attributeNodes = {
|
||||
@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;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@ToString.Exclude
|
||||
private Company company;
|
||||
|
||||
public enum Role {
|
||||
USER,
|
||||
ADMIN,
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,6 @@ package com.lanyuanxiaoyao.service.template.repository;
|
||||
import com.lanyuanxiaoyao.service.template.entity.Company;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250814
|
||||
*/
|
||||
@Repository
|
||||
public interface CompanyRepository extends SimpleRepository<Company, Long> {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.lanyuanxiaoyao.service.template.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.entity.Employee;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
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);
|
||||
}
|
||||
@@ -4,17 +4,9 @@ import com.lanyuanxiaoyao.service.template.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.repository.CompanyRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250814
|
||||
*/
|
||||
@Service
|
||||
public class CompanyService extends SimpleServiceSupport<Company> {
|
||||
public CompanyService(CompanyRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
|
||||
public void test() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.lanyuanxiaoyao.service.template.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.repository.EmployeeRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
public EmployeeService(EmployeeRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user