Spring Boot小結——增.刪.改.查瞭解一下 將數據庫商品數據進行-增.刪.改.查 一、創建項目並添加依賴 二、業務實現 三、易錯分析

寫在前面:2020年面試必備的Java後端進階面試題總結了一份複習指南在Github上,內容詳細,圖文並茂,有需要學習的朋友可以Star一下!
GitHub地址:https://github.com/abel-max/Java-Study-Note/tree/master

將數據庫商品數據進行-增.刪.改.查

一、創建項目並添加依賴

*創建項目並設置基本信息

*指定項目核心依賴

*項目結構

*項目配置文件

二、業務實現

*Pojo類定義

*Dao接口方法及映射定義

package com.cy.pj.goods.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.cy.pj.goods.pojo.Goods;
@Mapperpublic interface GoodsDao {    //查詢
    @Select("select * from tb_goods")
    List<Goods> findGoods();    //刪除
    @Delete("delete from tb_goods where id=#{id}")
    int deleteById(Integer id);    //添加
    @Insert("insert into tb_goods(name,remark,createdTime) value(#{name},#{remark},now())")
    int insertObject(Goods entity);    //修改
    @Select("select * from tb_goods where id=#{id}")
    Goods findById(Integer id);    @Update("update tb_goods set name=#{name},remark=#{remark} where id=#{id} ")
    int updateGoods(Goods goods);}

*Service接口方法定義及實現

package com.cy.pj.goods.service;
import java.util.List;
import com.cy.pj.goods.pojo.Goods;
public interface GoodsService {
    //查詢
    List<Goods> findGoods();
    //刪除
    int deleteById(Integer id);    
    //添加
    int saveGoods(Goods entity);
    //修改
    Goods findById(Integer id);
    int updateGoods(Goods goods);
}

*定義接口實現類GoodsServiceImpl及方法實現

package com.cy.pj.goods.service.impl;
import java.util.List;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.cy.pj.goods.dao.GoodsDao;import com.cy.pj.goods.pojo.Goods;import com.cy.pj.goods.service.GoodsService;@Servicepublic class GoodsServiceImpl implements GoodsService {    @Autowired    private GoodsDao goodsDao;    @Override  //查詢    public List<Goods> findGoods() {        return goodsDao.findGoods();
    }    @Override  //刪除    public int deleteById(Integer id) {
        int rows = goodsDao.deleteById(id);
        return rows;
    }    @Override  //添加    public int saveGoods(Goods entity) {
        int rows = goodsDao.insertObject(entity);
        return rows;
    }    @Override  //修改    public Goods findById(Integer id) {                return goodsDao.findById(id);
    }    @Override    public int updateGoods(Goods goods) {        
        return goodsDao.updateGoods(goods);
    }    }

*Controller對象方法定義及實現

package com.cy.pj.goods.controller;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import com.cy.pj.goods.pojo.Goods;import com.cy.pj.goods.service.GoodsService;@Controller@RequestMapping("/goods/")
public class GoodsController {    @Autowired    private GoodsService goodsService;    @RequestMapping("doGoodsUI")  //查詢
    public String doGoodsUI(Model model) {        List<Goods> list = goodsService.findGoods();        model.addAttribute("list", list);
        return "goods";        
    }        @RequestMapping("doDeleteById/{id}")   //刪除
    public String doDeleteById(@PathVariable Integer id) {        goodsService.deleteById(id);        return "redirect:/goods/doGoodsUI";        
    }        @RequestMapping("doSaveGoods")   //添加
    public String doSaveGoods(Goods entity) {        goodsService.saveGoods(entity);        return "redirect:/goods/doGoodsUI";        
    }    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI() {        return "goods-add";        
    }        @RequestMapping("doUpdateGoods")   //修改
    public String doUpdateGoods(Goods goods) {        goodsService.updateGoods(goods);        return "redirect:/goods/doGoodsUI";        
    }    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model) {        Goods goods = goodsService.findById(id);        model.addAttribute("goods", goods);
        return "goods-update";        
    }}

*設計Goods列表頁面及實現

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>The Goods Page</h1>
    <table width="50%">
    <a th:href="@{/goods/doGoodsAddUI}">添加商品</a>  
      <thead>
         <th>id</th>
         <th>name</th>
         <th>remark</th>
         <th>createdTime</th>
         <th colspan="2">operation</th>
       </thead>
       <tbody>
         <tr th:each="g:${list}">
           <td th:text="${g.id}">1</td>
           <td th:text="${g.name}">fbb</td>
           <td th:text="${g.remark}">fbbbb</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.html頁面

<!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="5" cols="50" name="remark"></textarea>
      <li><input type="submit" value="Save">
    </ul>
    </form>
</body>
</html>

*改--創建goods-update.html頁面

<!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>

三、易錯分析

  • 404--服務器依據請求資源路徑找不到對應的資源。1)錯誤原因:

a,請求地址寫錯了

b,<servlet-name>不一致

2)解決方式:

a,依據 http://ip :port/appname/servlet-url檢查請求地址。

b,檢查/.xml文件。

  • 500--運行時出錯。

1)錯誤原因:

a,配置文件類名寫錯了。

b,程序代碼寫錯。

2)解決方式:

a,檢查/.xml文件。有沒有把類名寫錯(必須是完整類名)

b,檢查程序代碼。

來源:https://www.tuicool.com/articles/QJjQjuj

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章