thymeleaf springboot CRUD (新手向)

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">

4.0.0

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

com.vue

mybatis_druid_mysql

0.0.1-SNAPSHOT

mybatis_druid_mysql

mybatis_druid_mysql

1.8

org.springframework.boot

spring-boot-starter-data-jpa

org.mybatis.spring.boot

mybatis-spring-boot-starter

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

com.alibaba

druid-spring-boot-starter

1.1.14

org.projectlombok

lombok

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-maven-plugin

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("============================");

Pagecustomers=service.getUserList(pageNum,pageSize);

System.out.println("总页数"+customers.getTotalPages());

System.out.println("当前页是:"+pageNum);

System.out.println("分页数据:");

Iteratoru=customers.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);

       }

Pagecustomers=service.getUserList(pageNum,pageSize);

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());

Pagecustomers=service.getUserList(pageNum,pageSize);

Iteratoru=customers.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(Mapattributes){...}

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

新增、编辑客户

id

goingToDel.html

删除客户

list.html

客户列表



新增用户



id

name

age

used

操作

编辑  

删除

  • 首页

  • MongoDB索引文件破坏后导致查询错误的问题 BigBird2012 mongodb
    问题描述: MongoDB在非正常情况下关闭时,可能会导致索引文件破坏,造成数据在更新时没有反映到索引上。 解决方案:   使用脚本,重建MongoDB所有表的索引。 var names = db.getCollectionNames(); for( var i in names ){ var name = names[i]; print(name);
  • Javascript Promise bijian1013 JavaScriptPromise
            Parse JavaScript SDK现在提供了支持大多数异步方法的兼容jquery的Promises模式,那么这意味着什么呢,读完下文你就了解了。 一.认识Promises         “Promises”代表着在javascript程序里下一个伟大的范式,但是理解他们为什么如此伟大不是件简
  • [Zookeeper学习笔记九]Zookeeper源代码分析之Zookeeper构造过程 bit1129 zookeeper
       Zookeeper重载了几个构造函数,其中构造者可以提供参数最多,可定制性最多的构造函数是     public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, boolea
  • 【Java命令三】jstack bit1129 jstack
    jstack是用于获得当前运行的Java程序所有的线程的运行情况(thread dump),不同于jmap用于获得memory dump   [hadoop@hadoop sbin]$ jstack Usage: jstack [-l] <pid> (to connect to running process) jstack -F
  • jboss 5.1启停脚本 动静分离部署 ronin47
    以前启动jboss,往各种xml配置文件,现只要运行一句脚本即可。start nohup sh /**/run.sh -c servicename  -b ip -g  clustername   -u broatcast jboss.messaging.ServerPeerID=int  -Djboss.service.binding.set=p
  • UI之如何打磨设计能力? brotherlamp UIui教程ui自学ui资料ui视频
      在越来越拥挤的初创企业世界里,视觉设计的重要性往往可以与杀手级用户体验比肩。在许多情况下,尤其对于 Web 初创企业而言,这两者都是不可或缺的。前不久我们在《右脑革命:别学编程了,学艺术吧》中也曾发出过重视设计的呼吁。如何才能提高初创企业的设计能力呢?以下是 9 位创始人的体会。 1.找到自己的方式 如果你是设计师,要想提高技能可以去设计博客和展示好设计的网站如D-lists或
  • 三色旗算法 bylijinnan java算法
    import java.util.Arrays; /** 问题: 假设有一条绳子,上面有红、白、蓝三种颜色的旗子,起初绳子上的旗子颜色并没有顺序, 您希望将之分类,并排列为蓝、白、红的顺序,要如何移动次数才会最少,注意您只能在绳 子上进行这个动作,而且一次只能调换两个旗子。 网上的解法大多类似: 在一条绳子上移动,在程式中也就意味只能使用一个阵列,而不使用其它的阵列来
  • 警告:No configuration found for the specified action: \'s chiangfai configuration
    1.index.jsp页面form标签未指定namespace属性。 <!--index.jsp代码--> <%@taglib prefix="s" uri="/struts-tags"%> ... <s:form action="submit" method="post"&g
  • redis -- hash_max_zipmap_entries设置过大有问题 chenchao051 redishash
    使用redis时为了使用hash追求更高的内存使用率,我们一般都用hash结构,并且有时候会把hash_max_zipmap_entries这个值设置的很大,很多资料也推荐设置到1000,默认设置为了512,但是这里有个坑   #define ZIPMAP_BIGLEN 254 #define ZIPMAP_END 255     /* Return th
  • select into outfile access deny问题 daizj mysqltxt导出数据到文件
    本文转自:http://hatemysql.com/2010/06/29/select-into-outfile-access-deny%E9%97%AE%E9%A2%98/ 为应用建立了rnd的帐号,专门为他们查询线上数据库用的,当然,只有他们上了生产网络以后才能连上数据库,安全方面我们还是很注意的,呵呵。 授权的语句如下: grant select on armory.* to rn
  • phpexcel导出excel表简单入门示例 dcj3sjt126com PHPExcelphpexcel
    <?php error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE);   if (PHP_SAPI == 'cli') die('This example should only be run from a Web Brows
  • 美国电影超短200句 dcj3sjt126com 电影
    1. I see. 我明白了。2. I quit! 我不干了!3. Let go! 放手!4. Me too. 我也是。5. My god! 天哪!6. No way! 不行!7. Come on. 来吧(赶快)8. Hold on. 等一等。9. I agree。 我同意。10. Not bad. 还不错。11. Not yet. 还没。12. See you. 再见。13. Shut up!
  • Java访问远程服务 dyy_gusi httpclientwebservicegetpost
        随着webService的崛起,我们开始中会越来越多的使用到访问远程webService服务。当然对于不同的webService框架一般都有自己的client包供使用,但是如果使用webService框架自己的client包,那么必然需要在自己的代码中引入它的包,如果同时调运了多个不同框架的webService,那么就需要同时引入多个不同的clien
  • Maven的settings.xml配置 geeksun settings.xml
    settings.xml是Maven的配置文件,下面解释一下其中的配置含义: settings.xml存在于两个地方: 1.安装的地方:$M2_HOME/conf/settings.xml 2.用户的目录:${user.home}/.m2/settings.xml 前者又被叫做全局配置,后者被称为用户配置。如果两者都存在,它们的内容将被合并,并且用户范围的settings.xml优先。
  • ubuntu的init与系统服务设置 hongtoushizi ubuntu
    转载自:  http://iysm.net/?p=178 init Init是位于/sbin/init的一个程序,它是在linux下,在系统启动过程中,初始化所有的设备驱动程序和数据结构等之后,由内核启动的一个用户级程序,并由此init程序进而完成系统的启动过程。 ubuntu与传统的linux略有不同,使用upstart完成系统的启动,但表面上仍维持init程序的形式。 运行
  • 跟我学Nginx+Lua开发目录贴 jinnianshilongnian nginxlua
    使用Nginx+Lua开发近一年的时间,学习和实践了一些Nginx+Lua开发的架构,为了让更多人使用Nginx+Lua架构开发,利用春节期间总结了一份基本的学习教程,希望对大家有用。也欢迎谈探讨学习一些经验。    目录 第一章 安装Nginx+Lua开发环境 第二章 Nginx+Lua开发入门 第三章 Redis/SSDB+Twemproxy安装与使用 第四章 L
  • php位运算符注意事项 home198979 位运算PHP&
    $a = $b = $c = 0; $a & $b = 1; $b | $c = 1  问a,b,c最终为多少?   当看到这题时,我犯了一个低级错误,误 以为位运算符会改变变量的值。所以得出结果是1 1 0 但是位运算符是不会改变变量的值的,例如: $a=1;$b=2; $a&$b;  这样a,b的值不会有任何改变
  • Linux shell数组建立和使用技巧 pda158 linux
    1.数组定义   [chengmo@centos5 ~]$ a=(1 2 3 4 5)   [chengmo@centos5 ~]$ echo $a   1   一对括号表示是数组,数组元素用“空格”符号分割开。    2.数组读取与赋值   得到长度:   [chengmo@centos5 ~]$ echo ${#a[@]}   5   用${#数组名[@或
  • hotspot源码(JDK7) ol_beta javaHotSpotjvm
    源码结构图,方便理解:   ├─agent                            Serviceab
  • Oracle基本事务和ForAll执行批量DML练习 vipbooks oraclesql
    基本事务的使用: 从账户一的余额中转100到账户二的余额中去,如果账户二不存在或账户一中的余额不足100则整笔交易回滚 select * from account; -- 创建一张账户表 create table account( -- 账户ID id number(3) not null, -- 账户名称 nam