欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot實(shí)現(xiàn)簡(jiǎn)單的增刪改查

 更新時(shí)間:2020年09月01日 08:19:55   作者:Jackson  
這篇文章主要介紹了Spring Boot如何實(shí)現(xiàn)簡(jiǎn)單的增刪改查,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下

在pom.xml添加相應(yīng)的依賴

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

配置文件配置數(shù)據(jù)庫等

#server
  server.port=80
  #項(xiàng)目名: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(假如沒有配置也會(huì)默認(rèn)配置,在默認(rèn)配置中prefix默認(rèn)值為classpath:/templates/,后綴默認(rèn)為.html)
  #不用重啟服務(wù)器,網(wǎng)頁就能刷新
  spring.thymeleaf.cache=false 
  spring.thymeleaf.prefix=classpath:/templates/pages/
  spring.thymeleaf.suffix=.html

數(shù)據(jù)層添加相應(yīng)注解實(shí)現(xiàn)sql語句(或者通過xml配置來實(shí)現(xiàn))

數(shù)據(jù)層封裝了商品信息,并提供get和set方法,為Goods類

1.查詢所有數(shù)據(jù)

@Select("select * from tb_goods")
  List<Goods> findAll();

2.按照id刪除數(shù)據(jù)

@Delete("delete from tb_goods where id=#{id}")
  int deleteById(Integer id);

3.修改數(shù)據(jù)

(1)修改數(shù)據(jù)首先要新建一個(gè)界面,按照id查找內(nèi)容,并將查找到的內(nèi)容顯示到文本框內(nèi)

@Select("select * from tb_goods where id=#{id}")
  Goods findById(Integer id);

(2)再添加查找的方法

@Update("update tb_goods set name=#{name},remark=# {remark},createdTime=now() where id=#{id}")
  int update(Goods goods);

4.新增數(shù)據(jù)

@Insert("insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},now())")
  int add(Goods goods);

業(yè)務(wù)層提供對(duì)應(yīng)接口方法和實(shí)現(xiàn)類

1.業(yè)務(wù)層接口

public interface GoodsService {
  List<Goods> findObject();
  int add(Goods goods);
  int update(Goods goods);
  Goods findById(Integer id);
}

2.業(yè)務(wù)層實(shí)現(xiàn)類

@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);
  }

控制層寫具體實(shí)現(xiàn)

1.跳轉(zhuǎn)到首頁并且查找所有商品

@RequestMapping("doGoodsUI")
  public String doGoodsUI(Model model) {
    List<Goods> list = goodsService.findObject();
    model.addAttribute("goods",list);
    return "goods";
    }

2.業(yè)務(wù)層實(shí)現(xiàn)類

@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);
  }

控制層寫具體實(shí)現(xiàn)

1.跳轉(zhuǎn)到首頁并且查找所有商品

@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)告訴服務(wù)器,id拿到的是從網(wǎng)頁上同樣叫id的數(shù)據(jù)
  public String dodeletebyId(@PathVariable Integer id){
    int delete = goodsDao.deleteById(id);
    //doGoodsUI前面沒有加/的話,跳轉(zhuǎn)的網(wǎng)址是替代了最后一個(gè)/后面的內(nèi)容
    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)實(shí)現(xiàn)修改

@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表示遍歷拿到的數(shù)組,goods是從控制層拿到的model的名字

3.id,name和remark與數(shù)據(jù)庫對(duì)應(yīng),date要格式化拿到數(shù)據(jù),該語法是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="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:href="@{/goods/doDeleteById(id=${g.id})}" rel="external nofollow" ><button>刪除</button></a></td>-->
    <td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:href="@{/goods/doDeleteById/{doDeleteById}(doDeleteById=${g.id})}" rel="external nofollow" ><button>刪除</button></a></td>
    <td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:href="@{/goods/doFindById/{id}(id=${g.id})}" rel="external nofollow" ><button>修改</button></a></td>
</tr>

4.新增商品界面

(1)標(biāo)簽里的name屬性要和sql語句一致

(2)這里由于數(shù)據(jù)庫中的id列設(shè)置了自增長(zhǎng),所以不需要id屬性,createdTime列使用了now()獲取當(dāng)前時(shí)間,所以也不需要傳值,所以在控制層的doUpdateGoods方法里可以使用封裝好的Goods來接收從html拿到的參數(shù)

<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)因?yàn)閕d列自增長(zhǎng),所以修改商品信息不需要id這一列,但傳參數(shù)有需要一起傳送過去,所以添加了一個(gè)輸入框,默認(rèn)設(shè)置為隱藏,將其value設(shè)置為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實(shí)現(xiàn)簡(jiǎn)單的增刪改查的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot增刪改查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring boot集成Kafka+Storm的示例代碼

    Spring boot集成Kafka+Storm的示例代碼

    這篇文章主要介紹了Spring boot集成Kafka+Storm的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • IDEA找不到database圖標(biāo)的簡(jiǎn)單圖文解決方法

    IDEA找不到database圖標(biāo)的簡(jiǎn)單圖文解決方法

    idea是一個(gè)功能十分強(qiáng)大的IDE,大家在使用他進(jìn)行開發(fā)時(shí)候,必不可少的就是連接數(shù)據(jù)庫了,這篇文章主要給大家介紹了關(guān)于IDEA找不到database圖標(biāo)的解決方法,需要的朋友可以參考下
    2024-07-07
  • Java回調(diào)函數(shù)原理實(shí)例與代理模式的區(qū)別講解

    Java回調(diào)函數(shù)原理實(shí)例與代理模式的區(qū)別講解

    今天小編就為大家分享一篇關(guān)于Java回調(diào)函數(shù)原理實(shí)例與代理模式的區(qū)別講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 深入淺析springboot中static和templates區(qū)別

    深入淺析springboot中static和templates區(qū)別

    這篇文章主要介紹了springboot中static和templates區(qū)別,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java Web之限制用戶多處登錄實(shí)例代碼

    Java Web之限制用戶多處登錄實(shí)例代碼

    本篇文章主要介紹了Java Web之限制用戶多處登錄實(shí)例代碼,可以限制單個(gè)用戶在多個(gè)終端登錄。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • MybatisPlus 多租戶架構(gòu)(Multi-tenancy)實(shí)現(xiàn)詳解

    MybatisPlus 多租戶架構(gòu)(Multi-tenancy)實(shí)現(xiàn)詳解

    這篇文章主要介紹了MybatisPlus 多租戶架構(gòu)(Multi-tenancy)實(shí)現(xiàn)詳解,詳細(xì)的介紹了什么是多租戶架構(gòu)以及使用MybatisPlus實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • Spring Aop基本流程原理示例詳解

    Spring Aop基本流程原理示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring Aop基本流程原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • JavaWeb開發(fā)之模仿知乎首頁完整代碼

    JavaWeb開發(fā)之模仿知乎首頁完整代碼

    這篇文章主要介紹了JavaWeb開發(fā)之模仿知乎首頁完整代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • 完美解決Logback configuration error detected的問題

    完美解決Logback configuration error detected的問題

    這篇文章主要介紹了完美解決Logback configuration error detected的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

    關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

    這篇文章主要給大家介紹了關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評(píng)論