JPA 条件查询方式
JPA 条件查询方式很有意思,是不需要写 SQL 语句的,只需要在 dao 接口里按照规范的命名定义对应的方法名,及可达到查询相应字段的效果了。
在如下代码里做了如下事情:
1. 首先通过 @Before 把 Category表里所有数据都删除了,并新增了10条。
2. 然后 test1() 查询所有数据,看看新增的10条数据。
3. 接着,test2() 通过自定义的接口方法 findByName,根据name 查询分类表
4. 接着,test3() 通过自定义的接口方法 findByNameLikeAndIdGreaterThanOrderByNameAsc,根据名称模糊查询,id 大于某值, 并且名称正排序查询。
在如下代码里做了如下事情:
1. 首先通过 @Before 把 Category表里所有数据都删除了,并新增了10条。
2. 然后 test1() 查询所有数据,看看新增的10条数据。
3. 接着,test2() 通过自定义的接口方法 findByName,根据name 查询分类表
4. 接着,test3() 通过自定义的接口方法 findByNameLikeAndIdGreaterThanOrderByNameAsc,根据名称模糊查询,id 大于某值, 并且名称正排序查询。
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
|
package
com.how2java.springboot.test;
import
java.util.List;
import
org.junit.Before;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.test.context.junit4.SpringRunner;
import
com.how2java.springboot.Application;
import
com.how2java.springboot.dao.CategoryDAO;
import
com.how2java.springboot.pojo.Category;
@RunWith
(SpringRunner.
class
)
@SpringBootTest
(classes = Application.
class
)
public
class
TestJPA {
@Autowired
CategoryDAO dao;
@Before
public
void
before() {
List<Category> cs= dao.findAll();
for
(Category c : cs) {
dao.delete(c);
}
for
(
int
i =
0
; i <
10
; i++) {
Category c =
new
Category();
c.setName(
"category "
+ i);
dao.save(c);
}
}
@Test
public
void
test1() {
List<Category> cs= dao.findAll();
System.out.println(
"所有的分类信息:"
);
for
(Category c : cs) {
System.out.println(c.getName());
}
System.out.println();
}
@Test
public
void
test2() {
System.out.println(
"查询名称是 \"category 1 \"的分类:"
);
List<Category> cs= dao.findByName(
"category 1"
);
for
(Category c : cs) {
System.out.println(
"c.getName():"
+ c.getName());
}
System.out.println();
}
@Test
public
void
test3() {
System.out.println(
"根据名称模糊查询,id 大于5, 并且名称正排序查询"
);
List<Category> cs= dao.findByNameLikeAndIdGreaterThanOrderByNameAsc(
"%3%"
,
5
);
for
(Category c : cs) {
System.out.println(c);
}
System.out.println();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package
com.how2java.springboot.dao;
import
java.util.List;
import
org.springframework.data.jpa.repository.JpaRepository;
import
com.how2java.springboot.pojo.Category;
public
interface
CategoryDAO
extends
JpaRepository<Category,Integer>{
public
List<Category> findByName(String name);
public
List<Category> findByNameLikeAndIdGreaterThanOrderByNameAsc(String name,
int
id);
}
|
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
|
package
com.how2java.springboot.pojo;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.GenerationType;
import
javax.persistence.Id;
import
javax.persistence.Table;
@Entity
@Table
(name =
"category_"
)
public
class
Category {
@Id
@GeneratedValue
(strategy = GenerationType.IDENTITY)
@Column
(name =
"id"
)
private
int
id;
@Column
(name =
"name"
)
private
String name;
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
@Override
public
String toString() {
return
"Category [id="
+ id +
", name="
+ name +
"]"
;
}
}
|
实现原理
虽然 JPA 没有自己手动写sql 语句,但是通过反射获取自定义的接口方法里提供的信息,就自导用户希望根据什么条件来查询了。 然后 JPA 底层再偷偷摸摸地拼装对应的 sql 语句,丢给数据库,就达到了条件查询的效果啦。
对反射不熟悉的同学,可了解反射基础教程: 反射基础教程
对反射不熟悉的同学,可了解反射基础教程: 反射基础教程
条件查询规范
JPA 条件查询方式 只是个别举例,下表把 jpa 做的各种查询规范都列出来了。 如果要做其他相关查询,按照表格中的规范设计接口方法即可。
http://download.how2j.cn/2004/springboot.rar