共计 6509 个字符,预计需要花费 17 分钟才能阅读完成。
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 这个依赖当前, 此依赖外部做了什么?
/
@Mapper
public 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 // 是一个非凡的 @Component
public 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>
配置文件
# server
server.port=80
#server.servlet.context-path=/
# close banner
spring.main.banner-mode=off
# 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 thymleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
# spring log
logging.level.com.cy=debug
正文完
发表至: springboot
2020-10-07