经过多年,spring data jpa越来越完善,在版本迭代的过程中,会不断增加功能,今天看新的reference发现有querydsl.然后搜索到上面的参考资料2
无论是jpaspecificationexecutor,还是querydslpredicateexecutor,它俩都提供了使用predicate(意义相同,都是构建where子句;类不同,javax.persistence.criteria.predicate,com.querydsl.core.types.predicate)去构建查询,使用比较方便.
关于两者的简单使用,上面的参考资料2有介绍.文末也有总结,从概括来看,我个人认为应倾向使用querydslpredicateexecutor,querydsl不仅适用于jpa repositories,还支持mongodb.
下面是个例子
1.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
124
125
126
127
128
129
130
131
132
133
134
135
136
|
<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/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>org.exam</groupid> <artifactid>testjava</artifactid> <version> 1.0 . 0 </version> <name>${project.artifactid}</name> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> <spring.version> 4.2 . 5 .release</spring.version> <spring-data.version>hopper-sr1</spring-data.version> <querydsl.version> 4.1 . 1 </querydsl.version> <hibernate.version> 5.1 . 0 . final </hibernate.version> <tomcat.version> 8.0 . 32 </tomcat.version> <logback.version> 1.1 . 7 </logback.version> <mysql.version> 5.1 . 33 </mysql.version> <junit.version> 4.12 </junit.version> </properties> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-framework-bom</artifactid> <version>${spring.version}</version> <scope> import </scope> <type>pom</type> </dependency> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-releasetrain</artifactid> <version>${spring-data.version}</version> <scope> import </scope> <type>pom</type> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version> 3.1 </version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupid>com.mysema.maven</groupid> <artifactid>maven-apt-plugin</artifactid> <version> 1.0 . 4 </version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>process</goal> </goals> <configuration> <outputdirectory>target/generated-sources</outputdirectory> <processor>com.querydsl.apt.jpa.jpaannotationprocessor</processor> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-jpa</artifactid> </dependency> <dependency> <groupid>org.springframework.data</groupid> <artifactid>spring-data-mongodb</artifactid> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-apt</artifactid> <version>${querydsl.version}</version> <scope>provided</scope> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-jpa</artifactid> <version>${querydsl.version}</version> </dependency> <dependency> <groupid>com.querydsl</groupid> <artifactid>querydsl-mongodb</artifactid> <version>${querydsl.version}</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-entitymanager</artifactid> <version>${hibernate.version}</version> </dependency> <dependency> <groupid>org.apache.tomcat</groupid> <artifactid>tomcat-jdbc</artifactid> <version>${tomcat.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> </dependency> <dependency> <groupid>ch.qos.logback</groupid> <artifactid>logback-classic</artifactid> <version>${logback.version}</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>${mysql.version}</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> </dependency> </dependencies> <repositories> <repository> <id>central</id> <name>central repository</name> <url>http: //repo1.maven.org/maven2</url> <layout> default </layout> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> </project> |
2.domain类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package org.exam.domain; import org.springframework.data.mongodb.core.mapping.document; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.id; import java.io.serializable; @entity @document public class employee implements serializable { @id @column (length = 38 ) private string id; @column (length = 32 ) private string name; private long salary; private long departmentid; //setter和getter略 } |
3.repository类
1
2
3
4
5
6
7
8
|
package org.exam.repository.jpa; import org.exam.domain.employee; import org.springframework.data.querydsl.querydslpredicateexecutor; import org.springframework.data.repository.pagingandsortingrepository; import java.util.collection; public interface jpaemployeerepository extends pagingandsortingrepository<employee,string>,querydslpredicateexecutor<employee>{ collection<employee> findbyidin(collection<string> ids); } |
1
2
3
4
5
6
7
8
9
|
package org.exam.repository.mongo; import org.exam.domain.employee; import org.springframework.data.querydsl.querydslpredicateexecutor; import org.springframework.data.repository.pagingandsortingrepository; import java.util.collection; public interface mongoemployeerepository extends pagingandsortingrepository<employee, string>, querydslpredicateexecutor<employee> { collection<employee> findbyidin(collection<string> ids); } |
jpa有jparepository,mongodb有mongorepository,它俩都继承pagingandsortingrepository
3.配置类
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
|
package org.exam.config; import com.mongodb.mongoclient; import com.mongodb.writeconcern; import org.apache.tomcat.jdbc.pool.datasource; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; import org.springframework.core.env.environment; import org.springframework.data.jpa.repository.config.enablejparepositories; import org.springframework.data.mongodb.mongodbfactory; import org.springframework.data.mongodb.core.mongotemplate; import org.springframework.data.mongodb.core.simplemongodbfactory; import org.springframework.data.mongodb.core.writeresultchecking; import org.springframework.data.mongodb.repository.config.enablemongorepositories; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.database; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import javax.annotation.resource; import java.net.unknownhostexception; import java.util.properties; @configuration @propertysource ( "classpath:config.properties" ) @enabletransactionmanagement @enablejparepositories (basepackages = "org.exam.repository.jpa" ) @enablemongorepositories (basepackages = "org.exam.repository.mongo" ) public class appconfig { @resource private environment env; @bean (destroymethod = "close" ) public datasource datasource() { datasource datasource = new datasource(); datasource.setdriverclassname(env.getproperty( "ds.driverclassname" )); datasource.seturl(env.getproperty( "ds.url" )); datasource.setusername(env.getproperty( "ds.username" )); datasource.setpassword(env.getproperty( "ds.password" )); datasource.setinitialsize(env.getproperty( "ds.initialsize" , integer. class )); datasource.setminidle(env.getproperty( "ds.minidle" , integer. class )); datasource.setmaxidle(env.getproperty( "ds.maxidle" , integer. class )); datasource.setmaxactive(env.getproperty( "ds.maxactive" , integer. class )); return datasource; } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { hibernatejpavendoradapter jpavendoradapter = new hibernatejpavendoradapter(); jpavendoradapter.setdatabase(database.valueof(env.getproperty( "jpa.database" ))); jpavendoradapter.setgenerateddl(env.getproperty( "jpa.generateddl" , boolean . class )); jpavendoradapter.setshowsql(env.getproperty( "jpa.showsql" , boolean . class )); localcontainerentitymanagerfactorybean emf = new localcontainerentitymanagerfactorybean(); emf.setdatasource(datasource()); emf.setpackagestoscan( "org.exam.domain" ); emf.setjpavendoradapter(jpavendoradapter); properties properties = new properties(); properties.setproperty( "hibernate.default_schema" , env.getproperty( "jpa.defaultschema" )); emf.setjpaproperties(properties); return emf; } @bean public platformtransactionmanager transactionmanager() { return new jpatransactionmanager(entitymanagerfactory().getobject()); } @bean public mongodbfactory mongodbfactory() throws unknownhostexception { return new simplemongodbfactory( new mongoclient(env.getproperty( "mongo.host" ), env.getproperty( "mongo.port" , integer. class )), env.getproperty( "mongo.db" )); } @bean public mongotemplate mongotemplate() throws unknownhostexception { mongotemplate mongotemplate = new mongotemplate(mongodbfactory()); mongotemplate.setwriteresultchecking(writeresultchecking.exception); mongotemplate.setwriteconcern(writeconcern.normal); return mongotemplate; } } |
4.测试类
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
|
package org.exam.repository.jpa; import org.exam.config.appconfig; import org.exam.domain.employee; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.domain.page; import org.springframework.data.domain.pagerequest; import org.springframework.test.annotation.rollback; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.support.annotationconfigcontextloader; import org.springframework.transaction.annotation.transactional; import java.util.uuid; @runwith (springjunit4classrunner. class ) @contextconfiguration (loader = annotationconfigcontextloader. class , classes = {appconfig. class }) @transactional (transactionmanager = "transactionmanager" ) //rollback默认为true public class jpaemployeerepositorytest { @autowired private jpaemployeerepository jpaemployeerepository; @test @rollback ( false ) public void testsave() { for ( int i = 0 ; i < 5 ; i++) { employee employee = new employee(); employee.setid(uuid.randomuuid().tostring()); employee.setname( "name" ); employee.setdepartmentid( 1 + i); employee.setsalary( 6800 + i); jpaemployeerepository.save(employee); } } @test public void testfindall() { page<employee> all = jpaemployeerepository.findall( null , new pagerequest( 0 , 8 )); for (employee employee : all) { system.out.println( "employee = " + employee); } } } |
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
|
package org.exam.repository.mongo; import com.mongodb.mongoclient; import org.exam.config.appconfig; import org.exam.domain.employee; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.domain.page; import org.springframework.data.domain.pagerequest; import org.springframework.data.geo.circle; import org.springframework.data.mongodb.core.mongotemplate; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.support.annotationconfigcontextloader; import java.net.unknownhostexception; @runwith (springjunit4classrunner. class ) @contextconfiguration (loader = annotationconfigcontextloader. class , classes = {appconfig. class }) public class mongoemployeerepositorytest { @autowired private mongoemployeerepository mongoemployeerepository; public static void main(string[] args) throws unknownhostexception { mongotemplate template = new mongotemplate( new mongoclient( "127.0.0.1" , 27017 ), "test" ); circle circle = new circle(- 73.99171 , 40.738868 , 0.01 ); system.out.println(); } @test public void testfindall() { page<employee> all = mongoemployeerepository.findall( null , new pagerequest( 0 , 8 )); for (employee employee : all) { system.out.println( "employee = " + employee); } } } |
5.其它config.properties,logback.xml文件不太重要,篇幅关系就省略.
再了解一下比较大的需求,从几张表来取几个字段的数据,返回分页排序数据.
1.在appconfig注册jpaqueryfactory bean
1
2
3
4
|
@bean public jpaqueryfactory jpaqueryfactory(entitymanager entitymanager) { return new jpaqueryfactory( new hqltemplates(), entitymanager); } |
2.建一个dto(数据传输对象)
1
2
3
4
5
6
|
public class userdto { private string empname; private string deptname; private long salary; //setter,getter略 } |
3.查询例子测试
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
|
private page<userdto> findall(string empname,pageable pageable) { qemployee qemp = qemployee.employee; qdepartment qdep = qdepartment.department; list<predicate> criteria = new arraylist<>(); if (stringutils.hastext(empname)){ criteria.add(qemp.name.eq(empname.trim())); } jpaquery<?> query = jpaqueryfactory.from(qemp).innerjoin(qdep).on(qemp.deptid.eq(qdep.id)).where(criteria.toarray( new predicate[criteria.size()])); long total = query.fetchcount(); list<userdto> content; if (pageable == null || total > pageable.getoffset()) { map<string, simpleexpression<?>> map = new hashmap<>(); map.put( "deptname" , qdep.name); map.put( "empname" , qemp.name); map.put( "salary" , qemp.salary); content = querydslutils.applypagination(pageable, query).select(projections.bean(userdto. class , map)).fetch(); } else { content = collections.emptylist(); } return new pageimpl<>(content, pageable, total); } @test public void test() { pageable pageable = new pagerequest( 0 , 10 , new qsort( new orderspecifier<>(order.desc, qemployee.employee.salary), new orderspecifier<>(order.asc, qdepartment.department.name))); page<userdto> page = findall( "name" , pageable); for (userdto userdto : page) { system.out.println( "userdto = " + userdto); } } |
参考资料
1:http://docs.spring.io/spring-data/jpa/docs/1.10.x/reference/pdf/spring-data-jpa-reference.pdf
2:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiejx618/article/details/51480265