第五天的代碼增刪改查

pojo層 層 層 層 層 層 層 層 層 層 層 層 層 層 層
//@AllArgsConstructor //添加基於所有屬性構建的構造函數
//@NoArgsConstructor //添加無參構造函數
@ToString
@Getter
@Setter
//@Data //添加此註解會在Goods類中自動添加set,get,toString等方法
public class Goods {//Goods.class
    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
}//lombok底層基於字節碼技術爲類添加相關方法或屬性
dao層 層 層 層 層 層 層 層 層 層 層 層 層 層
@Mapper
public interface GoodsDao {//這個接口在運行時系統底層會產生其實現類,但沒有源碼(只有字節碼).
     @Update("update tb_goods set name=#{name},remark=#{remark} where id=#{id} ")
     int updateGoods(Goods goods);
     @Insert("insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},#{createdTime})")
     int insertGoods(Goods goods);
     @Select("select id,name,remark,createdTime from tb_goods where id=#{id}")
     Goods findById(Integer id);
     @Select("select id,name,remark,createdTime from tb_goods")
     List<Goods> findGoods();
     @Delete("delete from tb_goods where id=#{id}")
     int deleteById(Integer id);
}
service 層 層 層 層 層 層 層 層 層 層 層 層
/**
 *  商品模塊業務接口:
 *  負責:
 * 1)核心業務
 * 2)拓展業務(記錄日誌,緩存,事務控制,權限控制,...)
 */
public interface GoodsService {
      Goods findById(Integer id);
      int updateGoods(Goods goods);
      int saveGoods(Goods goods);
      /**
 *     查詢全部商品信息
 * @return
 */
      List<Goods> findGoods();
      int deleteById(Integer id);
}
service imp實現層 實現層 實現層 實現層 實現層
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    //private static final Logger log=LoggerFactory.getLogger(GoodsServiceImpl.class);
    @Override
    public List<Goods> findGoods() {
        long t1=System.currentTimeMillis();
        List<Goods> list=goodsDao.findGoods();
        long t2=System.currentTimeMillis();
        log.info("findGoods().time:{}",(t2-t1));
        return list;
    }
    @Override
    public Goods findById(Integer id) {
        return goodsDao.findById(id);
    }
    @Override
    public int updateGoods(Goods goods) {
        // TODO Auto-generated method stub
        return goodsDao.updateGoods(goods);
    }
    @Override
    public int saveGoods(Goods goods) {
        // TODO Auto-generated method stub
        goods.setCreatedTime(new java.util.Date());
        return goodsDao.insertGoods(goods);
    }
    @Override
    public int deleteById(Integer id) {
        try{Thread.sleep(5000);}catch(Exception e) {}
        return goodsDao.deleteById(id);
    }
}
Controller 層 層 層 層 層 層 層 層 層
@Controller
@RequestMapping("/goods/")
public class GoodsController {
     @Autowired
     private GoodsService goodsService;
     @RequestMapping("doGoodsAddUI")
     public String doGoodsAddUI() {
         return "goods-add";
     }
     @RequestMapping("doFindById/{id}")
     public String doFindById(@PathVariable Integer id,Model model) {
         Goods goods=goodsService.findById(id);
         model.addAttribute("goods",goods);
         return "goods-update";//此view由誰解析?ViewResolver(ThymeleafViewResolver)
         //這裏的ThymeleafViewResolver作了什麼?
         //1)在viewname的基礎上添加前綴和後綴(/templates/pages/goods-update.html),並找到對應的view(真正的頁面對象)
         //2)將model中的數據取出,而後填充到view上(/templates/pages/goods-update.html)
         //3)將view交給DispatcherServlet
     }
     @RequestMapping("doUpdateGoods")
     public String doUpdateGoods(Goods goods) {
         goodsService.updateGoods(goods);
         return "redirect:/goods/doGoodsUI";
     }
     @RequestMapping("doSaveGoods")
     public String doSaveGoods(Goods goods) {
         goodsService.saveGoods(goods);
         return "redirect:/goods/doGoodsUI";
     }
     @RequestMapping("doDeleteById/{id}")
     public String doDeleteById(@PathVariable Integer id) {
         goodsService.deleteById(id);
         return "redirect:/goods/doGoodsUI";
     }
     @RequestMapping("doGoodsUI")
     public String doFindGoods(Model model)throws Exception {
         //Thread.sleep(7000);
         List<Goods> list=goodsService.findGoods();
         model.addAttribute("list", list);
         return "goods";
     }
}
goods 層 層 層 層 層 層
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<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:${list}">
            <td th:text="${g.id}">1</td>
            <td th:text="${g.name}">AAA</td>
            <td th:text="${g.remark}">AAAAA</td>
            <td th:text="${#dates.format(g.createdTime,'yyyy/MM/dd HH:mm')}">2020/09/01</td>
            <td><a th:href="@{/goods/doDeleteById/{id}(id=${g.id})}">delete</a></td>
            <td><a th:href="@{/goods/doFindById/{id}(id=${g.id})}">update</a></td>
        </tr>
    </tbody>
 </table>
</body>
</html>
goods-add 添加代碼
<!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 cols="30" rows="5" name="remark"></textarea>
        <li><input type="submit" value="Save Goods">
     </ul>
  </form>
</body>
</html>
goods-update 修改代碼
<!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 cols="30" rows="5" 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=/
spring.main.banner-mode=console
# 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 thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
#spring actuator
management.endpoints.web.exposure.include=*
#Spring log
logging.level.com.cy=debug
相關文章
相關標籤/搜索