详解非spring框架下使用querydsl的方法
程序员文章站
2024-02-24 19:59:22
querydsl是一个采用api代替拼凑字符串来构造查询语句,可跟 hibernate 和 jpa 等框架结合使用。网上很多教程都是结合spring jpa框架下使用的教程...
querydsl是一个采用api代替拼凑字符串来构造查询语句,可跟 hibernate 和 jpa 等框架结合使用。网上很多教程都是结合spring jpa框架下使用的教程,本文介绍的是非spring环境下querydsl jpa整合使用。
1、使用eclipse生成jpa实体类,idea也能生成同样jpa实体类。
鼠标右键打开jpa tools工具:
创建数据库链接:
选择那些数据库表需要生成jpa实体类:
关联表生成:
设置实体生成属性:
单个实体属性修改
生成的jpa实体类adminlog.java
package com.test.db.entity; import java.io.serializable; import javax.persistence.*; import java.util.date; /** * the persistent class for the admin_log database table. * */ @entity @table(name="admin_log") @namedquery(name="adminlog.findall", query="select a from adminlog a") public class adminlog implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy=generationtype.auto) @column(unique=true, nullable=false) private string id; @column(name="action_id", length=20) private string actionid; @column(name="client_ip", length=15) private string clientip; @column(name="controller_id", length=20) private string controllerid; @temporal(temporaltype.timestamp) @column(name="create_date") private date createdate; @column(name="create_user", length=50) private string createuser; @column(name="func_name", length=50) private string funcname; @column(name="module_name", length=50) private string modulename; @column(name="right_name", length=50) private string rightname; @column(length=200) private string url; public adminlog() { } public string getid() { return this.id; } public void setid(string id) { this.id = id; } public string getactionid() { return this.actionid; } public void setactionid(string actionid) { this.actionid = actionid; } public string getclientip() { return this.clientip; } public void setclientip(string clientip) { this.clientip = clientip; } public string getcontrollerid() { return this.controllerid; } public void setcontrollerid(string controllerid) { this.controllerid = controllerid; } public date getcreatedate() { return this.createdate; } public void setcreatedate(date createdate) { this.createdate = createdate; } public string getcreateuser() { return this.createuser; } public void setcreateuser(string createuser) { this.createuser = createuser; } public string getfuncname() { return this.funcname; } public void setfuncname(string funcname) { this.funcname = funcname; } public string getmodulename() { return this.modulename; } public void setmodulename(string modulename) { this.modulename = modulename; } public string getrightname() { return this.rightname; } public void setrightname(string rightname) { this.rightname = rightname; } public string geturl() { return this.url; } public void seturl(string url) { this.url = url; } }
2、生成querydsl查询类
maven pom.xml文件
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.test.db</groupid> <artifactid>dbtest</artifactid> <packaging>jar</packaging> <version>1.0-snapshot</version> <name>dbtest</name> <url>http://maven.apache.org</url> <properties> <querydsl.version>4.2.1</querydsl.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-sql</artifactid> <version>${querydsl.version}</version> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-apt</artifactid> <version>${querydsl.version}</version> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-jpa</artifactid> <version>${querydsl.version}</version> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-sql-codegen</artifactid> <version>${querydsl.version}</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.eclipse.persistence</groupid> <artifactid>org.eclipse.persistence.jpa</artifactid> <version>2.6.0</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.6.1</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>8.0.11</version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.1.10</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-entitymanager</artifactid> <version>5.4.0.final</version> </dependency> </dependencies> <build> <plugins> <!-- 生成jpa实体插件 --> <plugin> <groupid>com.querydsl</groupid> <artifactid>querydsl-maven-plugin</artifactid> <version>${querydsl.version}</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>jpa-export</goal> </goals> <configuration> <targetfolder>${project.basedir}/target/generated-sources/java</targetfolder> <packages>com.test.db.entity</packages> </configuration> </execution> </executions> </plugin> <!-- 生成jpa实体插件 --> <!-- <plugin> <groupid>com.mysema.maven</groupid> <artifactid>apt-maven-plugin</artifactid> <version>1.1.3</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputdirectory>target/generated-sources/java</outputdirectory> <processor>com.querydsl.apt.jpa.jpaannotationprocessor</processor> </configuration> </execution> </executions> </plugin> --> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> </project>
执行命令mvn compile或mvn eclipse:eclipse生成jpa实体对应的querydsl查询类
adminlog.java对应的querydsl对应查询类
package com.test.db.entity; import static com.querydsl.core.types.pathmetadatafactory.*; import com.querydsl.core.types.dsl.*; import com.querydsl.core.types.pathmetadata; import javax.annotation.generated; import com.querydsl.core.types.path; /** * qadminlog is a querydsl query type for adminlog */ @generated("com.querydsl.codegen.entityserializer") public class qadminlog extends entitypathbase<adminlog> { private static final long serialversionuid = 392071999l; public static final qadminlog adminlog = new qadminlog("adminlog"); public final stringpath actionid = createstring("actionid"); public final stringpath clientip = createstring("clientip"); public final stringpath controllerid = createstring("controllerid"); public final datetimepath<java.util.date> createdate = createdatetime("createdate", java.util.date.class); public final stringpath createuser = createstring("createuser"); public final stringpath funcname = createstring("funcname"); public final stringpath id = createstring("id"); public final stringpath modulename = createstring("modulename"); public final stringpath rightname = createstring("rightname"); public final stringpath url = createstring("url"); public qadminlog(string variable) { super(adminlog.class, forvariable(variable)); } public qadminlog(path<? extends adminlog> path) { super(path.gettype(), path.getmetadata()); } public qadminlog(pathmetadata metadata) { super(adminlog.class, metadata); } }
3、查询例子
jpa持久化meta-inf\persistence.xml配置文件,entitymanagerfactory创建实例时,需要读取该配置文件:
<?xml version="1.0" encoding="utf-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="jpa" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <properties> <!-- .mysqlinnodbdialect --> <property name="hibernate.dialect" value="org.hibernate.dialect.mysql8dialect"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/yiiboot?useunicode=true&characterencoding=utf-8&servertimezone=asia/shanghai&usessl=true" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="123" /> <property name="connection.provider_class" value="com.alibaba.druid.support.hibernate.druidconnectionprovider" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialsize" value="1" /> <property name="minidle" value="1" /> <property name="maxactive" value="20" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxwait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timebetweenevictionrunsmillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minevictableidletimemillis" value="300000" /> </properties> </persistence-unit> </persistence>
测试代码
package com.test.db.main; import java.util.list; import javax.persistence.entitymanagerfactory; import javax.persistence.persistence; import com.querydsl.jpa.impl.jpaqueryfactory; import com.test.db.entity.adminmodule; import com.test.db.entity.qadminmodule; public class jpatest { public static void main(string[] args) throws exception{ string persistenceunitname = "jpa"; entitymanagerfactory entitymanagerfactory = persistence.createentitymanagerfactory(persistenceunitname); jpaqueryfactory queryfactory = new jpaqueryfactory(entitymanagerfactory.createentitymanager()); qadminmodule adminmodule = qadminmodule.adminmodule; list<adminmodule> list = queryfactory.selectfrom(adminmodule).fetch(); list.foreach(module->{ system.out.println(module.getid() + "," + module.getdisplaylabel() + "," + module.getcreatedate()); }); } }
querydsl官方文档:
java类库:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java.security.UnrecoverableKeyException: Cannot recover key 博客分类: JavaJava Web keytoolca证书获取私钥数字证书
下一篇: IDEA中osgi的开发应用指南详解
推荐阅读
-
详解非spring框架下使用querydsl的方法
-
Spring boot中使用ElasticSearch的方法详解
-
Spring MVC中使用Google kaptcha验证码的方法详解
-
Spring boot中PropertySource注解的使用方法详解
-
使用dubbo+zookeeper+spring boot构建服务的方法详解
-
Spring Boot中使用Spring-data-jpa的配置方法详解
-
详解spring cloud使用Hystrix实现单个方法的fallback
-
使用dubbo+zookeeper+spring boot构建服务的方法详解
-
Spring boot中PropertySource注解的使用方法详解
-
详解spring切面使用传递给被通知方法的参数