===新手入门指南===
新建goods包,并在goods包下创建model、repository、service、controller四个包,在/WEB-INF/views下创建goods文件夹
1.创建goods商品表结构
代码语言:javascript复制 CREATE TABLE `goods` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`number` varchar(255) NOT NULL COMMENT '商品编号',
`name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`price` double(11,0) NOT NULL,
`total` int(11) NOT NULL COMMENT '商品数量',
`onsaletime` datetime DEFAULT NULL COMMENT '上架时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
2.创建与goods表对应的实体
代码语言:javascript复制 @Entity
@Table(name = "goods")
public class Goods {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键由数据库自动生成
private Integer id;
@NotNull(message = "商品编号不能为空")
@Column
private String name;//商品名
@Column
private String number;//商品编号
@Column
private Double price;//商品价格
@Column
private Integer total;//商品数量
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column
private Date onSaleTime;//商品上架时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
@JsonSerialize(using = CustomDateTimeSerializer.class)
public Date getOnSaleTime() {
return onSaleTime;
}
public void setOnSaleTime(Date onSaleTime) {
this.onSaleTime = onSaleTime;
}
}
3.创建针对Goods实体的持久化操作Repository
代码语言:javascript复制 @Repository
public interface GoodsRepository extends GPJPARepository<Goods,Integer>
4.创建GoodsService
代码语言:javascript复制 @Service
public class GoodsService {
}
5.创建GoodsController
代码语言:javascript复制 @Controller
@RequestMapping("/goods")
public class GoodsController {
}
6.实现商品列表的显示
在/WEB-INF/views/goods下新建index.jsp,在GoodsController中添加进入index页面的方法,在GoodsService下添加列出所有商品的方法
GoodsController:
代码语言:javascript复制 @Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired//自动注入goodsService
GoodsService goodsService;
@Autowired
GoodsRepository goodsRepository;
@RequestMapping("/index")
public void index(Model model,String number) {
List<Goods> goodsList = goodsService.findAllGoods();
model.addAttribute("goodsList",goodsList);
}
}
GoodsService:
代码语言:javascript复制 @Service
public class GoodsService {
@Autowired//自动注入goodsRepository
GoodsRepository goodsRepository;
public List<Goods> findAllGoods() {
return goodsRepository.findAll();
}
}
index.jsp:(table1位table,tbody1位tbody,thead1为thead,tr1为tr,th1为th,td1为td)
代码语言:javascript复制 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>商品列表</title>
</head>
<body>
商品列表
<a href="creategoods">添加商品</a>
<table1>
<thead1>
<th1>商品编号</th1>
<th1>商品名称</th1>
<th1>商品价格</th1>
<th1>商品数量</th1>
<th1>上架时间</th1>
<th1>操作</th>
</thead1>
<tbody1>
<c:forEach items="${goodsList}" var="goods">
<tr1>
<td1>${goods.number}</td1>
<td1>${goods.name}</td1>
<td1>${goods.price}</td1>
<td1>${goods.total}</td1>
<td1><fmt:formatDate value="${goods.onSaleTime}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate> </td1>
<td1><a href="updategoods?goodsId=${goods.id}">编辑</a><a href="deletegoods?goodsId=${goods.id}">删除</a></td1>
</tr1>
</c:forEach>
</tbody1>
</table1>
</body>
</html>
7.实现添加商品
在/WEB-INF/views/goods下新建creategoods.jsp,GoodsController下新建进入creategoods页面的方法,以及接收提交数据并保存商品信息的方法,在GoodsService中添加保存商品的方法
creategoods.jsp:
代码语言:javascript复制 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加商品</title>
</head>
<body>
<form action="creategoodssubmit" method="post">
<table1>
<tr1><th1>商品编号</th1><td1><input name="number" type="text"></td1></tr1>
<tr1><th1>商品名称</th1><td1><input name="name" type="text"></td1></tr1>
<tr1><th1>商品价格</th1><td1><input name="price" type="number"></td1></tr1>
<tr1><th1>商品数量</th1><td1><input name="total" type="number"></td1></tr1>
<tr1><th1>上架时间</th1><td1><input name="onSaleTime" type="datetime"></td1></tr1>
</table1>
<button type="submit">提交</button>
</form>
</body>
</html>
GoodsService:
代码语言:javascript复制 public Goods saveGoods(Goods goods) {
if (goods.getId() == null) {
return goodsRepository.saveAndFlush(goods);
} else {
Goods one = goodsRepository.findOne(goods.getId());
BeanUtils.copyProperties(goods,one,"id");//实际应该将goods实例中的非空属性copy至one实例
goodsRepository.saveAndFlush(one);
return one;
}
}
GoodsController:
代码语言:javascript复制 @RequestMapping("/creategoods")
public String createGoods() {
return "/goods/creategoods";
}
@RequestMapping("/creategoodssubmit")
public String createGoodsSubmit(Goods goods) {
goodsService.saveGoods(goods);
return "redirect:/goods/index";
}
9.实现商品的编辑
在/WEB-INF/views/goods下新建updategoods.jsp,GoodsController下新建进入updategoods页面的方法,以及接收提交数据并保存商品信息的方法 updategoods.jsp:
代码语言:javascript复制 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加商品</title>
</head>
<body>
<form action="creategoodssubmit" method="post">
<table1>
<tr1><th1>商品编号</th1><td1><input name="number" type="text" value="${goods.number}></td1></tr1>
<tr1><th1>商品名称</th1><td1><input name="name" type="text" value="${goods.name}></td1></tr1>
<tr1><th1>商品价格</th1><td1><input name="price" type="number" value="${goods.price}></td1></tr1>
<tr1><th1>商品数量</th1><td1><input name="total" type="number" value="${goods.total}></td1></tr1>
<tr1><th1>上架时间</th1><td1><input name="onSaleTime" type="datetime" value="<fmt:formatDate value='${goods.onSaleTime}' pattern='yyyy-MM-dd HH:mm:ss'></fmt:formatDate>"></td1></tr1>
</table1>
<button type="submit">提交</button>
</form>
</body>
</html>
GoodsController:
代码语言:javascript复制 @RequestMapping("/updategoods")
public String updateGoods(Integer goodsId,Model model) {
model.addAttribute(goodsRepository.findOne(goodsId));
return "/goods/updategoods";
}
@RequestMapping("/updategoodssubmit")
public String updateGoodsSubmit(Goods goods) {
goodsService.saveGoods(goods);
return "redirect:/goods/index";
}
10.实现商品的删除
在GoodsController中添加删除商品的方法
代码语言:javascript复制 @RequestMapping("/deletegoods")
public String deleteGoods(Integer goodsId) {
goodsRepository.delete(goodsId);
return "redirect:/goods/index";
}
11.实现商品的编号检索
在index.jsp中添加检索商品信息的表单,在GoodsRepository中添加通过编号检索商品的查询方法,修改GoodsController中进入商品详情界面的方法
index.jsp
代码语言:javascript复制 <form action="index" method="post">
商品编号:<input type="text" name="number" value="${number}">
<button type="submit">查询</button>
</form>
GoodsRepository:
代码语言:javascript复制 public List<Goods> findByNumber(String number);
GoodsController:
代码语言:javascript复制 @RequestMapping("/index")
public void index(Model model,String number) {
List<Goods> goodsList = new ArrayList<Goods>();
if (StringUtils.isNotBlank(number)) {
goodsList = goodsRepository.findByNumber(number);
} else {
goodsList = goodsService.findAllGoods();
}
model.addAttribute("goodsList",goodsList);
model.addAttribute("number",number);
}