pojo层 层 层 层 层 层 层 层 层 层 层 层 层 层 层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 + "]"; }}
dao层 层 层 层 层 层 层 层 层 层 层 层 层 层/** @Mapper 用于形容(做标记)数据层拜访接口,用于通知mybatis框架, 应用此注解形容的接口要由底层为创立实现类.在实现类中基于mybatis API实现与数据库的交互.这个类的对象最初会交给spring治理. FAQ 咱们增加了MyBatis-Spring-Boot-Starter这个依赖当前,此依赖外部做了什么? /@Mapperpublic interface GoodsDao {//还有一些企业这个GoodsDao的名字会定义为GoodsMapper @Insert("insert into tbgoods(name,remark,createdTime) values (#{name},#{remark},#{createdTime})") int insertGoods(Goods goods); @Update("update tbgoods set name=#{name},remark=#{remark} where id=#{id}") int updateGoods(Goods goods); /**基于商品id查问商品信息/ @Select("select from tbgoods where id={id}") Goods findById(Integer id); @Select("select from tb_goods") List<Goods> findGoods(); /** 基于id执行商品信息的删除,在mybatis中如果SQL映射语句比较简单 能够间接在dao办法上以注解形式进行定义. @param id 商品id @return 删除的行数/ @Delete("delete from tbgoods where id=#{id}") int deleteById(Integer id); /** 基于多个id执行商品删除业务 @param ids 可变参数,用于接管传入的商品id值 @return 删除行数 /int deleteObjects(@Param("ids")Integer...ids);}
service层 层 层/** 商品模块的业务层接口,负责具体业务规范的定义 @author pc */public interface GoodsService { Goods findById(Integer id); int saveGoods(Goods goods); int updateGoods(Goods goods); int deleteById(Integer id); List<Goods> findGoods();}
service层 imp实现层 imp实现层 imp实现层 imp实现层/*** 业务层对象,后续会在此对象中执行: 1)外围业务(例如,点击购买商品信息,要生成点单项信息,扣减库存,....) 2)扩大业务(例如,事务管制,权限管制,日志记录,。。。。) /@Service //是一个非凡的@Componentpublic class GoodsServiceImpl implements GoodsService { private static final Logger log= LoggerFactory.getLogger(GoodsServiceImpl.class); @Autowired private GoodsDao goodsDao; @Override public Goods findById(Integer id) { return goodsDao.findById(id); } @Override public int saveGoods(Goods goods) { goods.setCreatedTime(new java.util.Date()); return goodsDao.insertGoods(goods); } @Override public int updateGoods(Goods goods) { return goodsDao.updateGoods(goods); } @Override public List<Goods> findGoods() { return goodsDao.findGoods(); } @Override public int deleteById(Integer id) { long t1=System.currentTimeMillis(); int rows=goodsDao.deleteById(id); long t2=System.currentTimeMillis();//System.out.println(log.getClass().getName()); log.info("deleteById execute time : {}",(t2-t1)); return rows; }}
Controller 层 层 层 层 层 层 层 层 层 层 层@Controller //@Service,@Component@RequestMapping("/goods/")public class GoodsController { //has a+DI @Autowired private GoodsService goodsService; @RequestMapping("doFindById/{id}") public String doFindById(@PathVariable Integer id,Model model) { Goods goods=goodsService.findById(id); model.addAttribute("goods",goods); return "goods-update"; } @RequestMapping("doGoodsAddUI") public String doGoodsAddUI() { return "goods-add" } @RequestMapping("doSaveGoods") public String doSaveGoods(Goods goods) {//"name=aaa&&remark=aaaaaaaaa" goodsService.saveGoods(goods); return "redirect:/goods/doGoodsUI"; } @RequestMapping("doUpdateGoods") public String doUpdateGoods(Goods goods) { goodsService.updateGoods(goods); return "redirect:/goods/doGoodsUI"; } /** 基于商品id执行商品删除操作 @param id 商品id,肯定要与客户端传过来的参数id名雷同. @return 重定向到doGoodsUI rest格调:一种软件架构编码格调,其设计的目标次要是在异构零碎之间实现兼容(跨平台) rest格调的url的定义:{a}/{b}/{c},这里的a,b,c别离为变量 如果心愿办法参数的值来自rest格调的url, 能够应用@PathVariable注解对办法参数进行形容(办法参数名须要和url中{}外部变量名雷同)/ @RequestMapping("doDeleteById/{id}") public String doDeleteById(@PathVariable Integer id) { goodsService.deleteById(id); return "redirect:/goods/doGoodsUI"; }//这里redirect示意重定向,前面的第一个"/"默认地位为localhost:port/context-path/ @RequestMapping("doGoodsUI") public String doGoodsUI(Model model) {//ModelAndView中的model对象 //调用业务层办法获取商品信息 List<Goods> list= goodsService.findGoods(); //将数据存储到申请作用域 model.addAttribute("goods", list); return "goods";//viewname //FAQ: //1)返回的viewname会给谁?谁调用doGoodsUI办法就给谁(DispatcherServlet). //2)谁负责解析viewname?ViewResolver(ThymleafViewResolver) //3)viewname解析的后果会响应到哪里?(prefix+viewname+suffix会响应到客户端) }}
sql语句......<mapper namespace="com.cy.pj.goods.dao.GoodsDao"> <delete id="deleteObjects"> delete from tb_goods where id in <!-- (1,2,3,4,5) --> <foreach collection="array" open="(" close=")" separator="," item="id"> #{id} </foreach> </delete> </mapper>
html页面 页面 页面 页面 页面<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body> <h1>The Goods Page</h1><a th:href="@{/goods/doGoodsAddUI}">增加商品</a><table> <thead> <tr> <th>id</th> <th>name</th> <th>remark</th> <th>createdTime</th> <th colspan="2">operation</th> </tr> </thead> <tbody> <tr th:each="g:${goods}"> <td th:text="${g.id}">1</td> <td th:text="${g.name}">AAAAAAA</td> <td th:text="${g.remark}">AA....</td> <td th:text="${#dates.format(g.createdTime, 'yyyy/MM/dd HH:mm')}">2020/08/31</td> <td><a href="#" th:href="@{/goods/doDeleteById/{id}(id=${g.id})}">delete</a></td> <td><a href="#" th:href="@{/goods/doFindById/{id}(id=${g.id})}">update</a></td> </tr> </tbody></table></body></html>
sql 增加语句<!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 Add Page</h1> <form th:action="@{/goods/doSaveGoods}" method="post"> <ul> <li>name: <li><input type="text" name="name"> <li>remark: <li><textarea rows="3" cols="30" name="remark"></textarea> <li><input type="submit" value="Save Goods"> </ul> </form></body></html>
批改sql语句:<!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>
配置文件 # serverserver.port=80#server.servlet.context-path=/# close bannerspring.main.banner-mode=off# Spring DataSourcespring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=root# spring mybatismybatis.mapper-locations=classpath:/mapper/*/*.xml# spring thymleafspring.thymeleaf.cache=falsespring.thymeleaf.prefix=classpath:/templates/pages/spring.thymeleaf.suffix=.html# spring loglogging.level.com.cy=debug