forked from wemakebug/FileUpload.Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseService.java
More file actions
234 lines (208 loc) · 5.73 KB
/
BaseService.java
File metadata and controls
234 lines (208 loc) · 5.73 KB
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
package com.zhangzhihao.FileUpload.Java.Service;
import com.zhangzhihao.FileUpload.Java.Dao.BaseDao;
import com.zhangzhihao.FileUpload.Java.Dao.Query;
import com.zhangzhihao.FileUpload.Java.Utils.PageResults;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
/**
* BaseService作为所有Service的基类,需要使用的话,需要先编写一个继承自此类的类
*
* @param <T> 实体类型
*/
@SuppressWarnings({"rawtypes", "unchecked"})
class BaseService<T> {
@Autowired
private BaseDao<T> baseDao;
private Class<T> modelClass;
public BaseService() {
modelClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
/**
* 保存对象
*
* @param model 需要添加的对象
*/
public void save(@NotNull final T model) throws Exception {
baseDao.save(model);
}
/**
* 批量保存对象
*
* @param modelList 需要增加的对象的集合
* 失败会抛异常
*/
public void saveAll(@NotNull final List<T> modelList) throws Exception {
baseDao.saveAll(modelList);
}
/**
* 删除对象
*
* @param model 需要删除的对象
* 失败会抛异常
*/
public void delete(@NotNull final T model) throws Exception {
baseDao.delete(model);
}
/**
* 批量删除对象
*
* @param modelList 需要删除的对象的集合
* 失败会抛异常
*/
public void deleteAll(@NotNull final List<T> modelList) throws Exception {
baseDao.deleteAll(modelList);
}
/**
* 按照id删除对象
*
* @param id 需要删除的对象的id
* 失败抛出异常
*/
public void deleteById(@NotNull final Serializable id) throws Exception {
baseDao.deleteById(modelClass, id);
}
/**
* 更新或保存对象
*
* @param model 需要更新的对象
* 失败会抛出异常
*/
public void saveOrUpdate(@NotNull final T model) throws Exception {
baseDao.saveOrUpdate(model);
}
/**
* 批量更新或保存对象
*
* @param modelList 需要更新或保存的对象
* 失败会抛出异常
*/
public void saveOrUpdateAll(@NotNull final List<T> modelList) throws Exception {
baseDao.saveOrUpdateAll(modelList);
}
/**
* 通过主键, 查询对象
*
* @param id 主键(Serializable)
* @return model
*/
public T getById(@NotNull final Serializable id) throws Exception {
return baseDao.getById(modelClass, id);
}
/**
* 获得全部
*
* @return List
*/
public List<T> getAll() throws Exception {
return baseDao.getAll(modelClass);
}
/**
* 分页查询
*
* @param currentPageNumber 页码
* @param pageSize 每页数量
* @return 查询结果
*/
public List<T> getListByPage(@NotNull final Integer currentPageNumber,
@NotNull final Integer pageSize)
throws Exception {
return baseDao.getListByPage(modelClass, currentPageNumber, pageSize);
}
/**
* 按条件分页
*
* @param currentPageNumber 页码
* @param pageSize 每页数量
* @param query 封装的查询条件
* @return 查询结果
*/
public PageResults<T> getListByPageAndQuery(@NotNull Integer currentPageNumber,
@NotNull Integer pageSize,
@NotNull Query query)
throws Exception {
return baseDao.getListByPageAndQuery(currentPageNumber, pageSize, query);
}
/**
* 获得数量 利用Count(*)实现
*
* @return 数量
*/
public int getCount() throws Exception {
return baseDao.getCount(modelClass);
}
/**
* 获得符合对应条件的数量 利用Count(*)实现
*
* @param query 查询条件
* @return 数量
*/
public int getCountByQuery(@NotNull final Query query) throws Exception {
return baseDao.getCountByQuery(query);
}
/**
* 执行Sql语句
*
* @param sql sql
* @param values 不定参数数组
* @return 受影响的行数
*/
public int executeSql(@NotNull final String sql, @NotNull final Object... values)
throws Exception {
return baseDao.executeSql(sql, values);
}
/**
* 通过jpql查询
*
* @param jpql jpql语句
* @param values 参数列表
* @return 受影响的行数
*/
public Object queryByJpql(@NotNull final String jpql, @NotNull final Object... values) {
return baseDao.queryByJpql(jpql, values);
}
/**
* 获得符合对应条件的数量 利用Count(*)实现
*
* @param jpql jpql查询条件
* @return 数量
*/
public int getCountByJpql(@NotNull final String jpql, @NotNull final Object... values) {
return baseDao.getCountByJpql(jpql, values);
}
/**
* 通过Jpql分页查询
*
* @param currentPageNumber 当前页
* @param pageSize 每页数量
* @param jpql jpql语句
* @param values jpql参数
* @return 查询结果
*/
public PageResults<Object> getListByPageAndJpql(@NotNull Integer currentPageNumber,
@NotNull Integer pageSize,
@NotNull final String jpql,
@NotNull Object... values) {
return baseDao.getListByPageAndJpql(currentPageNumber, pageSize, jpql, values);
}
/**
* 执行jpql语句
*
* @param jpql jpql语句
* @param values 参数列表
* @return 受影响的行数
*/
public int executeJpql(@NotNull final String jpql, @NotNull final Object... values) {
return baseDao.executeJpql(jpql, values);
}
/**
* refresh 刷新实体状态
*
* @param model 实体
*/
public void refresh(@NotNull T model) throws Exception {
baseDao.refresh(model);
}
}