基于Spring,MyBatis,SpringBoot,Thymeleaf技术实现商品模块的增删改查操做。html
1. MySQL(5.7) 2. JDK (1.8) 3. Maven (3.6.3) 4. STS(4.7.1)
打开mysql控制台,而后按以下步骤执行goods.sql文件。
第一步:登陆mysql。java
mysql –uroot –proot
第二步:设置控制台编码方式。mysql
set names utf8;
第三步:执行goods.sql文件(切记不要打开文件复制到mysql客户端运行)。web
source d:/goods.sql
其中goods.sql文件内容以下:spring
drop database if exists dbgoods; create database dbgoods default character set utf8; use dbgoods; create table tb_goods( id bigint primary key auto_increment, name varchar(100) not null, remark text, createdTime datetime not null )engine=InnoDB; insert into tb_goods values (null,'java','very good',now()); insert into tb_goods values (null,'mysql','RDBMS',now()); insert into tb_goods values (null,'Oracle','RDBMS',now()); insert into tb_goods values (null,'java','very good',now()); insert into tb_goods values (null,'mysql','RDBMS',now()); insert into tb_goods values (null,'Oracle','RDBMS',now()); insert into tb_goods values (null,'java','very good',now());
第一步:基于start.spring.io 建立项目并设置基本信息sql
第二步:建立项目时指定项目核心依赖数据库
第三步:项目建立之后分析其结构apache
#server server.port=80 #server.servlet.context-path=/ #spring datasource spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root #spring mybatis mybatis.mapper-locations=classpath:/mapper/*/*.xml #spring logging logging.level.com.cy=debug #spring thymeleaf spring.thymeleaf.prefix=classpath:/templates/pages/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false
其API架构设计,如图所示:segmentfault
从商品库查询商品信息,并将商品信息呈如今页面上,如图所示:
查询全部商品信息,其业务时序分析,如图所示:
定义Goods对象,用于封装从数据库查询到的商品信息。
package com.cy.pj.goods.pojo; import java.util.Date; public class Goods { private Long id;//id bigint primary key auto_increment private String name;//name varchar(100) not null private String remark;//remark text private Date createdTime;//createdTime datetime public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public Date getCreatedTime() { return createdTime; } public void setCreatedTime(Date createdTime) { this.createdTime = createdTime; } @Override public String toString() { return "Goods [id=" + id + ", name=" + name + ", remark=" + remark + ", createdTime=" + createdTime + "]"; } }
在GoodsDao接口中定义商品查询方法以及SQL映射,基于此方法及SQL映射获取全部商品信息。代码以下:
package com.cy.pj.goods.dao; import java.util.List; import org.apache.ibatis.annotations.Select; import com.cy.pj.goods.pojo.Goods; @Mapper public interface GoodsDao { @Select("select * from tb_goods") List<Goods> findGoods(); }
GoodsService接口及商品查询方法定义
package com.cy.pj.goods.service; import java.util.List; import com.cy.pj.goods.pojo.Goods; public interface GoodsService { List<Goods> findGoods(); }
GoodsService接口实现类GoodsServiceImpl定义及方法实现
package com.cy.pj.goods.service; import java.util.List; import com.cy.pj.goods.pojo.Goods; @Service public class GoodsServiceImpl implements GoodsService { @Autowired private GoodsDao goodsDao; @Override public List<Goods> findGoods(){ return goodsDao.findGoods(); } }
定义GoodsController类,并添加doGoodsUI方法,添加查询商品信息代码,并将查询到的商品信息存储到model,并返回goods页面。
package com.cy.pj.goods.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import com.cy.pj.goods.pojo.Goods; import com.cy.pj.goods.service.GoodsService; @Controller //@Service,@Component @RequestMapping("/goods/") public class GoodsController { //has a+di @Autowired private GoodsService goodsService; @RequestMapping("doGoodsUI") public String doGoodsUI(Model model) { //调用业务层方法获取商品信息 List<Goods> list= goodsService.findGoods(); //将数据存储到请求做用域 model.addAttribute("list", list); return "goods";//viewname } }
在templates/pages目录中添加goods.html页面,并在body中添加html元素,在运行内部使用thymeleaf标签属性获取数据,代码以下:
<table width="50%"> <thead> <th>id</th> <th>name</th> <th>remark</th> <th>createdTime</th> <th>operation</th> </thead> <tbody> <tr th:each="g:${list}"> <td th:text="${g.id}">1</td> <td th:text="${g.name}">MySQL</td> <td th:text="${g.remark}">DBMS</td> <td th:text="${#dates.format(g.createdTime, 'yyyy/MM/dd HH:mm')}">2020/07/03</td> <td><a>delete</a></td> </tr> </tbody> </table>
thymeleaf 是一种模板引擎,此引擎以html为模板,能够添加自定义标签属性,能够将服务端model中数据填充在页面上,而后实现与用于交互。其官网为thymeleaf.org
Goods页面上数据呈现分析:
首先,启动tomcat,而后在打开浏览器,在地址栏输入访问地址,获取服务端数据并进行呈现,如图所示:
从商品库查询商品信息后,点击页面上删除超连接,基于id删除当前行记录,如图所示:
在商品呈现页面,用户执行删除操做,其删除时序如图所示:
在GoodsDao接口中定义商品删除方法以及SQL映射,代码以下:
@Delete("delete from tb_goods where id=#{id}") int deleteById(Integer id);
在GoodsService接口中添加删除方法,代码以下:
int deleteById(Integer id);
在GoodsService的实现类GoodsServiceImpl中添加deleteById方法实现。代码以下。
@Override public int deleteById(Integer id) { long t1=System.currentTimeMillis(); int rows=goodsDao.deleteById(id); long t2=System.currentTimeMillis(); System.out.println("execute time:"+(t2-t1)); return rows; }
在GoodsController中的添加doDeleteById方法,代码以下:
@RequestMapping("doDeleteById/{id}") public String doDeleteById(@PathVariable Integer id){ goodsService.deleteById(id); return "redirect:/goods/doGoodsUI"; }
说明:Restful 风格为一种软件架构编码风格,定义了一种url的格式,其url语法为/a/b/{c}/{d},在这样的语法结构中{}为一个变量表达式。假如咱们但愿在方法参数中获取rest url中变量表达式的值,能够使用@PathVariable注解对参数进行描述。
在goods.html页面中添加删除超连接,如图所示:
Thymeleaf 官方th:href应用说明,如图所示:
删除操做中,客户端与服务端代码关联说明,如图所示:
首先,启动tomcat,而后在打开浏览器,在地址栏输入访问地址,获取服务端数据并进行呈现,接下来点击页面上的删除按钮,如图所示:
删除成功之后,的页面如图所示:
在Goods列表页面,添加添加按钮,进行添加页面,而后在添加页面输入商品相关信息,而后保存到数据库,如图所示:
商品添加页面,设计如图所示:
在商品添加页面,输入商品信息,而后提交到服务端进行保存,其时序分析如图所示:
在GoodsDao中添加用于保存商品信息的接口方法以及SQL映射,代码以下:
@Insert("insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},now())") int insertObject(Goods entity);
说明:当SQL语句比较复杂时,也能够将SQL定义到映射文件(xml文件)中。
在GoodsService接口中添加业务方法,用于实现商品信息添加,代码以下:
int saveGoods(Goods entity);
在GoodsSerivceImpl类中添加接口方法实现,代码以下:
@Override public int saveGoods(Goods entity) { int rows=goodsDao.insertObject(entity); return rows; }
在GoodsController类中添加用于处理商品添加请求的方法,代码以下:
@RequestMapping("doSaveGoods") public String doSaveGoods(Goods entity) { goodsService.saveGoods(entity); return "redirect:/goods/doGoodsUI"; }
在GoodsController类中添加用于返回商品添加页面的方法,代码以下:
@RequestMapping("doGoodsAddUI") public String doGoodsAddUI() { return "goods-add"; }
在templates的pages目录中添加goods-add.html页面,代码以下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> ul li {list-style-type: none;} </style> </head> <body> <h1>The Goods Add Page</h1> <form th:action="@{/goods/doSaveGoods}" method="post"> <ul> <li>name: <li><input type="text" name="name"> <li>remark: <li><textarea rows="5" cols="50" name="remark"></textarea> <li><input type="submit" value="Save"> </ul> </form> </body> </html>
在goods.html页面中添加,超连接能够跳转到添加页面,关键代码以下:
<a th:href="@{/goods/doGoodsAddUI}">添加商品</a>
第一步:启动web服务器,检测启动过程是否OK,假如没有问题进入下一步。
第二步:打开浏览器在地址里输入http://localhost/goods/doGood...),出现以下界面,如图所示:
第三步:在添加页面中填写表单,而后点击save按钮将表单数据提交到服务端,如图所示:
第四步:添加页面中表单数据提交过程分析,如图所示:
在商品列表页面,点击update选项,基于商品id查询当前行记录而后将其更新到goods-update页面,如图所示:
在update页面选中,修改商品信息,而后点击 update goods 将表单数据提交到服务端进行更新
基于id查询商品信息的时序设计
将goods-update页面中的数据提交到服务端进行更新的时序设计
在GoodsDao中添加基于id查询商品信息的方法及SQL映射,代码以下:
@Select("select * from tb_goods where id=#{id}") Goods findById(Integer id);
在GoodsDao中添加基于id执行Goods商品更新的方法及SQL映射,代码以下:
@Update("update tb_goods set name=#{name},remark=#{remark} where id=#{id}") int updateGoods(Goods goods);
在GoodsService 中添加基于id查询商品信息和更新商品信息的方法,代码以下:
Goods findById(Integer id); int updateGoods(Goods goods);
在GoodsServiceImpl中基于id查询商品信息和更新商品信息的方法,代码以下:
@Override public Goods findById(Integer id) { //..... return goodsDao.findById(id); }
@Override public int updateGoods(Goods goods) { return goodsDao.updateGoods(goods); }
在GoodsController中添加基于id查询商品信息的方法,代码以下:
@RequestMapping("doFindById/{id}") public String doFindById(@PathVariable Integer id,Model model) { Goods goods=goodsService.findById(id); model.addAttribute("goods",goods); return "goods-update"; }
在GoodsController中添加更新商品信息的方法,代码以下:
@RequestMapping("doUpdateGoods") public String doUpdateGoods(Goods goods) { goodsService.updateGoods(goods); return "redirect:/goods/doGoodsUI"; }
在templates目录中添加goods-update.html页面,代码设计以下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> ul li {list-style-type: none} </style> </head> <body> <h1>The Goods Update Page</h1> <form th:action="@{/goods/doUpdateGoods}" method="post"> <input type="hidden" name="id" th:value="${goods.id}"> <ul> <li>name: <li><input type="text" name="name" th:value="${goods.name}"> <li>remark: <li><textarea rows="3" cols="30" name="remark" th:text="${goods.remark}"></textarea> <li><input type="submit" value="Update Goods"> </ul> </form> </body> </html>
启动tomcat服务,访问商品列表页面,如图所示:
在列表页面,点击update选项,进入更新页面
在更新页面更新表单数据,而后提交,进入列表页面查看更新结果,如图所示:
本小节重点讲解了SpringBoot工程下MyBatis,SpringMVC,Thymeleaf技术的综合应用,重点理解其业务实现过程以及问题的解决过程。