结合Querydsl-jpa和querydsl-sql与代码生成

时间:2019-01-05 06:32:43

标签: java spring-data-jpa querydsl

这是东西:

  1. 我在项目中一直使用Generate template,并且代码生成从未成为问题。我在 requestTemplates: application/json: | ## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html ## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload #set($allParams = $input.params()) { "body-json" : $input.json('$'), "params" : { #foreach($type in $allParams.keySet()) #set($params = $allParams.get($type)) "$type" : { #foreach($paramName in $params.keySet()) "$paramName" : "$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end #end } #if($foreach.hasNext),#end #end }, "stage-variables" : { #foreach($key in $stageVariables.keySet()) "$key" : "$util.escapeJavaScript($stageVariables.get($key))" #if($foreach.hasNext),#end #end }, "context" : { "account-id" : "$context.identity.accountId", "api-id" : "$context.apiId", "api-key" : "$context.identity.apiKey", "authorizer-principal-id" : "$context.authorizer.principalId", "caller" : "$context.identity.caller", "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider", "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType", "cognito-identity-id" : "$context.identity.cognitoIdentityId", "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId", "http-method" : "$context.httpMethod", "stage" : "$context.stage", "source-ip" : "$context.identity.sourceIp", "user" : "$context.identity.user", "user-agent" : "$context.identity.userAgent", "user-arn" : "$context.identity.userArn", "request-id" : "$context.requestId", "resource-id" : "$context.resourceId", "resource-path" : "$context.resourcePath" } } passthroughBehavior: "never" httpMethod: "POST" type: "aws" 中使用了这个插件:

    querydsl-jpa
  2. 现在,我还需要使用maven,显然,我不能使用 <plugin> <groupId>com.mysema.maven</groupId> <artifactId>maven-apt-plugin</artifactId> <version>1.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>process</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/generated-sources</outputDirectory> <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> </configuration> </execution> </executions> </plugin> 创建的querydsl-sql生成的类。这是Q中的插件:

    com.querydsl.apt.jpa.JPAAnnotationProcessor

挑战

  1. 上面的第二个插件为我的DBMS(MySql)中的所有模式生成maven类,而我已经指定了从中生成 <plugin> <groupId>com.querydsl</groupId> <artifactId>querydsl-maven-plugin</artifactId> <version>4.2.1</version> <executions> <execution> <goals> <goal>export</goal> </goals> </execution> </executions> <configuration> <jdbcDriver>com.mysql.cj.jdbc.Driver</jdbcDriver> <jdbcUrl>jdbc:mysql://localhost:3306/mydatabase</jdbcUrl> <jdbcUser>root</jdbcUser> <jdbcPassword></jdbcPassword> <packageName>com.myproject.domain</packageName> <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies> </plugin> 类的模式。

  2. 由于不想将敏感信息存储在Q存储库中,因此如何从文件中指定用户名,密码和jdbcUrl。

2 个答案:

答案 0 :(得分:1)

这是我的解决方案:

  1. 对于挑战之一,我本身还没有找到解决方案,但是找到了一种解决方法。我在DBMS(MySql)中创建了一个对我感兴趣的单一模式具有特权的用户。这样,该用户将无法为其他模式生成Q类。问题一“解决了”。

尽管我仍然相信在插件中应该能够指定要生成的架构。有趣的是,{Rober Bain所建议的<schemaPattern></schemaPattern>也在querydsl-sql文档中无效。

  1. 对于第二个挑战,首先需要创建一个属性文件,例如dev.properties,其中包含所需的内容

    jdbc-url=jdbc:mysql://localhost:3306/myschema?nullNamePatternMatchesAll=true

    jdbc-user=my_user

    jdbc-password=my_password

    然后,包含以下 properties-maven-plugin

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>dev.properties</file> // Reference to properties file
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>   
    

    ...以及您的query-dsl插件中...

        <plugin>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-maven-plugin</artifactId>
            <version>4.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>export</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
                <jdbcUrl>${jdbc-url}</jdbcUrl>
                <jdbcUser>${jdbc-user}</jdbcUser>
                <jdbcPassword>${jdbc-password}</jdbcPassword>
                <packageName>com.myproject.domain</packageName>
                <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>6.0.6</version>
                </dependency>
            </dependencies>
        </plugin> 
    

查看此链接以获取更多信息Read pom.xml configurations from properties file

答案 1 :(得分:0)

  1. schemaPattern元素中使用configuration:“ LIKE模式形式的模式名称模式;必须与存储在数据库中的模式名称匹配,多个可以用逗号分隔(默认:null)”,从querydsl docs

  2. 虽然不能完全满足您的要求,但我认为这是解决此问题的标准方法。 Use encrypted data in a Maven pom