wang-hao-jie
2021-11-11 47ae9468857e0a13d91fc5e8c126246b80cfda62
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
package cn.exrick.xboot.core.base;
 
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
 
import java.io.Serializable;
import java.util.List;
 
/**
 * @author Exrickx
 */
public abstract class XbootBaseController<E, ID extends Serializable> {
 
    /**
     * 获取service
     * @return
     */
    @Autowired
    public abstract XbootBaseService<E, ID> getService();
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取")
    public Result<E> get(@PathVariable ID id) {
 
        E entity = getService().get(id);
        return new ResultUtil<E>().setData(entity);
    }
 
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部数据")
    public Result<List<E>> getAll() {
 
        List<E> list = getService().getAll();
        return new ResultUtil<List<E>>().setData(list);
    }
 
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页获取")
    public Result<Page<E>> getByPage(PageVo page) {
 
        Page<E> data = getService().findAll(PageUtil.initPage(page));
        return new ResultUtil<Page<E>>().setData(data);
    }
 
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    public Result<E> save(E entity) {
 
        E e = getService().save(entity);
        return new ResultUtil<E>().setData(e);
    }
 
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "更新数据")
    public Result<E> update(E entity) {
 
        E e = getService().update(entity);
        return new ResultUtil<E>().setData(e);
    }
 
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delByIds(ID[] ids) {
 
        for (ID id : ids) {
            getService().delete(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}