服务器之家

服务器之家 > 正文

详解非spring框架下使用querydsl的方法

时间:2021-06-30 14:47     来源/作者:penngo

querydsl是一个采用api代替拼凑字符串来构造查询语句,可跟 hibernate 和 jpa 等框架结合使用。网上很多教程都是结合spring jpa框架下使用的教程,本文介绍的是非spring环境下querydsl jpa整合使用。

1、使用eclipse生成jpa实体类,idea也能生成同样jpa实体类。

鼠标右键打开jpa tools工具:

详解非spring框架下使用querydsl的方法

创建数据库链接:

详解非spring框架下使用querydsl的方法

详解非spring框架下使用querydsl的方法

详解非spring框架下使用querydsl的方法

选择那些数据库表需要生成jpa实体类:

详解非spring框架下使用querydsl的方法

关联表生成:

详解非spring框架下使用querydsl的方法

设置实体生成属性:

详解非spring框架下使用querydsl的方法

单个实体属性修改

详解非spring框架下使用querydsl的方法

生成的jpa实体类adminlog.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<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对应查询类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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创建实例时,需要读取该配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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>

测试代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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官方文档:http://www.querydsl.com/static/querydsl/latest/reference/html/ch02.html#jpa_integration

java类库:https://www.21doc.net/java/awesomejava#database

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://my.oschina.net/penngo/blog/2998232

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部