<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 前端使用thymeleaf來代替jsp --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
#server server.port=80 #項目名:server.servlet.context-path #spring dataSource spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:/mapper/*/*.xml #spring log logging.level.com.cy=debug #spring thymeleaf(假如沒有配置也會默認配置,在默認配置中prefix默認值爲classpath:/templates/,後綴默認爲.html) #不用重啓服務器,網頁就能刷新 spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/pages/ spring.thymeleaf.suffix=.html
數據層封裝了商品信息,並提供get和set方法,爲Goods類html
1.查詢全部數據前端
@Select("select * from tb_goods") List<Goods> findAll();
2.按照id刪除數據java
@Delete("delete from tb_goods where id=#{id}") int deleteById(Integer id);
3.修改數據
(1)修改數據首先要新建一個界面,按照id查找內容,並將查找到的內容顯示到文本框內mysql
@Select("select * from tb_goods where id=#{id}") Goods findById(Integer id);
(2)再添加查找的方法web
@Update("update tb_goods set name=#{name},remark=# {remark},createdTime=now() where id=#{id}") int update(Goods goods);
4.新增數據spring
@Insert("insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},now())") int add(Goods goods);
1.業務層接口sql
public interface GoodsService { List<Goods> findObject(); int add(Goods goods); int update(Goods goods); Goods findById(Integer id); }
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"; }
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 href="#" th:href="@{/goods/doDeleteById(id=${g.id})}"><button>刪除</button></a></td>--> <td><a href="#" th:href="@{/goods/doDeleteById/{doDeleteById}(doDeleteById=${g.id})}"><button>刪除</button></a></td> <td><a href="#" th:href="@{/goods/doFindById/{id}(id=${g.id})}"><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>