thymeleaf springboot CRUD (新手向)
前言
我是以我原来mybatis+druid+mysql做CRUD为基础https://www.jianshu.com/p/4b285b5b34f8,主要借鉴了https://blog.csdn.net/zhuyu19911016520/article/details/81537154。这个网址的没做分页,我的做了能在我的程序上运行的分页处理以及一些优化。其中,包括显示上一页与下一页(如果有的话),当前是第几页以及总共有多少页。在删除时不会删了最后一页的最后一个,直接跳到首页(这是不知为何的机制所致),而是对使用者友好的删除后还在当前页面(如果当前页面仍然有数据)。由于刚接触,所以在对于工具所提供的接口只会使用,背后的机制是不了解的。总之,网上找的代码大概意思能看懂,具体代码层面的工作流程需要通过修复一个一个bug,以及加些新功能或者优化一起学习。
放一下thymeleaf官网 https://www.thymeleaf.org/。如果英语差,可以看菜鸟教程。
项目目的
前后端共同实现mysql分页增删改查
文件结构
上代码
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
Controller
importcom.mybatis_druid_mysql.demo.Entity.Customer;
importcom.mybatis_druid_mysql.demo.Service.Service;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.data.domain.Page;
importorg.springframework.ui.ModelMap;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestParam;
importjava.util.Iterator;
@org.springframework.stereotype.Controller
@RequestMapping("customers")
publicclassController{
@Autowired
privateServiceservice;
@RequestMapping("edit")
publicStringedit(ModelMapmap,@RequestParam(value="pageNum",defaultValue="0")intpageNum,@RequestParam(value="pageSize",defaultValue="2")intpageSize,@RequestParam(defaultValue="0")intid){
//isAdd : 向前端页面返回一个是新增与编辑的标识
if(id>0){
map.addAttribute("isAdd",0);
map.addAttribute("customer",service.getById(id));
map.addAttribute("pageNum",pageNum);
}else{
map.addAttribute("isAdd",1);
map.addAttribute("customer",newCustomer());
map.addAttribute("pageNum",pageNum);
}
return"customer/edit";
}
@RequestMapping("goingToDel")
publicStringgoingToDel(ModelMapmap,@RequestParam(value="pageNum",defaultValue="0")intpageNum,@RequestParam(value="pageSize",defaultValue="2")intpageSize,@RequestParam(value="id")intid){
map.addAttribute("customer",service.getById(id));
map.addAttribute("pageNum",pageNum);
return"customer/goingToDel";
}
@RequestMapping("list")
publicStringlist(ModelMapmap,@RequestParam(value="pageNum",defaultValue="0")intpageNum,@RequestParam(value="pageSize",defaultValue="2")intpageSize) {
System.out.println("============================");
Page
System.out.println("总页数"+customers.getTotalPages());
System.out.println("当前页是:"+pageNum);
System.out.println("分页数据:");
Iterator
while(u.hasNext()){
System.out.println(u.next().toString());
}
map.addAttribute("customers",customers);
map.addAttribute("pageNum",pageNum);
return"customer/list";
}
//新增和编辑
@RequestMapping("save")
publicStringsave(ModelMapmap,@RequestParam(value="pageNum",defaultValue="0")intpageNum,@RequestParam(value="pageSize",defaultValue="2")intpageSize,@ModelAttributeCustomercustomer){
if(customer==null){
return"customer/list";
}
if(customer.getId()!=null&&customer.getId()>0){
service.edit(customer);
}else{
service.add(customer);
}
Page
map.addAttribute("customers",customers);
map.addAttribute("pageNum",pageNum);
return"customer/list";
}
@RequestMapping("del")
publicStringdel(ModelMapmap,@RequestParam(value="pageNum",defaultValue="0")intpageNum,@RequestParam(value="pageSize",defaultValue="2")intpageSize,@ModelAttributeCustomercustomer){
if(customer==null){
return"customer/list";
}
service.delete(customer.getId());
Page
Iterator
map.addAttribute("customers",customers);
map.addAttribute("pageNum",pageNum);
System.out.println(pageNum);
if(u.hasNext()){
System.out.println("1");
return"redirect:list?pageNum="+pageNum;
}
elseif(customers.getTotalPages()!=0) {
pageNum--;
System.out.println("2");
return"redirect:list?pageNum="+pageNum;
}
else{
System.out.println("3");
return"redirect:list?pageNum=0";
}
}
}
ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可。通过以下方法向页面传递参数:
publicModelMapaddAttribute(StringattributeName,ObjectattributeValue){...}
publicModelMapaddAttribute(ObjectattributeValue){...}
publicModelMapaddAllAttributes(CollectionattributeValues) {...}
publicModelMapaddAllAttributes(Map
thymeleaf配置
###ThymeLeaf??
spring.thymeleaf.mode=HTML5
#????????HTML,XMLTEXTJAVASCRIPT
spring.thymeleaf.encoding=UTF-8
#???????
spring.thymeleaf.content-type=text/html
#????,?????
spring.thymeleaf.cache=false
#?????false,?????????????
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix= classpath:/templates/spring.thymeleaf.suffix= .html
设置了thymeleaf的寻址地址以及文件格式。
Controller中return "customer/list";就是在thymeleaf的寻址地址基础上再寻址找到相应的页面。
至于return "redirect:list?pageNum=0"; 这是个重定位。相当于调用Controller方法 http://127.0.0.1:8080/customers/list?pageNum=0
edit.html