【问题标题】:Combining Querydsl-jpa and querydsl-sql and code generation结合 Querydsl-jpa 和 querydsl-sql 和代码生成
【发布时间】:2019-05-31 15:42:38
【问题描述】:

事情是这样的:

  1. 我一直在我的项目中使用querydsl-jpa,代码生成从来都不是问题。我在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>
    
  2. 现在,我还需要使用querydsl-sql,显然,我不能使用com.querydsl.apt.jpa.JPAAnnotationProcessor 创建的Q 生成的类。这是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>
    

挑战

  1. 上面的第二个插件为我的 DBMS (MySql) 中的所有模式生成Q-classes,而我已经指定了生成Q-classes 的模式。

  2. 我不想将敏感信息存储在git 存储库中,如何从文件中指定用户名、密码和 jdbcUrl。

【问题讨论】:

    标签: java spring-data-jpa querydsl


    【解决方案1】:

    这是我的解决方案:

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

    虽然我仍然相信在插件中应该能够指定要生成的架构。有趣的是,@Rober Bain 所建议的 &lt;schemaPattern&gt;&lt;/schemaPattern&gt;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

    【讨论】:

    • 非常感谢!经过几个小时试图解决这个问题,你的回答拯救了我的一天。
    • @AlexandreMucci 我一直不明白为什么&lt;schemaPattern&gt;&lt;/schemaPattern&gt; 不起作用。看起来这是querydsl-maven 插件中的错误。但是将用户限制在特定的架构中会产生魔力。
    • 我都没有。你还在用querydsl吗?我喜欢这个工具,但我认为设置起来非常复杂。你有什么替代方案吗?
    • @AlexandreMucci,是的,我是。我更喜欢 JPA 的 Criteria API 和 JOOQ(非开源)。我喜欢 querydsl 用于编写动态查询的语法。关于设置,这是真的。这可能有点棘手,尤其是对于初次使用的用户,但一旦设置完成,它就是一个很棒的工具。
    【解决方案2】:
    1. configuration 元素中使用schemaPattern:“LIKE 模式形式的模式名称模式;必须与存储在数据库中的模式名称匹配,多个可以用逗号分隔(默认值:null )”来自querydsl docs

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

    【讨论】:

    • 1.还是不行。我的架构名为inuka2,我添加了&lt;schemaPattern&gt;inuka2&lt;/schemaPattern&gt;,但它仍在为我的本地mysql数据库中的所有架构生成。 2. 我不想硬编码用户名、密码和 jdbcUrl,因为我不想为测试和生产环境编译应用程序。
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 2012-11-09
    • 2019-03-25
    • 2021-01-20
    • 2018-06-03
    • 1970-01-01
    • 2014-09-14
    • 2016-02-17
    相关资源
    最近更新 更多