BaseController.java 3.01 KB
package com.bsth.controller;

import com.bsth.service.BaseService;
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.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) {

        Direction d;
        if(null != direction && direction.equals("ASC"))
            d = Direction.ASC;
        else
            d = Direction.DESC;

        return baseService.list(map, PageRequest.of(page, size, new Sort(d, order)));
    }

    /**
     * @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  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);
    }

}