首页>代码>IntelliJ IDEA下SpringBoot+Maven+Spring Data JPA+Layui整合实现增删改查及分页的单表项目实例>/studyLayui/src/main/java/cn/temptation/web/CategoryController.java
package cn.temptation.web; import cn.temptation.dao.CategoryDao; import cn.temptation.domain.Category; 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.ui.Model; 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.ArrayList; import java.util.HashMap; import java.util.Map; @Controller public class CategoryController { @Autowired private CategoryDao categoryDao; @RequestMapping("/category") public String index() { return "category"; } @RequestMapping("/category_list") @ResponseBody public Map<String, Object> categoryList(@RequestParam Map<String, Object> queryParams) { Map<String, Object> result = new HashMap<>(); try { Integer page = Integer.parseInt(queryParams.get("page").toString()); Integer limit = Integer.parseInt(queryParams.get("limit").toString()); String keyword = (String) queryParams.get("keyword"); // 创建查询规格对象 Specification<Category> specification = (Root<Category> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> { Predicate predicate = null; if (keyword != null && !"".equals(keyword)) { Path path = root.get("categoryname"); predicate = cb.like(path, "%" + keyword + "%"); } return predicate; }; Pageable pageable = PageRequest.of(page - 1, limit, Sort.Direction.ASC, "categoryid"); Page<Category> categories = categoryDao.findAll(specification, pageable); result.put("code", 0); result.put("msg", "查询OK"); result.put("count", categories.getTotalElements()); result.put("data", categories.getContent()); } catch (Exception e) { e.printStackTrace(); result.put("code", 500); result.put("msg", "服务器内部错误"); result.put("count", 0); result.put("data", new ArrayList()); } return result; } @RequestMapping("/category_delete") @ResponseBody public Integer categoryDelete(@RequestParam String categoryid) { try { categoryDao.deleteById(Integer.parseInt(categoryid)); return 0; } catch (Exception e) { e.printStackTrace(); } return -1; } @RequestMapping("/category_view") public String view(Integer categoryid, Model model) { Category category = new Category(); if (categoryid != null) { category = categoryDao.getOne(categoryid); } model.addAttribute("category", category); return "category_view"; } @RequestMapping("/category_update") @ResponseBody public Integer categoryUpdate(Category category) { try { categoryDao.save(category); return 0; } catch (Exception e) { e.printStackTrace(); } return -1; } }
最近下载更多
oulingqiao LV13
2023年12月12日
interface LV22
2023年5月11日
hbsoft2008 LV16
2023年3月20日
lyws1986 LV17
2023年3月13日
微信网友_6368711690080256 LV3
2023年2月28日
swx777019 LV3
2022年6月30日
banzhenghao LV7
2022年5月7日
Hachi6 LV13
2021年12月18日
采暖11 LV11
2021年11月23日
258000gan LV1
2021年11月12日
最近浏览更多
流水本无情 LV9
3月24日
oulingqiao LV13
2023年12月12日
漫步的海星 LV4
2023年9月21日
cwb6357123 LV6
2023年9月21日
张朕朕 LV3
2023年9月10日
淡心伤 LV11
2023年8月2日
hhvivi LV2
2023年7月2日
遗留问题 LV2
2023年6月9日
skook7 LV2
2023年6月1日
hbsoft2008 LV16
2023年3月20日