feat: 增加本地查询示例
This commit is contained in:
@@ -231,6 +231,24 @@ public class TestApplication {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runNativeQueryTests() 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 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 list = employeeRepository.findAllEmployeeWithCompanyName();
|
||||
Assert.isTrue(list.size() == 3, "数量错误");
|
||||
log.debug("Results: {}", list);
|
||||
var list_native = employeeRepository.findAllEmployeeWithCompanyNameNative();
|
||||
Assert.isTrue(list_native.size() == 3, "数量错误");
|
||||
log.debug("Results: {}", list_native);
|
||||
}
|
||||
|
||||
private HttpHeaders headers() {
|
||||
var headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
@@ -12,8 +12,6 @@ import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MapKeyEnumerated;
|
||||
import jakarta.persistence.NamedAttributeNode;
|
||||
import jakarta.persistence.NamedEntityGraph;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.Getter;
|
||||
@@ -35,9 +33,6 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
@DynamicUpdate
|
||||
@DynamicInsert
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@NamedEntityGraph(name = "employee.detail", attributeNodes = {
|
||||
@NamedAttributeNode("company")
|
||||
})
|
||||
@Comment("员工")
|
||||
public class Employee extends SimpleEntity {
|
||||
@Column(nullable = false)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.lanyuanxiaoyao.service.template.entity.vo;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.entity.Employee;
|
||||
|
||||
public record EmployeeWithCompanyName(
|
||||
String name,
|
||||
String companyName,
|
||||
Integer age,
|
||||
Employee.Role role
|
||||
) {
|
||||
}
|
||||
@@ -1,15 +1,25 @@
|
||||
package com.lanyuanxiaoyao.service.template.repository;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.entity.vo.EmployeeWithCompanyName;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Repository
|
||||
public interface EmployeeRepository extends SimpleRepository<Employee> {
|
||||
@EntityGraph(value = "employee.detail", type = EntityGraph.EntityGraphType.FETCH)
|
||||
@EntityGraph(attributePaths = {"company"})
|
||||
@Override
|
||||
Optional<Employee> findOne(Specification<Employee> specification);
|
||||
|
||||
@Query(value = "select e.name, c.name, e.age, e.role from employee e, company c where e.company_id = c.id", nativeQuery = true)
|
||||
List<Map<String, Object>> findAllEmployeeWithCompanyNameNative();
|
||||
|
||||
@Query("select new com.lanyuanxiaoyao.service.template.entity.vo.EmployeeWithCompanyName(employee.name, employee.company.name, employee.age, employee.role) from Employee employee")
|
||||
List<EmployeeWithCompanyName> findAllEmployeeWithCompanyName();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user