简体   繁体   English

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

[英]Combining Querydsl-jpa and querydsl-sql and code generation

Here is the thing: 这是东西:

  1. I have been using querydsl-jpa in my projects and code generation has never been a problem. 我在项目中一直使用querydsl-jpa ,并且代码生成从未成为问题。 I use this plugin in maven : 我在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. Now, I need to also use querydsl-sql and apparently, I can't use the Q -generated classes created by com.querydsl.apt.jpa.JPAAnnotationProcessor . 现在,我还需要使用querydsl-sql ,显然,我不能使用com.querydsl.apt.jpa.JPAAnnotationProcessor创建的Q生成的类。 Here is the plugin in maven : 这是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> 

THE CHALLENGE 挑战

  1. The second plugin above generates Q -classes for all schemas in my DBMS (MySql) whereas I have specified the schema to generate Q -classes from. 上面的第二个插件为我的DBMS(MySql)中的所有模式生成Q类,而我指定了从中生成Q类的模式。

  2. How do I specify the username, password and jdbcUrl from a file since I don't want to store sensitive information in the git repository. 如何从文件中指定用户名,密码和jdbcUrl,因为我不想在git存储库中存储敏感信息。

Here are my solutions: 这是我的解决方案:

  1. For challenge one, I haven't found a solution per se but some sort of workaround. 对于挑战之一,我本身还没有找到解决方案,但是找到了某种解决方法。 I created a user in my DBMS (MySql) that has privileges on the single schema that I am interested in. That way, the user won't be able to generate Q -classes for other schemas. 我在DBMS(MySql)中创建了一个对我感兴趣的单一模式具有特权的用户。这样,该用户将无法为其他模式生成Q类。 So problem one "solved". 于是问题一“解决了”。

Though I still believe that in the plugin one should be able to specify the schema to be generated. 尽管我仍然相信在插件中应该能够指定要生成的架构。 Interestingly enough <schemaPattern></schemaPattern> as suggested by @Rober Bain which is also in the querydsl-sql documentation does not work. 有趣的是, <schemaPattern></schemaPattern> Rober Bain建议的<schemaPattern></schemaPattern>也不起作用,这在querydsl-sql文档中也不起作用。

  1. For challenge two, first you need to create a properties files say dev.properties with the needed content 对于第二个挑战,首先需要创建一个属性文件,例如dev.properties以及所需的内容

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

    jdbc-user=my_user

    jdbc-password=my_password

    Then, include the following properties-maven-plugin 然后,包含以下属性-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> 

    ... and in your query-dsl 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> 

Check out this link for more info Read pom.xml configurations from properties file 查看此链接以获取更多信息。 从属性文件中读取pom.xml配置。

  1. Use schemaPattern within the configuration element: "a schema name pattern in LIKE pattern form; must match the schema name as it is stored in the database, multiple can be separated by comma (default: null)" from the querydsl docs . configuration元素中使用schemaPattern :“ LIKE模式形式的模式名称模式;必须与存储在数据库中的模式名称匹配,多个模式可以用逗号分隔(默认:null)”(来自querydsl docs)

  2. While the doesn't do exactly what you're asking for, I believe it's the standard way of solving this problem. 尽管不能完全满足您的要求,但我认为这是解决此问题的标准方法。 Use encrypted data in a Maven pom . 在Maven pom中使用加密的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM