BaseController.java
6.74 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
package com.viontech.fanxing.commons.base;
import com.github.pagehelper.PageInfo;
import com.viontech.keliu.util.JsonMessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.viontech.keliu.util.JsonMessageUtil.getErrorJsonMsg;
import static com.viontech.keliu.util.JsonMessageUtil.getSuccessJsonMsg;
/**
* Controller基类<br>
* 所有Controller都需要继承自该类
*
* @param <T>
*
* @author suman 2016年8月15日 下午1:59:19
*/
public abstract class BaseController<O extends BaseModel, T extends VoInterface<O>> {
/**
* 通用message
*/
public static final String MESSAGE_ADD_SUCCESS = "添加成功";
public static final String MESSAGE_ADD_ERROR = "addError";
public static final String MESSAGE_UPDATE_SUCCESS = "更新成功";
public static final String MESSAGE_UPDATE_ERROR = "updateError";
public static final String MESSAGE_DELETE_SUCCESS = "删除成功";
public static final String MESSAGE_DELETE_ERROR = "delError";
public static final String MESSAGE_COUNT_SUCCESS = "成功";
public static final String MESSAGE_COUNT_ERROR = "countError";
public static final String MESSAGE_SELECT_SUCCESS = "成功";
public static final String MESSAGE_SELECT_ERROR = "selError";
public static final String MESSAGE_LIST_SUCCESS = "成功";
public static final String MESSAGE_LIST_ERROR = "listError";
public static final String MESSAGE_PAGE_SUCCESS = "成功";
public static final String MESSAGE_PAGE_ERROR = "pageError";
public static final String MESSAGE_WEBROOT_NOTFOUND_ERROR = "webRootNotFoundError";
public static final String MESSAGE_FILE_UPLOAD_SUCCESS = "fileUploadSuccess";
public static final String MESSAGE_FILE_UPLOAD_ERROR = "fileUploadError";
public static final String MESSAGE_FILE_DOWNLOAD_ERROR = "fileDownloadError";
public static final String MESSAGE_FILE_NOTFOUND_ERROR = "fileNotFoundError";
public static final String MESSAGE_ID_NOT_EMPTY = "idNotEmpty";
public static final String MESSAGE_MODE_NOT_EXISTENCE = "modleNotExistence";
public static final int EXAMPLE_TYPE_SIMPLE = 0;
public static final int EXAMPLE_TYPE_NORMAL = 1;
public static final int EXAMPLE_TYPE_COUNT = 3;
public static final int EXAMPLE_TYPE_PAGE = 4;
/**
* slf4j 日志对象 用来记录log
*/
protected final Logger logger = LoggerFactory.getLogger(getClass());
/*@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(true,
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-MM", "HH:mm:ss"));
}*/
public static boolean isNotNull(Object object) {
if (object == null) {
return false;
}
if (object.toString().trim().isEmpty()) {
return false;
}
return true;
}
public static JsonMessageUtil.JsonMessage<Object> success() {
return JsonMessageUtil.getSuccessJsonMsg(MESSAGE_SELECT_SUCCESS, null);
}
public static <T> JsonMessageUtil.JsonMessage<T> success(T t) {
return JsonMessageUtil.getSuccessJsonMsg(MESSAGE_SELECT_SUCCESS, t);
}
/**
* 通用添加方法
*
* @param t 需要插入数据库的对象
*
* @return
*/
@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
public Object add(@RequestBody T t) {
getService().insertSelective(t.getModel());
return getSuccessJsonMsg(MESSAGE_ADD_SUCCESS, t);
}
/**
* 通用数量方法
*
* @param t 需要插入数据库的对象的条件
*
* @return
*/
@RequestMapping(value = "/count", method = RequestMethod.GET)
@ResponseBody
public Object count(T t) {
int result = getService().countByExample(getExample(t, EXAMPLE_TYPE_COUNT));
return getSuccessJsonMsg(MESSAGE_COUNT_SUCCESS, result);
}
/**
* 通用更新方法
*
* @param t 需要更新的对象及内容
*
* @return
*/
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
@ResponseBody
public Object update(@PathVariable(value = "id") Long id, @RequestBody T t) {
t.getModel().setId(id);
getService().updateByPrimaryKeySelective(t.getModel());
return getSuccessJsonMsg(MESSAGE_UPDATE_SUCCESS, t);
}
/**
* 通用删除方法
*
* @param id 需要删除的数据id
*
* @return
*/
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseBody
public Object del(@PathVariable(value = "id") Long id) {
if (id == null) {
return getErrorJsonMsg(MESSAGE_ID_NOT_EMPTY);
}
int result = getService().deleteByPrimaryKey(id);
if (result == 0) {
return getErrorJsonMsg(MESSAGE_MODE_NOT_EXISTENCE);
}
return getSuccessJsonMsg(MESSAGE_DELETE_SUCCESS, null);
}
/**
* 通用列表查询方法,将数据从数据库中全部查出
* saveFile
*
* @return
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public Object selOne(@PathVariable(value = "id") Long id) {
BaseModel t = getService().selectByPrimaryKey(id);
return getSuccessJsonMsg(MESSAGE_LIST_SUCCESS, t);
}
/**
* 通用分页查询方法,从数据库中查出一页数据
*
* @return
*/
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
public Object page(T t, @RequestParam(value = "page", defaultValue = "-1") int page, @RequestParam(value = "pageSize", defaultValue = "100") int pageSize, String sortName, String sortOrder) {
BaseExample baseExample = getExample(t, EXAMPLE_TYPE_PAGE);
if (isNotNull(sortOrder) && isNotNull(sortName)) {
baseExample.setOrderByClause(baseExample.getTableAlias() + "." + sortName + " " + sortOrder);
} else if (isNotNull(sortName) && !isNotNull(sortOrder)) {
baseExample.setOrderByClause(sortName);
}
if (page <= 0) {
List result = getService().selectByExample(baseExample);
return getSuccessJsonMsg(MESSAGE_SELECT_SUCCESS, result);
} else {
PageInfo pageInfo = getService().pagedQuery(baseExample, page, pageSize);
return getSuccessJsonMsg(MESSAGE_PAGE_SUCCESS, pageInfo);
}
}
/**
* 获取到执行各项操作需要的service
*
* @return
*/
protected abstract BaseService<O> getService();
/**
* 得到执行查询操作需要的查询条件
*
* @param t 查询条件存储对象
*
* @return 查询条件对象
*/
protected abstract BaseExample getExample(T t, int type);
}