Home > Back-end >  configuration of query-dsl-sql without hardcoding db configuration in pom.xml file?
configuration of query-dsl-sql without hardcoding db configuration in pom.xml file?

Time:10-19

working on a spring boot project with query-dsl-sql library. but the db configuration for this library is getting added in pom.xml as per documentation. is there any safer way where i can configure this db configuration from appplication.properties file or any other @configuration class. can't use query-dsl-jpa as we want to use sql window functions. It is a production project so cant use this configuration in pom file. Is there any way to solve this issue?

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lion</groupId>
    <artifactId>querydsl-bookstore-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-data-jpa-querydsl</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql-codegen</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-maven-plugin</artifactId>
                <version>5.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>export</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- <jdbcDriver>org.postgresql.Driver</jdbcDriver>
                    <jdbcUrl>jdbc:postgresql://127.0.0.1:5432/test</jdbcUrl> -->
                    <packageName>com.lion.bookstore</packageName>
                    <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

After searching web found a solution. in which we can configure to generate metamodels with below config

import java.io.File;
import java.sql.SQLException;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.querydsl.sql.codegen.MetaDataExporter;

@Configuration
public class QuerydslMetamodelConfig {

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void config() throws SQLException {
        try (java.sql.Connection connection = dataSource.getConnection();) {
            MetaDataExporter exporter = new MetaDataExporter();
            exporter.setPackageName("com.lion.querydsl.bookstore");
            exporter.setTargetFolder(new File("target/generated-sources/java"));
            exporter.setSchemaPattern("bookstore_schema");
            exporter.export(connection.getMetaData());
        }
    }

}

now no need to configure plugin with database details in pom.xml just adding two querydsl dependencies is enough

 <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql-codegen</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
  • Related