在实际开发中,最为场景的是基于数据库的CURD等,比如Springboot集成mysql的数据库,常用的方式有JPA和Mybatis;本文主要介绍基于JPA方式的基础封装思路
1. 准备数据库
创建数据库以及表,创建表语句如下
1 | -- 创建用户表 |
2. 案例代码
2.1 引入maven依赖
1 | <dependency> |
2.2 配置yml
1 | spring: |
2.3 定义实体类
BaseEntity
1
2
3
4
5/**
* @author xiaoyuge
*/
public interface BaseEntity extends Serializable {
}User对象
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
58import lombok.Data;
import javax.persistence.*;
import java.time.LocalDateTime;
/**
* @author xiaoyuge
*/
public class User implements BaseEntity {
/**
* user id.
*/
private Long id;
/**
* username.
*/
private String userName;
/**
* user pwd.
*/
private String password;
/**
* email.
*/
private String email;
/**
* phoneNumber.
*/
private long phoneNumber;
/**
* description.
*/
private String description;
/**
* create date time.
*/
private LocalDateTime createTime;
/**
* update date time.
*/
private LocalDateTime updateTime;
public User() {
}
//如果要关联角色表的话,需要有中间表tb_user_role
// @ManyToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.EAGER)
// @JoinTable(name = "tb_user_role", joinColumns = {
// @JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
// private Set<Role> roles;
}用户查询VO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/**
* @author xiaoyuge
*/
public class UserQueryBean {
/**
* contains name pattern.
*/
private String name;
/**
* contains desc pattern.
*/
private String description;
}
2.4 定义DAO层
IBaseDao
1
2
3
4
5
6/**
* @author xiaoyuge
*/
public interface IBaseDao<T extends BaseEntity, I extends Serializable> extends JpaRepository<T, I>, JpaSpecificationExecutor<T> {
}IUserDao
1
2
3
public interface IUserDao extends IBaseDao<User, Long> {
}
2.5 定义Service层
封装IBaseService
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import java.io.Serializable;
import java.util.List;
/**
* @author xiaoyuge
*/
public interface IBaseService<T,I extends Serializable> {
/**
* @param id id
* @return T
*/
T find(I id);
/**
* @return List
*/
List<T> findAll();
/**
* @param ids ids
* @return List
*/
List<T> findList(I[] ids);
/**
* @param ids ids
* @return List
*/
List<T> findList(Iterable<I> ids);
/**
* @param pageable pageable
* @return Page
*/
Page<T> findAll(Pageable pageable);
/**
* 查寻全部
*
* @param spec spec
* @param pageable pageable
* @return Page
*/
Page<T> findAll(Specification<T> spec, Pageable pageable);
/**
* @param spec spec
* @return T
*/
T findOne(Specification<T> spec);
/**
* count.
*
* @return long
*/
long count();
/**
* count.
*
* @param spec spec
* @return long
*/
long count(Specification<T> spec);
/**
* exists.
*
* @param id id
* @return boolean
*/
boolean exists(I id);
/**
* save.
*
* @param entity entity
*/
void save(T entity);
/**
* save.
*
* @param entities entities
*/
void save(List<T> entities);
/**
* update.
*
* @param entity entity
* @return T
*/
T update(T entity);
/**
* delete.
*
* @param id id
*/
void delete(I id);
/**
* delete by ids.
*
* @param ids ids
*/
void deleteByIds(List<I> ids);
/**
* delete.
*
* @param entities entities
*/
void delete(T[] entities);
/**
* delete.
*
* @param entities entities
*/
void delete(Iterable<T> entities);
/**
* delete.
*
* @param entity entity
*/
void delete(T entity);
/**
* delete all.
*/
void deleteAll();
/**
* find list.
*
* @param spec spec
* @return list
*/
List<T> findList(Specification<T> spec);
/**
* find list.
*
* @param spec spec
* @param sort sort
* @return List
*/
List<T> findList(Specification<T> spec, Sort sort);
/**
* flush.
*/
void flush();
}定义
IUserService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import org.example.entity.User;
import org.example.entity.vo.UserQueryBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
/**
* @author xiaoyuge
*/
public interface IUserService extends IBaseService<User, Long> {
/**
* page
* @param user
* @param pageRequest
* @return
*/
Page<User> findPage(UserQueryBean user, PageRequest pageRequest);
}
2.6 定义Service实现层
BaseServiceImpl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import lombok.extern.slf4j.Slf4j;
import org.example.dao.IBaseDao;
import org.example.entity.BaseEntity;
import org.example.service.IBaseService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
/**
* @author xiaoyuge
*/
public abstract class BaseServiceImpl<T extends BaseEntity, I extends Serializable> implements IBaseService<T, I> {
/**
* @return IBaseDao
*/
public abstract IBaseDao<T, I> getBaseDao();
/**
* @param id id
* @return T
*/
public T find(I id) {
return getBaseDao().findById(id).orElse(null);
}
/**
* @return List
*/
public List<T> findAll() {
return getBaseDao().findAll();
}
/**
* @param ids ids
* @return List
*/
public List<T> findList(I[] ids) {
List<I> idList = Arrays.asList(ids);
return getBaseDao().findAllById(idList);
}
/**
* @param ids ids
* @return List
*/
public List<T> findList(Iterable<I> ids) {
return getBaseDao().findAllById(ids);
}
/**
* @param pageable pageable
* @return Page
*/
public Page<T> findAll(Pageable pageable) {
return getBaseDao().findAll(pageable);
}
/**
* 查寻全部
*
* @param spec spec
* @param pageable pageable
* @return Page
*/
public Page<T> findAll(Specification<T> spec, Pageable pageable) {
return getBaseDao().findAll(spec, pageable);
}
/**
* @param spec spec
* @return T
*/
public T findOne(Specification<T> spec) {
return getBaseDao().findOne(spec).orElse(null);
}
/**
* count.
*
* @return long
*/
public long count() {
return getBaseDao().count();
}
/**
* count.
*
* @param spec spec
* @return long
*/
public long count(Specification<T> spec) {
return getBaseDao().count(spec);
}
/**
* exists.
*
* @param id id
* @return boolean
*/
public boolean exists(I id) {
return getBaseDao().findById(id).isPresent();
}
/**
* save.
*
* @param entity entity
*/
public void save(T entity) {
getBaseDao().save(entity);
}
/**
* save.
*
* @param entities entities
*/
public void save(List<T> entities) {
getBaseDao().saveAll(entities);
}
/**
* update.
*
* @param entity entity
* @return T
*/
public T update(T entity) {
return getBaseDao().saveAndFlush(entity);
}
/**
* delete.
*
* @param id id
*/
public void delete(I id) {
getBaseDao().deleteById(id);
}
/**
* delete by ids.
*
* @param ids ids
*/
public void deleteByIds(List<I> ids) {
for (I id : ids) {
getBaseDao().deleteById(id);
}
}
/**
* delete.
*
* @param entities entities
*/
public void delete(T[] entities) {
List<T> tList = Arrays.asList(entities);
getBaseDao().deleteAll(tList);
}
/**
* delete.
*
* @param entities entities
*/
public void delete(Iterable<T> entities) {
getBaseDao().deleteAll(entities);
}
/**
* delete.
*
* @param entity entity
*/
public void delete(T entity) {
getBaseDao().delete(entity);
}
/**
* delete all.
*/
public void deleteAll() {
getBaseDao().deleteAll();
}
/**
* find list.
*
* @param spec spec
* @return list
*/
public List<T> findList(Specification<T> spec) {
return getBaseDao().findAll(spec);
}
/**
* find list.
*
* @param spec spec
* @param sort sort
* @return List
*/
public List<T> findList(Specification<T> spec, Sort sort) {
return getBaseDao().findAll(spec, sort);
}
/**
* flush.
*/
public void flush() {
getBaseDao().flush();
}
}用户UserServiceImpl
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
38import com.github.wenhao.jpa.Specifications;
import org.apache.commons.lang3.StringUtils;
import org.example.dao.IBaseDao;
import org.example.dao.IUserDao;
import org.example.entity.User;
import org.example.entity.vo.UserQueryBean;
import org.example.service.IUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
/**
* @author xiaoyuge
*/
public class UserServiceImpl extends BaseServiceImpl<User, Long> implements IUserService {
private final IUserDao userDao;
public UserServiceImpl(IUserDao userDao) {
this.userDao = userDao;
}
public IBaseDao<User, Long> getBaseDao() {
return this.userDao;
}
public Page<User> findPage(UserQueryBean user, PageRequest pageRequest) {
Specification<User> specification = Specifications.<User>and()
.like(StringUtils.isNotEmpty(user.getName()), "user_name", user.getName())
.like(StringUtils.isNotEmpty(user.getDescription()), "description", user.getDescription())
.build();
return this.getBaseDao().findAll(specification, pageRequest);
}
}
2.7 定义Controller层
1 |
|