Querydsl是一個(gè)采用API代替拼湊字符串來構(gòu)造查詢語句,可跟 Hibernate 和 JPA 等框架結(jié)合使用。網(wǎng)上很多教程都是結(jié)合spring JPA框架下使用的教程,本文介紹的是非spring環(huán)境下querydsl JPA整合使用。
創(chuàng)新互聯(lián)建站專注于成縣企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城建設(shè)。成縣網(wǎng)站建設(shè)公司,為成縣等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
1、使用eclipse生成jpa實(shí)體類,IDEA也能生成同樣JPA實(shí)體類。
鼠標(biāo)右鍵打開JPA Tools工具:
創(chuàng)建數(shù)據(jù)庫鏈接:
選擇那些數(shù)據(jù)庫表需要生成JPA實(shí)體類:
關(guān)聯(lián)表生成:
設(shè)置實(shí)體生成屬性:
單個(gè)實(shí)體屬性修改
生成的JPA實(shí)體類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文件
4.0.0 com.test.db dbtest jar 1.0-SNAPSHOT dbtest http://maven.apache.org 4.2.1 junit junit 3.8.1 test com.querydsl querydsl-sql ${querydsl.version} com.querydsl querydsl-apt ${querydsl.version} com.querydsl querydsl-jpa ${querydsl.version} com.querydsl querydsl-sql-codegen ${querydsl.version} provided org.eclipse.persistence org.eclipse.persistence.jpa 2.6.0 org.slf4j slf4j-log4j12 1.6.1 MySQL mysql-connector-java 8.0.11 com.alibaba druid 1.1.10 org.hibernate hibernate-entitymanager 5.4.0.Final com.querydsl querydsl-maven-plugin ${querydsl.version} generate-sources jpa-export ${project.basedir}/target/generated-sources/java com.test.db.entity org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 UTF-8
執(zhí)行命令mvn compile或mvn eclipse:eclipse生成jpa實(shí)體對(duì)應(yīng)的querydsl查詢類
AdminLog.java對(duì)應(yīng)的querydsl對(duì)應(yīng)查詢類
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{ 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 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創(chuàng)建實(shí)例時(shí),需要讀取該配置文件:
<?xml version="1.0" encoding="UTF-8"?>org.hibernate.ejb.HibernatePersistence
測(cè)試代碼
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; Listlist = queryFactory.selectFrom(adminModule).fetch(); list.forEach(module->{ System.out.println(module.getId() + "," + module.getDisplayLabel() + "," + module.getCreateDate()); }); } }
querydsl官方文檔:http://www.querydsl.com/static/querydsl/latest/reference/html/ch02.html#jpa_integration
java類庫:https://www.21doc.net/java/awesomejava#database
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。