All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.mvc.base.controller.BaseController Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.base4j.mvc.base.controller;

import com.base4j.mvc.base.entity.EntityUtil;
import com.base4j.mvc.base.exception.IllegalleControllerNameException;
import com.base4j.mvc.base.param.Param;
import com.base4j.mvc.base.service.BaseService;
import com.base4j.mvc.util.GenricUtil;
import com.base4j.mvc.util.Res;
import com.base4j.mybatis.base.QueryParams;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseController {

    public BaseController() {
        entityClass = GenricUtil.getGenericClass(this.getClass(), 0);
    }

    public static final String URL_PATH_SEPRATOR = "/";
    private Class entityClass;  //泛型T的类型

    @Autowired
    private BaseService service;

    @RequestMapping("/toView/{viewPath}")
    public ModelAndView toView(@PathVariable String viewPath, @RequestParam Map params) {
        ModelAndView model = new ModelAndView(viewPath);
        model.addAllObjects(params);
        return model;
    }

    private String getViewName(String className) {
        for (int i = 0; i < className.length(); i++) {
            if (i > 0 && Character.isUpperCase(className.charAt(i)) == true) {
                String viewPath = className.substring(0, i).toLowerCase();
                String viewName = className.substring(i, className.length() - 10).toLowerCase();
                return URL_PATH_SEPRATOR + viewPath + URL_PATH_SEPRATOR + viewName;
            }
        }
        throw new IllegalleControllerNameException();
    }

    @RequestMapping("/selectRelativeByPrimaryKey/{id}")
    @ResponseBody
    public Res selectRelativeByPrimaryKey(@PathVariable long id) {
        T record = service.selectRelativeByPrimaryKey(id);
        return Res.ok(record);
    }

    @RequestMapping("/selectPageByParams/{pageNum}/{pageSize}")
    @ResponseBody
    public Res selectPageByParams(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        Page page = service.selectPageByParams(pageNum, pageSize, queryParams);
        Map pageInfo = new HashMap();
        pageInfo.put("total", page.getTotal());
        pageInfo.put("pages", page.getPages());
        pageInfo.put("page", page);
        return Res.ok(pageInfo);
    }

    @RequestMapping("/selectPageRelativeByParams/{pageNum}/{pageSize}")
    @ResponseBody
    public Res selectPageRelativeByParams(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        Page page = service.selectPageRelativeByParams(pageNum, pageSize, queryParams);
        Map pageInfo = new HashMap();
        pageInfo.put("total", page.getTotal());
        pageInfo.put("pages", page.getPages());
        pageInfo.put("page", page);
        return Res.ok(pageInfo);
    }

    @RequestMapping("/selectListByParams")
    @ResponseBody
    public Res selectListByParams(@RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        List list = service.selectListByParams(queryParams);
        return Res.ok(list);
    }

    @RequestMapping("/selectListRelativeByParams")
    @ResponseBody
    public Res selectListRelativeByParams(@RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        List list = service.selectListRelativeByParams(queryParams);
        return Res.ok(list);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Res insert(T record) {
        service.insert(record);
        return Res.ok(record);
    }

    public Res insertList(List records) {
        int num = service.insertList(records);
        return Res.ok(num);
    }

    @RequestMapping("/updateSelectiveByPrimaryKey")
    @ResponseBody
    public Res updateSelectiveByPrimaryKey(T record) {
        service.updateSelectiveByPrimaryKey(record);
        return Res.ok(record);
    }

    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public Res updateByPrimaryKey(T record) {
        service.updateByPrimaryKey(record);
        return Res.ok(record);
    }

    @RequestMapping("/updateByParams")
    @ResponseBody
    public Res updateByParams(T record, @RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        int num = service.updateByParams(record, queryParams);
        return Res.ok(num);
    }

    @RequestMapping("/updateSelectiveByParams")
    @ResponseBody
    public Res updateSelectiveByParams(T record, @RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        int num = service.updateSelectiveByParams(record, queryParams);
        return Res.ok(num);
    }

    @RequestMapping("/deleteByPrimaryKey/{id}")
    @ResponseBody
    public Res deleteByPrimaryKey(@PathVariable Long id) {
        int num = service.deleteByPrimaryKey(id);
        return Res.ok(num);
    }

    @RequestMapping("/deleteByParams")
    @ResponseBody
    public Res deleteByParams(@RequestBody Param param) {
        QueryParams queryParams = param.toQueryParams(entityClass);
        int num = service.deleteByParams(queryParams);
        return Res.ok(num);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Res save(@RequestBody T record) {

        if (EntityUtil.getIdValule(record) == null) {
            return insert(record);
        } else {
            return updateByPrimaryKey(record);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy