- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章springData使用QueryDsl的示例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
经过多年,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 。
最后此篇关于springData使用QueryDsl的示例代码的文章就讲到这里了,如果你想了解更多关于springData使用QueryDsl的示例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
本节主要介绍Repository接口规范,及其子接口 Repository是一个空接口,即标准接口若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repositoty
SpringData JPA 的 PagingAndSortingRepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springframework.data.do
@Query注解查询适用于所查询的数据无法通过关键字查询得到结果的查询。这种查询可以摆脱像关键字查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring Data的特有实现。
我将 Doc 对象存储在 mongodb 存储库中,确保条形码属性在整个集合中是唯一的: @Document(collection = "Doc") public class Doc { @Id
我有这个简单的类: @Document (collection = "advertise") public class AdvertiseCache { @Id private int
我正在使用SpringData将我的应用程序连接到Elastic search本地实例。当我执行常规的curl获取ES信息时,它可以正常工作,但是我无法从Springboot应用程序连接到它。 Ela
我想通过job的code和code的列表找到一个Type文档category,我尝试了以下查询,但没有成功 @Document public class Type { @Id priv
在以下情况下,当我保存 CharFoo 对象然后执行查找时,通用字段 abstractFooField 会转换回字符,但子文档中的 bar.field 会转换为字符串,而不是字符。 难道我做错了什么?
一. 假设需求场景 在我们开发的过程中,经常出现两个对象存在一对多或多对一的关系。如何在程序在表明这两个对象的关系,以及如何利用这种关系优雅地使用它们。 其实,在javax.persistenc
他们是否可以通过任何方式使用 mongodb 投影功能从文档中获取选定的字段。 即使在互联网上搜索了一整天后也找不到任何线索。 最佳答案 这是relevant section有关存储库的文档。还有 s
如何在运行时参数化 SpringData ElasticSearch 索引? 例如,数据模型: @Document(indexName = "myIndex") public class Asset
我正在使用spring-data-elasticsearch-4.0.1-RELEASE和elastic 7.6.2。存储实体时,某些对象未正确序列化。它存储如下: "ownerResource":
我需要获取所有文件,按某个字段排序(在我的例子中是 parent)。 在我的 ItemRepository我添加了以下签名: public List findAllOrderByParent(); 但
我发出了一个请求,当我直接在 Mongo 中执行它时,它很快就会给出结果,当我使用 SpringData 执行代码时,它会给出 AsyncRequestTimeoutException (即使在使用索
我已将 POJO 映射到 SpringData MongoDB 文档 @Document(collection = “cacheVersion” ) public class CacheVersion
我们正在使用: spring-data-mongodb 版本 1.7.2.RELEASE(但我们开放升级) spring 版本 4.2.6.RELEASE(但我们开放升级) MongoDB 版本 3.
我从 REST 服务获取以下 JSON(170~ 费率) { "success":true, "timestamp":1527944949, "base":"EUR", "date":
有没有什么方法可以通过 MongoDB 的 Spring Data 库进行 NotNull、Size 等验证? 最佳答案 我相信你知道,很多 MongoDB 验证必须在软件层强制执行,因为数据库本身是
我有以下查询来从集合中获取文档: public List findSubEventsBetweenDates(Event parentEvent, LocalDate lowerDate, Local
我们有一个用例,用户可以为集合传递任意搜索条件,并希望输出分页。使用 Spring Data 存储库,如果我们通过简单扩展 MongoRepository 并声明一个: Page findByFooA
我是一名优秀的程序员,十分优秀!