我是 Java 和 Spring 的新手,我正在做我的第一個休息服務。我遇到了一個我找不到答案的問題。
我使用 Jooq 和 pojos 和 daos 生成物體
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.16.6</version>
<executions>
...
</executions>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/spring</url>
<user>postgres</user>
<password>${env.PGPASSWORD}</password>
</jdbc>
<generator>
<name>org.jooq.codegen.JavaGenerator</name>
<database>
...
</database>
<target>
<packageName>com.example.otrtesttask.jooq</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
<generate>
<pojos>true</pojos>
<daos>true</daos>
<records>true</records>
</generate>
</generator>
</configuration>
</plugin>
我的資料庫結構:
CREATE TABLE branch(id SERIAL PRIMARY KEY, title VARCHAR(100));
CREATE TABLE position(id SERIAL PRIMARY KEY, title VARCHAR(30));
CREATE TABLE employee
(
id SERIAL PRIMARY KEY,
manager_id INTEGER REFERENCES employee (id),
position_id INTEGER REFERENCES position (id) NOT NULL,
full_name VARCHAR(50) NOT NULL,
branch_id INTEGER REFERENCES branch (id) NOT NULL);
CREATE TABLE task
(
id SERIAL PRIMARY KEY,
priority SMALLINT NOT NULL,
description VARCHAR(200) NOT NULL,
employee_id INT REFERENCES employee (id) NOT NULL);
然后我制作了呼叫服務的控制器,該服務呼叫存盤庫來呼叫 CRUD 操作。它作業得很好,但我想得到職位標題和id等。所以我做了一個DTO:
@Data
public class EmployeeDto {
private Integer id;
private Integer managerId;
private Integer positionId;
private String fullName;
private Integer branchId;
private Employee manager;
private Position position;
private Branch branch;
}
之后,我制作了一個將 Employee 轉換為 EmplyeeDto 的映射器。有人告訴我,獲取嵌套資料(如職位標題)的最佳方法是使用 DAO fetchOnyById 函式。
@Service
public class MappingUtils {
EmployeeDao employeeDao = new EmployeeDao();
PositionDao positionDao = new PositionDao();
BranchDao branchDao = new BranchDao();
public EmployeeDto mapToEmployeeDto(Employee employee) {
EmployeeDto dto = new EmployeeDto();
dto.setId(employee.getId());
dto.setBranchId(employee.getBranchId());
dto.setPositionId(employee.getPositionId());
dto.setFullName(employee.getFullName());
dto.setManagerId(employee.getManagerId());
dto.setManager(employeeDao.fetchOneById(employee.getManagerId()));
dto.setPosition(positionDao.fetchOneById(employee.getPositionId()));
dto.setBranch(branchDao.fetchOneById(employee.getBranchId()));
return dto;
}
我像這樣在存盤庫中映射物體:
@Repository
@RequiredArgsConstructor
public class EmployeeRepository {
@Autowired
private final DSLContext dsl;
private final MappingUtils mappingUtils;
public List<EmployeeDto> findAll(Condition condition) {
return dsl.selectFrom(Tables.EMPLOYEE)
.where(condition)
.fetchInto(Employee.class)
.stream().map(mappingUtils::mapToEmployeeDto)
.collect(Collectors.toList());
}
}
它作業正常,直到它到達 dao fetch 函式。它拋出一個例外org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured。一旦我洗掉了 dao 函式,get 查詢就會回傳很好的回應,并將 manager、position 和 branch 設定為 null。
我在這里做錯了什么?以及如何提供必要的連接?
==UPD== 我的 application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
spring.datasource.password=${PGPASSWORD}
spring.liquibase.change-log=classpath:liquibase/changelog.sql
uj5u.com熱心網友回復:
問題是您的 DAO 類沒有附加到 jOOQ Configuration。您剛剛像這樣創建它們:
EmployeeDao employeeDao = new EmployeeDao();
但是你必須像這樣創建它們:
EmployeeDao employeeDao = new EmployeeDao(configuration);
您還可以配置代碼生成器以生成 Spring 注釋,然后將 DAO 注入您的類。配置將是:
<configuration>
<generator>
<generate>
<springAnnotations>true</springAnnotations>
<!-- ... -->
進而:
@Service
public class MappingUtils {
@Autowired
EmployeeDao employeeDao;
@Autowired
PositionDao positionDao;
@Autowired
BranchDao branchDao;
// ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/475597.html
上一篇:如何使用JPA連接兩個表?
