Flask接口签名sign原理与实例代码浅析
524
2023-01-15
详解非spring框架下使用querydsl的方法
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文件
OwXxRoPxmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> OwXxRoP
OwXxRoPxmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
OwXxRoP
执行命令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
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
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创建实例时,需要读取该配置文件:
xmlns:xsi="http://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">
xmlns:xsi="http://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">
测OwXxRoP试代码
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
list.forEach(module->{
System.out.prinOwXxRoPtln(module.getId() + "," + module.getDisplayLabel() + "," + module.getCreateDate());
});
}
}
querydsl官方文档:http://querydsl.com/static/querydsl/latest/reference/html/ch02.html#jpa_integration
java类库:https://21doc.net/java/awesomejava#database
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~