[英]Combining Querydsl-jpa and querydsl-sql and code generation
Here is the thing: 这是东西:
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>
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 挑战
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
类的模式。
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: 这是我的解决方案:
Q
-classes for other schemas. 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
文档中也不起作用。
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配置。
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) 。
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.