BaseController.java
5.54 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
package com.bsth.controller;
import com.bsth.service.BaseService;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @param <T>
* @param <ID> 主键类型
* @author PanZhao
* @ClassName: BaseController
* @Description: TODO(基础的Controller实现)
* @date 2016年3月17日 下午12:44:06
*/
public class BaseController<T, ID extends Serializable> {
@Autowired
protected BaseService<T, ID> baseService;
/**
* @param @param map 查询条件
* @param @param page 页码
* @param @param size 每页显示数量
* @throws
* @Title: list
* @Description: TODO(多条件分页查询)
*/
@RequestMapping(method = RequestMethod.GET)
public Page<T> list(@RequestParam Map<String, Object> map,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(defaultValue = "id") String order,
@RequestParam(defaultValue = "DESC") String direction) {
// 允许多个字段排序,order可以写单个字段,也可以写多个字段
// 多个字段格式:{col1},{col2},{col3},....,{coln}
List<String> order_columns = Splitter.on(",").trimResults().splitToList(order);
// 多字段排序:DESC,ASC...
List<String> order_dirs = Splitter.on(",").trimResults().splitToList(direction);
if (order_dirs.size() == 1) { // 所有字段采用一种排序
if (null != order_dirs.get(0) && order_dirs.get(0).equals("ASC")) {
return baseService.list(map, new PageRequest(page, size, new Sort(Direction.ASC, order_columns)));
} else {
return baseService.list(map, new PageRequest(page, size, new Sort(Direction.DESC, order_columns)));
}
} else if (order_columns.size() == order_dirs.size()) {
List<Sort.Order> orderList = new ArrayList<>();
for (int i = 0; i < order_columns.size(); i++) {
if (null != order_dirs.get(i) && order_dirs.get(i).equals("ASC")) {
orderList.add(new Sort.Order(Direction.ASC, order_columns.get(i)));
} else {
orderList.add(new Sort.Order(Direction.DESC, order_columns.get(i)));
}
}
return baseService.list(map, new PageRequest(page, size, new Sort(orderList)));
} else {
throw new RuntimeException("多字段排序参数格式问题,排序顺序和字段数不一致");
}
}
/**
* @param @param map
* @throws
* @Title: list
* @Description: TODO(多条件查询)
*/
@RequestMapping(value = "/all", method = RequestMethod.GET)
public Iterable<T> list(@RequestParam Map<String, Object> map) {
return baseService.list(map);
}
/**
* @param @param map
* @throws
* @Title: list
* @Description: TODO(多条件查询)
*/
@RequestMapping(value = "/findAll", method = RequestMethod.GET)
public List<T> findAll(@RequestParam Map<String, Object> map,
@RequestParam(defaultValue = "id") String order,
@RequestParam(defaultValue = "DESC") String direction) {
String[] columns = order.split(","), directions = direction.split(",");
if (columns.length != directions.length) {
throw new IllegalArgumentException("不合法的排序参数");
}
List<Sort.Order> orders = new ArrayList<>();
for (int i = 0;i < columns.length;i++) {
String column = columns[i], direction1 = directions[i];
if ("DESC".equals(direction1)) {
orders.add(Sort.Order.desc(column));
} else if ("ASC".equals(direction1)) {
orders.add(Sort.Order.asc(column));
} else {
throw new IllegalArgumentException("不合法的排序参数");
}
}
return baseService.findAll(map, Sort.by(orders));
}
/**
* @param @param t
* @param @return 设定文件
* @return Map<String,Object> {status: 1(成功),-1(失败)}
* @throws
* @Title: save
* @Description: TODO(持久化对象)
*/
@RequestMapping(method = RequestMethod.POST)
public Map<String, Object> save(T t) {
return baseService.save(t);
}
/**
* @param @param id
* @throws
* @Title: findById
* @Description: TODO(根据主键获取单个对象)
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public T findById(@PathVariable("id") ID id) {
return baseService.findById(id);
}
/**
* @param @param id
* @throws
* @Title: delete
* @Description: TODO(根据主键删除对象)
*/
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public Map<String, Object> delete(@PathVariable("id") ID id) {
return baseService.delete(id);
}
}