1.跳转到首页并且查找所有商品
@RequestMapping("doGoodsUI") public String doGoodsUI(Model model) { List<Goods> list = goodsService.findObject(); model.addAttribute("goods",list); return "goods"; }
2.业务层实现类
@Service public class GoodsServiceImpl implements GoodsService { @Autowired private GoodsDao goodsDao; @Override public List<Goods> findObject() { long start=System.currentTimeMillis(); List<Goods> list = goodsDao.findObjects(); long end=System.currentTimeMillis(); System.out.println("query time:"+(end-start)); return list; } @Override public int add(Goods goods) { return goodsDao.add(goods); } @Override public int update(Goods goods) { return goodsDao.update(goods); } @Override public Goods findById(Integer id) { return goodsDao.findById(id); }
控制层写具体实现
1.跳转到首页并且查找所有商品
@RequestMapping("doGoodsUI") public String doGoodsUI(Model model) { List<Goods> list = goodsService.findObject(); model.addAttribute("goods",list); return "goods"; }
2.删除商品
@RequestMapping("doDeleteById/{id}") // (@PathVariable Integer id)告诉服务器,id拿到的是从网页上同样叫id的数据 public String dodeletebyId(@PathVariable Integer id){ int delete = goodsDao.deleteById(id); //doGoodsUI前面没有加/的话,跳转的网址是替代了最后一个/后面的内容 return "redirect:/goods/doGoodsUI"; }
3.修改商品
(1)先将查找出来的商品显示在文本框中
@RequestMapping("doFindById/{id}") public String doFindByID(@PathVariable Integer id,Model model){ Goods goods = goodsService.findById(id); model.addAttribute("goods",goods); return "goods-update"; }
(2)实现修改
@RequestMapping("doUpdateGoods") public String doUpdateGoods(Goods goods){ goodsService.update(goods); return "redirect:/goods/doGoodsUI"; }
4.新增商品
@RequestMapping("doSaveGoods") public String doSaveGoods(Goods goods){ goodsService.add(goods); return "redirect:/goods/doGoodsUI"; }
前端采用html+thymeleaf模板代替jsp
1.thymeleaf的语法参考: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
2.each表示遍历拿到的数组,goods是从控制层拿到的model的名字
3.id,name和remark与数据库对应,date要格式化拿到数据,该语法是thymeleaf固定写法
<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')}">aa</td> <!-- <td><a rel="external nofollow" rel="external nofollow" rel="external nofollow" th: rel="external nofollow" ><button>删除</button></a></td>--> <td><a rel="external nofollow" rel="external nofollow" rel="external nofollow" th: rel="external nofollow" ><button>删除</button></a></td> <td><a rel="external nofollow" rel="external nofollow" rel="external nofollow" th: rel="external nofollow" ><button>修改</button></a></td> </tr>
4.新增商品界面
(1)标签里的name属性要和sql语句一致
(2)这里由于数据库中的id列设置了自增长,所以不需要id属性,createdTime列使用了now()获取当前时间,所以也不需要传值,所以在控制层的doUpdateGoods方法里可以使用封装好的Goods来接收从html拿到的参数
<form th:action="@{/goods/doSaveGoods}" method="post"> <ul> <li>name:<input type="text" name="name"></li> <li>remark:<textarea rows="3" cols="20" name="remark"></textarea></li> <li><input type="submit" value="Save Goods"></li> </ul> </form>
5.修改商品界面
(1)因为id列自增长,所以修改商品信息不需要id这一列,但传参数有需要一起传送过去,所以添加了一个输入框,默认设置为隐藏,将其value设置为id的值
<form th:action="@{/goods/doUpdateGoods}" method="post"> <input type="hidden" name="id" th:value="${goods.id}"> <ul> <li>name:<input type="text" name="name" th:value="${goods.name}"></li> <li>remark:<textarea rows="3" cols="20" name="remark" th:text="${goods.remark}"></textarea></li> <li><input type="submit" value="Update Goods"></li> </ul> </form>
以上就是Spring Boot实现简单的增删改查的详细内容,更多关于Spring Boot增删改查的资料请关注其它相关文章!