首页>代码>IntelliJ IDEA下SpringBoot+Maven+springdata JPA+EasyUI整合实现增删改查及分页入门项目实例>/studySpringBootAndEasyUI/src/main/java/cn/temptation/web/PersonController.java
package cn.temptation.web;

import cn.temptation.dao.PersonDao;
import cn.temptation.model.Person;
import cn.temptation.util.TypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonDao personDao;

    /**
     * 跳转至列表页
     *
     * @return
     */
    @RequestMapping("/view")
    public String view() {
        // 跳转至列表页
        return "personlist";
    }

    /**
     * 查询列表信息
     *
     * @param searchcondition 查询条件
     * @param searchcontent   查询内容
     * @param page            页数
     * @param rows            每页记录数
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "searchcondition", required = false) String searchcondition,
                                    @RequestParam(value = "searchcontent", required = false) String searchcontent,
                                    @RequestParam(value = "page", required = false) Integer page,
                                    @RequestParam(value = "rows", required = false) Integer rows) {
        // 创建查询规格对象
        Specification<Person> specification = new Specification<Person>() {
            @Override
            public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = null;
                Path path = null;

                if (searchcondition != null && !"".equals(searchcondition)
                        && searchcontent != null && !"".equals(searchcontent)) {
                    switch (searchcondition) {
                        case "personname":      // 人员名称
                            path = root.get("personname");
                            predicate = cb.like(path, "%" + searchcontent + "%");
                            break;
                        case "personage":       // 人员年龄
                            path = root.get("personage");
                            if (TypeUtil.isNum(searchcontent)) {
                                predicate = cb.equal(path, Integer.parseInt(searchcontent));
                            }
                            break;
                    }
                }

                return predicate;
            }
        };

        Pageable pageable = new PageRequest(page - 1, rows, Sort.Direction.ASC, "personid");
        Page<Person> pagePerson = personDao.findAll(specification, pageable);

        // 获取rows
        List<Person> list = pagePerson.getContent();
        // 获取count
        Long count = pagePerson.getTotalElements();

        Map<String, Object> resultMap = new HashMap();
        resultMap.put("total", count);
        resultMap.put("rows", list);
        resultMap.put("success", true);

        return resultMap;
    }

    /**
     * 新增处理 和 修改处理
     *
     * @param person
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> personsave(Person person) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        personDao.save(person);
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 删除处理
     *
     * @param personid
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> persondelete(@RequestParam("id") String personid) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        personDao.deleteById(Integer.parseInt(personid));
        resultMap.put("success", true);
        return resultMap;
    }
}
最近下载更多
601601lmy  LV5 2023年10月20日
刘亦菲9527  LV15 2022年1月25日
c15042361021  LV4 2021年7月8日
woshicainiao2  LV4 2021年5月12日
caozongan  LV19 2021年3月10日
Qolmen  LV12 2020年12月22日
hidemoon  LV2 2020年11月25日
qa38113202  LV1 2020年9月14日
bianjun  LV1 2020年9月2日
hao895937469  LV4 2020年6月24日
最近浏览更多
killler 3月25日
暂无贡献等级
601601lmy  LV5 2023年10月20日
SDASDASDAD  LV1 2023年10月16日
漫步的海星  LV4 2023年9月21日
飞呀飞呀飞不放  LV7 2023年8月9日
hbsoft2008  LV16 2023年3月20日
xhjzsx0715  LV1 2023年3月11日
yohohero  LV1 2023年1月14日
liujiaxin666  LV1 2022年12月1日
内心向阳  LV4 2022年11月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友