gpt4 book ai didi

java - 如何使用 spring boot 和 jOOQ 对数据库进行逆向工程并生成代码?

转载 作者:行者123 更新时间:2023-11-29 06:13:02 25 4
gpt4 key购买 nike

简介

在阅读了一些博客并观看了一些关于 jOOQ 的演示后,我非常兴奋地尝试了逆向工程 api 并替换了我目前用来对数据库进行逆向工程的 hibernate 工具。

基于 Petri Kainulainen 的示例配置 http://www.petrikainulainen.net/programming/jooq/using-jooq-with-spring-configuration/以及来自 jOOQ 创始人的 spring boot 友好改编 https://github.com/jOOQ/jOOQ/tree/master/jOOQ-examples/jOOQ-spring-boot-example ,我尝试设置一个 spring-boot-starter-jdbc,目标是从 mysql 数据库(来自 MySQL 网站的 sakila 示例)生成 POJO。

当使用 mvn clean install 运行程序和/或仅将其作为 spring boot 应用程序运行时,不会发生代码生成。标准的 spring boot 应用程序运行并且没有显示错误和/或警告。

[2m2016-05-23 14:07:50.604[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[           main][0;39m [36mn.sander.mieras.application.Application [0;39m [2m:[0;39m Starting Application on MacBook-Pro-van-Sander.local with PID 628 (/Users/Sander/Development/Workspace/reverse-engineer-jooq/target/classes started by Sander in /Users/Sander/Development/Workspace/reverse-engineer-jooq)
[2m2016-05-23 14:07:50.606[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[ main][0;39m [36mn.sander.mieras.application.Application [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2016-05-23 14:07:50.641[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.c.a.AnnotationConfigApplicationContext[0;39m [2m:[0;39m Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Mon May 23 14:07:50 CEST 2016]; root of context hierarchy
[2m2016-05-23 14:07:51.451[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter [0;39m [2m:[0;39m Registering beans for JMX exposure on startup
[2m2016-05-23 14:07:51.460[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[ main][0;39m [36mn.sander.mieras.application.Application [0;39m [2m:[0;39m Started Application in 1.036 seconds (JVM running for 1.66)
[2m2016-05-23 14:07:51.461[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[ Thread-1][0;39m [36ms.c.a.AnnotationConfigApplicationContext[0;39m [2m:[0;39m Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Mon May 23 14:07:50 CEST 2016]; root of context hierarchy
[2m2016-05-23 14:07:51.463[0;39m [32m INFO[0;39m [35m628[0;39m [2m---[0;39m [2m[ Thread-1][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter [0;39m [2m:[0;39m Unregistering JMX-exposed beans on shutdown

给出

pom.xml 构建配置取自 jOOQ-spring-boot-example 并进行了一些调整(为了便于阅读,省略了一些标准依赖项)

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/application.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>reverse-engineer</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jdbc>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<user>${db.username}</user>
<password>${db.password}</password>
</jdbc>
<generator>
<database>
<name>${jooq.generator.db.dialect}</name>
<includes>.*</includes>
<excludes></excludes>
<dateAsTimestamp>true</dateAsTimestamp>
<inputSchema>SAKILA</inputSchema>
</database>
<generate>
<deprecated>false</deprecated>
<instanceFields>true</instanceFields>
<pojos>true</pojos>
<records>true</records>
</generate>
<target>
<packageName>nl.sander.mieras.domain</packageName>
<directory>src/main/java</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>

鉴于我的主要配置类如下所示(所有 bean 代码均取自 jOOQ-spring-boot-example):

@Configuration
// not sure if I need this setting since I initialize it in the pom build
@PropertySource("classpath:application.properties")
@Import({SpringTransactionProvider.class, ExceptionTranslator.class})
public class JooqSpringBootConfiguration {

@Autowired
private Environment env;

@Autowired
private SpringTransactionProvider txp;

@Autowired
private ExceptionTranslator ext;

@Bean(destroyMethod = "close")
public DataSource dataSource() throws IllegalStateException, PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(env.getRequiredProperty("db.driver"));
dataSource.setJdbcUrl(env.getRequiredProperty("db.url"));
dataSource.setUser(env.getRequiredProperty("db.username"));
dataSource.setPassword("db.password");

return dataSource;
}

@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) throws IllegalStateException, PropertyVetoException{
return new DataSourceTransactionManager(dataSource());
}

@Bean
public DSLContext dsl(org.jooq.Configuration config){
return new DefaultDSLContext(config);
}

@Bean
public ConnectionProvider connectionProvider(DataSource dataSource){
return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy(dataSource));
}

@Bean
public ExecuteListenerProvider executeListenerProvider(ExceptionTranslator exceptionTranslator){
return new DefaultExecuteListenerProvider(ext);
}

@Bean
public org.jooq.Configuration jooqConfig(ConnectionProvider connectionProvider, TransactionProvider transactionProvider, ExecuteListenerProvider executeListenerProvider){

return new DefaultConfiguration()
.derive(connectionProvider)
.derive(txp)
.derive(executeListenerProvider)
.derive(SQLDialect.MYSQL);
}

}

最后但同样重要的是 source/main/resources 目录中的 application.properties 文件:

#Database Configuration
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/SAKILA?useSSL=false
db.username=root
db.password=****

#jOOQ Configuration
jooq.generator.db.dialect=MYSQL

问题

鉴于上述配置,我错过/和/或错误配置了什么配置以启用代码生成(例如 POJO)?

最佳答案

根据 https://github.com/sivaprasadreddy/springboot-tutorials/tree/master/springboot-jooq-demo 中的示例找到了我的问题的解决方案

我只需要一个配置良好的 pom,不需要实际的 java 代码。

只需使用带有反向配置文件的全新安装,它就会生成我想要的代码。

pom(配置文件中 id 为“reverse”的插件):

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/sakila</url>
<user>root</user>
<password>****</password>
</jdbc>
<generator>
<name>org.jooq.util.DefaultGenerator</name>
<database>
<name>org.jooq.util.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes />
<inputSchema>sakila</inputSchema>
</database>
<generate>
<deprecated>false</deprecated>
<jpaAnnotations>false</jpaAnnotations>
<validationAnnotations>true</validationAnnotations>
<instanceFields>false</instanceFields>
<records>false</records>
<immutablePojos>false</immutablePojos>
<relations>true</relations>
<globalObjectReferences>false</globalObjectReferences>
<pojos>true</pojos>
</generate>
<target>
<packageName>nl.sander.mieras.domain</packageName>
<directory>src/main/java</directory>
</target>
</generator>
</configuration>
</plugin>

和应用程序:

package nl.sander.mieras.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}

关于java - 如何使用 spring boot 和 jOOQ 对数据库进行逆向工程并生成代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37391498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com