doc(jpa): 补充部份注释说明,优化日志输出
This commit is contained in:
@@ -19,6 +19,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.Session;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
@@ -39,6 +40,7 @@ public class TestApplication extends AbstractTestApplication {
|
||||
private final CompanyRepository companyRepository;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final ReportRepository reportRepository;
|
||||
private final Session session;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
@@ -93,7 +95,7 @@ public class TestApplication extends AbstractTestApplication {
|
||||
var skill3 = Skill.builder().name("Spring").description("Spring 框架").build();
|
||||
var skill4 = Skill.builder().name("MySQL").description("MySQL 数据库").build();
|
||||
|
||||
var employee1 = employeeRepository.save(Employee.builder()
|
||||
employeeRepository.save(Employee.builder()
|
||||
.name("Alice").age(30).role(Employee.Role.ADMIN).code("E001")
|
||||
.salary(new BigDecimal("50000.00")).bonus(new BigDecimal("5000.00"))
|
||||
.active(true).company(company1)
|
||||
@@ -104,7 +106,7 @@ public class TestApplication extends AbstractTestApplication {
|
||||
.connections(Map.of(Employee.ConnectionType.EMAIL, "alice@example.com"))
|
||||
.build());
|
||||
|
||||
var employee2 = employeeRepository.save(Employee.builder()
|
||||
employeeRepository.save(Employee.builder()
|
||||
.name("Bob").age(25).role(Employee.Role.USER).code("E002")
|
||||
.salary(new BigDecimal("40000.00")).bonus(new BigDecimal("4000.00"))
|
||||
.active(true).company(company2)
|
||||
@@ -115,7 +117,7 @@ public class TestApplication extends AbstractTestApplication {
|
||||
.connections(Map.of(Employee.ConnectionType.PHONE, "1234567890"))
|
||||
.build());
|
||||
|
||||
var employee3 = employeeRepository.save(Employee.builder()
|
||||
employeeRepository.save(Employee.builder()
|
||||
.name("Charlie").age(35).role(Employee.Role.ADMIN).code("E003")
|
||||
.salary(new BigDecimal("60000.00")).bonus(new BigDecimal("6000.00"))
|
||||
.active(false).company(company1)
|
||||
@@ -126,7 +128,7 @@ public class TestApplication extends AbstractTestApplication {
|
||||
.connections(Map.of(Employee.ConnectionType.EMAIL, "charlie@example.com", Employee.ConnectionType.PHONE, "0987654321"))
|
||||
.build());
|
||||
|
||||
var employee4 = employeeRepository.save(Employee.builder()
|
||||
employeeRepository.save(Employee.builder()
|
||||
.name("David").age(28).role(Employee.Role.USER).code("E004")
|
||||
.salary(new BigDecimal("45000.00")).bonus(null)
|
||||
.active(true).company(company3)
|
||||
@@ -137,7 +139,7 @@ public class TestApplication extends AbstractTestApplication {
|
||||
.connections(Map.of())
|
||||
.build());
|
||||
|
||||
var employee5 = employeeRepository.save(Employee.builder()
|
||||
employeeRepository.save(Employee.builder()
|
||||
.name("Alice Smith").age(32).role(Employee.Role.USER).code("E005")
|
||||
.salary(new BigDecimal("55000.00")).bonus(new BigDecimal("5500.00"))
|
||||
.active(true).company(company2)
|
||||
@@ -148,7 +150,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
.connections(Map.of(Employee.ConnectionType.EMAIL, "alicesmith@example.com"))
|
||||
.build());
|
||||
|
||||
formatLog("1. 基本比较操作符 - equal, notEqual, greaterThan, lessThan, greaterThanOrEqualTo, lessThanOrEqualTo");
|
||||
formatLog("1. 基本比较操作符查询 JPA");
|
||||
// 查找姓名为"Bob"、角色不是ADMIN、年龄在20-30之间、薪资在40000-45000之间的员工
|
||||
var result1 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.equal(root.get(Employee_.name), "Bob"),
|
||||
cb.notEqual(root.get(Employee_.role), Employee.Role.ADMIN),
|
||||
@@ -159,7 +162,20 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result1.size() == 1, "基本比较操作符查询失败 (%d)".formatted(result1.size()));
|
||||
|
||||
formatLog("2. 区间和集合操作符 - between, in, notIn");
|
||||
formatLog("1. 基本比较操作符查询 Fenix");
|
||||
var result1_fenix = employeeRepository.findAll(
|
||||
builder -> builder.andEquals(Employee.Fields.name, "Bob")
|
||||
.andNotEquals(Employee.Fields.role, Employee.Role.ADMIN)
|
||||
.andGreaterThan(Employee.Fields.age, 20)
|
||||
.andLessThan(Employee.Fields.age, 30)
|
||||
.andGreaterThanEqual(Employee.Fields.salary, new BigDecimal("40000.00"))
|
||||
.andLessThanEqual(Employee.Fields.salary, new BigDecimal("45000.00"))
|
||||
.build()
|
||||
);
|
||||
Assert.isTrue(result1_fenix.size() == 1, "基本比较操作符查询失败 (%d)".formatted(result1.size()));
|
||||
|
||||
formatLog("2. 区间和集合操作符查询");
|
||||
// 查找年龄在25-30之间、年龄不在40-50之间、年龄在25/30/35中、角色是USER或ADMIN、姓名不在Charlie/David中的员工
|
||||
var result2 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.between(root.get(Employee_.age), 25, 30),
|
||||
cb.between(root.get(Employee_.age), 40, 50).not(),
|
||||
@@ -170,7 +186,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result2.size() == 2, "区间和集合操作符查询失败 (%d)".formatted(result2.size()));
|
||||
|
||||
formatLog("3. 字符串操作符 - like, notLike, lower, upper, length, substring");
|
||||
formatLog("3. 字符串操作符查询");
|
||||
// 查找以A开头、不以C开头、包含"ali"(忽略大小写)、名称长度在4-10之间、前3个字符为"Ali"的员工
|
||||
var result3 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.like(root.get(Employee_.name), "A%"),
|
||||
cb.notLike(root.get(Employee_.name), "C%"),
|
||||
@@ -182,7 +199,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result3.size() == 1, "字符串操作符查询失败 (%d)".formatted(result3.size()));
|
||||
|
||||
formatLog("4. NULL 和布尔操作符 - isNull, isNotNull, isTrue, isFalse");
|
||||
formatLog("4. NULL 和布尔操作符查询");
|
||||
// 查找激活状态为true、bonus不为null、code不在E999中的员工
|
||||
var result4 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.isTrue(root.get(Employee_.active)),
|
||||
cb.isFalse(cb.literal(false)),
|
||||
@@ -191,7 +209,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result4.isEmpty(), "NULL 和布尔操作符查询失败 (%d)".formatted(result4.size()));
|
||||
|
||||
formatLog("5. 集合操作符 - isEmpty, isNotEmpty, isMember, size");
|
||||
formatLog("5. 集合操作符查询");
|
||||
// 查找技能集合非空、爱好集合非空、包含"Reading"爱好、不包含"Riding"爱好、爱好数量大于1、技能数量小于4的员工
|
||||
var result5 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.isNotEmpty(root.get(Employee_.skills)),
|
||||
cb.isEmpty(root.get(Employee_.hobbies)).not(),
|
||||
@@ -202,7 +221,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result5.size() == 2, "集合操作符查询失败 (%d)".formatted(result5.size()));
|
||||
|
||||
formatLog("6. 逻辑操作符 - and, or, not");
|
||||
formatLog("6. 逻辑操作符查询");
|
||||
// 查找姓名为Alice或Bob、且姓名不为Charlie或David的员工
|
||||
var result6 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.or(
|
||||
cb.equal(root.get(Employee_.name), "Alice"),
|
||||
@@ -217,7 +237,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result6.size() == 2, "逻辑操作符查询失败 (%d)".formatted(result6.size()));
|
||||
|
||||
formatLog("7. Specification 链式调用 - where, and, or");
|
||||
formatLog("7. Specification 链式调用查询");
|
||||
// 链式组合:激活状态为true、年龄大于25、角色不是ADMIN、或姓名为Charlie、且姓名不为Alice Smith
|
||||
var result7 = employeeRepository.findAll(
|
||||
Specification.<Employee>where((root, query, cb) -> cb.isTrue(root.get(Employee_.active)))
|
||||
.and((root, query, cb) -> cb.greaterThan(root.get(Employee_.age), 25))
|
||||
@@ -227,7 +248,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
);
|
||||
Assert.isTrue(result7.size() == 2, "Specification 链式调用失败 (%d)".formatted(result7.size()));
|
||||
|
||||
formatLog("8. Join 操作 - join, fetch + 集合 Join + Map Join + 多条件组合");
|
||||
formatLog("8. Join 操作查询");
|
||||
// 查找公司名为TechCorp、技能包含Java、属性值为Senior的员工(使用join、fetch、集合join、map join)
|
||||
var result8 = employeeRepository.findAll((root, query, cb) -> {
|
||||
root.fetch(Employee_.company);
|
||||
query.distinct(true);
|
||||
@@ -245,7 +267,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
});
|
||||
Assert.isTrue(result8.size() == 1, "Join 操作查询失败 (%d)".formatted(result8.size()));
|
||||
|
||||
formatLog("9. 子查询 - 聚合函数 avg, sum, count + 数学运算 sum, coalesce + 多条件组合");
|
||||
formatLog("9. 子查询和聚合函数查询");
|
||||
// 查找薪资高于平均薪资、总记录数不为5、总薪酬在55000-70000之间、激活状态为true、姓名不为David、年龄大于28、角色不是USER的员工
|
||||
var result9 = employeeRepository.findAll((root, query, cb) -> {
|
||||
var avgSalarySubquery = query.subquery(Double.class);
|
||||
var avgSubRoot = avgSalarySubquery.from(Employee.class);
|
||||
@@ -272,7 +295,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
});
|
||||
Assert.isTrue(result9.isEmpty(), "子查询(聚合函数)+ 数学运算失败 (%d)".formatted(result9.size()));
|
||||
|
||||
formatLog("10. 排序 - Sort 单字段和多字段 + 多条件组合");
|
||||
formatLog("10. 排序查询");
|
||||
// 查找激活状态为true、角色不是ADMIN、年龄大于20、薪资小于60000的员工,按年龄降序、姓名升序排序
|
||||
var result10 = employeeRepository.findAll(
|
||||
(root, query, cb) -> cb.and(
|
||||
cb.isTrue(root.get(Employee_.active)),
|
||||
@@ -287,7 +311,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
);
|
||||
Assert.isTrue(result10.size() == 3 && result10.get(0).getAge() >= result10.get(1).getAge(), "排序查询失败 (%d)".formatted(result10.size()));
|
||||
|
||||
formatLog("11. 分页 - PageRequest + 多条件组合");
|
||||
formatLog("11. 分页查询");
|
||||
// 分页查找激活状态为true、角色不是ADMIN、年龄大于20、薪资小于60000的员工,每页2条,按年龄排序
|
||||
var page11 = employeeRepository.findAll(
|
||||
(root, query, cb) -> cb.and(
|
||||
cb.isTrue(root.get(Employee_.active)),
|
||||
@@ -300,7 +325,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
Assert.isTrue(page11.getContent().size() == 2, "分页大小不正确 (%d)".formatted(page11.getContent().size()));
|
||||
Assert.isTrue(page11.getTotalElements() == 3, "总元素数不正确 (%d)".formatted(page11.getTotalElements()));
|
||||
|
||||
formatLog("12. CASE WHEN 条件表达式 + 多条件组合");
|
||||
formatLog("12. CASE WHEN 条件表达式查询");
|
||||
// 查找年龄大于30(Senior)或年龄在25-30之间(Middle)的员工,排除Junior级别的员工
|
||||
var result12 = employeeRepository.findAll((root, query, cb) -> cb.and(
|
||||
cb.equal(
|
||||
cb.selectCase()
|
||||
@@ -319,7 +345,8 @@ public class TestApplication extends AbstractTestApplication {
|
||||
));
|
||||
Assert.isTrue(result12.size() == 2, "CASE WHEN 查询失败 (%d)".formatted(result12.size()));
|
||||
|
||||
formatLog("13. 综合多条件查询 - Join + Embedded + 集合 + Map + 日期时间 + 多条件组合");
|
||||
formatLog("13. 综合多条件查询");
|
||||
// 综合查询:公司名为TechCorp、技能包含Java、城市为Beijing、技能和爱好非空、属性非空、创建和修改时间不为null、激活状态为true、姓名不为Alice Smith、年龄大于25、角色不是USER
|
||||
var result13 = employeeRepository.findAll((root, query, cb) -> {
|
||||
query.distinct(true);
|
||||
return cb.and(
|
||||
|
||||
Reference in New Issue
Block a user