사용자 도구

사이트 도구


gradle:querydsl_native_sql_codegen

Gradle 에서 Native SQL Code Generation

ANT 사용

dependencies {
    compile "com.querydsl:querydsl-sql:${queryDslVersion}"
}
 
configurations {
    queryDslNativeSql
}
 
dependencies {
    queryDslNativeSql "com.querydsl:querydsl-sql-codegen:4.1.4"
    queryDslNativeSql "com.microsoft.sqlserver:mssql-jdbc"
}
 
task generateQueryDslNative << {
    file('src/main/generated-querydslnative').deleteDir()
    file('src/main/generated-querydslnative').mkdirs()
 
    ant.taskdef(name: 'generateQueryDslNative', classname: 'com.querydsl.sql.codegen.ant.AntMetaDataExporter', classpath: configurations.queryDslNativeSql.asPath)
    ant.generateQueryDslNative(
            jdbcDriver: '-JDBC-Driver-',
            jdbcUrl: '-JDBC-URL-',
            jdbcUser: 'username',
            jdbcPassword: "password",
            namePrefix: 'QS',
            packageName: 'test',
            schemaPattern: 'dbo', // SQLServer 등에서 필요
            tableNamePattern: 'table1|table2', // 특정 테이블만 생성
            targetFolder: 'src/main/generated-querydslnative'
    )
}
 
sourceSets {
    main {
        java {
             srcDirs += 'src/main/generated-querydslnative'
 
        }
    }
}
 
idea {
    module {
         generatedSourceDirs += file('src/main/generated-querydslnative') // just hint
    }
}

코드 직접 호출 : 비권장

먼저 Create Oracle database JDBC connection in gradle 를 통해 JDBC Driver를 build script 에서 로딩해야한다.

import java.sql.Connection
import java.sql.DriverManager
import com.mysema.query.sql.codegen.MetaDataExporter
 
buildscript {
    dependencies {
        classpath 'com.mysema.querydsl:querydsl-sql-codegen:{version}'
    }
}
 
configurations {
    querydslSqlJdbcDriver
}
 
dependencies {
    querydslSqlJdbcDriver "mysql:mysql-connector-java:${mysqlJdbcDriverVersion}"
    compile "com.mysema.querydsl:querydsl-sql:{version}"
}
 
task generateQueryDslSql {
    // 별도 configuration 에서 JDBC Driver loading 할 것
    File mysql = configurations.querydslSqlJdbcDriver.find { it.toString().contains("mysql-connector-java") }
    URLClassLoader loader = GroovyObject.class.classLoader
    loader.addURL(mysql.toURI().toURL())
 
    DriverManager.registerDriver(loader.loadClass('com.mysql.jdbc.Driver').newInstance())
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8", "user", "password")
 
    MetaDataExporter exporter = new MetaDataExporter()
    exporter.namePrefix = 'S'
    exporter.packageName = 'package.to.querydslsql'
    exporter.targetFolder = file('src/generated/java-querydsl')
 
    // 기타 설정들
 
    exporter.export(connection.getMetaData())
 
    connection.close()
}
gradle/querydsl_native_sql_codegen.txt · 마지막으로 수정됨: 2018/08/31 13:53 저자 kwon37xi