简体   繁体   English

spring boot jooq dao 麻烦

[英]Spring boot jooq dao trouble

I'm new to Java and Spring and I am doing my first rest service.我是 Java 和 Spring 的新手,我正在做我的第一个休息服务。 I've encountered a problem to which I cannot find an answer.我遇到了一个我找不到答案的问题。

I generated entities using Jooq with pojos and daos我使用 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>

My database structure:我的数据库结构:

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

Then I made Controller that calls Service that calls Repository to invoke CRUD operations.然后我制作了调用服务的控制器,该服务调用存储库来调用 CRUD 操作。 And it works perfectly fine, but I want to get position title along with id, etc. So I made a DTO:它工作得很好,但我想得到职位标题和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;
}

After that I made a mapper that converts Employee to EmplyeeDto.之后,我制作了一个将 Employee 转换为 EmplyeeDto 的映射器。 I've been told that the best way to get nested data (like position title) is to use DAO fetchOnyById function.有人告诉我,获取嵌套数据(如职位标题)的最佳方法是使用 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;
    }

And I map entities inside Repository like this:我像这样在存储库中映射实体:

   @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());
        }
    }

And it works fine until it gets to the dao fetch function.它工作正常,直到它到达 dao fetch 函数。 It throws an exception org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured它抛出异常org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured . org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured Once I remove the dao functions the get query returns nice response with manager, position and branch set to null.一旦我删除了 dao 函数,get 查询就会返回很好的响应,并将 manager、position 和 branch 设置为 null。

What am I doing wrong here?我在这里做错了什么? And how to provide necessary connection?以及如何提供必要的连接?

==UPD== My application.properties: ==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

The problem is that your DAO classes aren't attached to a jOOQ Configuration .问题是您的 DAO 类没有附加到 jOOQ Configuration You just created them like this:您刚刚像这样创建它们:

EmployeeDao employeeDao = new EmployeeDao();

But you have to create them like this:但是你必须像这样创建它们:

EmployeeDao employeeDao = new EmployeeDao(configuration);

You can also configure the code generator to generate Spring annotations and then inject the DAOs to your class.您还可以配置代码生成器以生成 Spring 注释,然后将 DAO 注入您的类。 The configuration would be:配置将是:

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

And then:接着:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM