Spring 启动 jooq dao 麻烦

Spring boot jooq dao trouble

我是 Java 和 Spring 的新手,我正在做我的第一次休息服务。我遇到了一个找不到答案的问题。

我使用带有 pojos 和 daos 的 Jooq 生成了实体

            <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);

然后我做了Controller调用Service调用Repository调用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 中映射实体:

   @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 获取函数之前,它工作正常。它抛出异常 org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured。 一旦我删除了 dao 函数,get 查询 returns 很好的响应,经理、职位和分支设置为空。

我在这里做错了什么?以及如何提供必要的连接?

==更新== 我的 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

问题是您的 DAO classes 没有附加到 jOOQ Configuration。您刚刚像这样创建了它们:

EmployeeDao employeeDao = new EmployeeDao();

但是你必须这样创建它们:

EmployeeDao employeeDao = new EmployeeDao(configuration);

您也可以 configure the code generator to generate Spring annotations 然后将 DAO 注入您的 class。配置为:

<configuration>
  <generator>
    <generate>
      <springAnnotations>true</springAnnotations>
      <!-- ... -->

然后:

@Service
public class MappingUtils {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    PositionDao positionDao;
    @Autowired
    BranchDao branchDao;
    // ...