最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作

 更新時間:2018年08月26日 12:01:58   作者:心和夢的方向  
這篇文章主要為大家詳細介紹了SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文演示了SpringBoot下,實用Spring-Data-Jpa來實現(xiàn)CRUD操作,視圖層采用Freemarker

這里我們先把application.properties修改成application.yml 主流格式

內(nèi)容也改成yml規(guī)范格式:

server:
 port: 8888
 context-path: /
 
helloWorld: spring Boot\u4F60\u597D
 
msyql:
 jdbcName: com.mysql.jdbc.Driver
 dbUrl: jdbc:mysql://localhost:3306/db_diary
 userName: root
 password: 123456
 
spring:
 datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/db_book
  username: root
  password: passwd
 jpa:
  hibernate.ddl-auto: update
  show-sql: true

yml格式有個注意點 冒號后面一定要加個空格

還有我們把context-path改成/方便開發(fā)應用

先寫一個BookDao接口

package com.hik.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.hik.entity.Book;

/**
 * 圖書Dao接口
 * @author jed
 *
 */
public interface BookDao extends JpaRepository<Book, Integer>{

}

要求實現(xiàn)JpaRepository,JpaRepository是繼承PagingAndSortingRepository,PagingAndSortingRepository是繼承CrudRepository。CrudRepository實現(xiàn)了實體增刪改查操作

/*
 * Copyright 2008-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.repository;

import java.io.Serializable;

/**
 * Interface for generic CRUD operations on a repository for a specific type.
 * 
 * @author Oliver Gierke
 * @author Eberhard Wolff
 */
@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {

 /**
  * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
  * entity instance completely.
  * 
  * @param entity
  * @return the saved entity
  */
 <S extends T> S save(S entity);

 /**
  * Saves all given entities.
  * 
  * @param entities
  * @return the saved entities
  * @throws IllegalArgumentException in case the given entity is {@literal null}.
  */
 <S extends T> Iterable<S> save(Iterable<S> entities);

 /**
  * Retrieves an entity by its id.
  * 
  * @param id must not be {@literal null}.
  * @return the entity with the given id or {@literal null} if none found
  * @throws IllegalArgumentException if {@code id} is {@literal null}
  */
 T findOne(ID id);

 /**
  * Returns whether an entity with the given id exists.
  * 
  * @param id must not be {@literal null}.
  * @return true if an entity with the given id exists, {@literal false} otherwise
  * @throws IllegalArgumentException if {@code id} is {@literal null}
  */
 boolean exists(ID id);

 /**
  * Returns all instances of the type.
  * 
  * @return all entities
  */
 Iterable<T> findAll();

 /**
  * Returns all instances of the type with the given IDs.
  * 
  * @param ids
  * @return
  */
 Iterable<T> findAll(Iterable<ID> ids);

 /**
  * Returns the number of entities available.
  * 
  * @return the number of entities
  */
 long count();

 /**
  * Deletes the entity with the given id.
  * 
  * @param id must not be {@literal null}.
  * @throws IllegalArgumentException in case the given {@code id} is {@literal null}
  */
 void delete(ID id);

 /**
  * Deletes a given entity.
  * 
  * @param entity
  * @throws IllegalArgumentException in case the given entity is {@literal null}.
  */
 void delete(T entity);

 /**
  * Deletes the given entities.
  * 
  * @param entities
  * @throws IllegalArgumentException in case the given {@link Iterable} is {@literal null}.
  */
 void delete(Iterable<? extends T> entities);

 /**
  * Deletes all entities managed by the repository.
  */
 void deleteAll();
}

再寫一個BookController類

package com.hik.Controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hik.dao.BookDao;
import com.hik.entity.Book;

/**
 * Book控制類
 * @author jed
 *
 */
@Controller
@RequestMapping("/book")
public class BookController {
 
 @Resource
 private BookDao bookDao;
 
 /**
  * 查詢所有圖書
  * @return
  */
 @RequestMapping(value="/list")
 public ModelAndView list() {
  ModelAndView mav = new ModelAndView ();
  mav.addObject("bookList", bookDao.findAll());
  mav.setViewName("bookList");
  return mav;
 }

 /**
  * 添加圖書
  * @param book
  * @return
  */
 @RequestMapping(value="/add", method=RequestMethod.POST)
 public String add(Book book) {
  bookDao.save(book);
  return "forward:/book/list";
 }
 
 @GetMapping(value="/preUpdate/{id}")
 public ModelAndView preUpdate(@PathVariable("id") Integer id) {
  ModelAndView mav = new ModelAndView();
  mav.addObject("book", bookDao.getOne(id));
  mav.setViewName("bookUpdate");
  return mav;
 }
 
 /**
  * 修改圖書
  * @param book
  * @return
  */
 @PostMapping(value="/update")
 public String update(Book book) {
  bookDao.save(book);
  return "forward:/book/list";
 }
 
 /**
  * 刪除圖書
  * @param id
  * @return
  */
 @RequestMapping(value="/delete",method = RequestMethod.GET)
 public String delete(Integer id) {
  bookDao.delete(id);
  return "forward:/book/list";
 }
}

實現(xiàn)了 CRUD

這里的@GetMapping(value="xxx") 類似  @RequestMapping(value="xxx",method=RequestMethod.GET)

以及@PostMapping(value="xxx") 類似  @RequestMapping(value="xxx",method=RequestMethod.POST)

bookList.ftl 展示數(shù)據(jù)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>圖書管理頁面</title>
</head>
<body>
<a href="/bookAdd.html" rel="external nofollow" >添加圖書</a>
 <table>
  <tr>
   <th>編號</th>
   <th>圖書名稱</th>
   <th>操作</th>
  </tr>
  <#list bookList as book>  
  <tr>  
   <td>${book.id}</td>  
   <td>${book.bookName}</td> 
   <td>
    <a href="/book/preUpdate/${book.id}" rel="external nofollow" >修改</a>
    <a href="/book/delete?id=${book.id}" rel="external nofollow" >刪除</a>
   </td>
  </tr> 
  </#list> 
 </table> 
</body>
</html>

bookAdd.html 圖書添加頁面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>圖書添加頁面</title>
</head>
<body>
<form action="book/add" method="post">
 圖書名稱:<input type="text" name="bookName"/><br/>
 <input type="submit" value="提交"/>
</form>
</body>
</html>

bookUpdate.ftl圖書修改頁面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>圖書修改頁面</title>
</head>
<body>
<form action="/book/update" method="post">
<input type="hidden" name="id" value="${book.id}"/>
 圖書名稱:<input type="text" name="bookName" value="${book.bookName}"/><br/>
 <input type="submit" value="提交"/>
</form>
</body>
</html>

瀏覽器請求:http://localhost:8888/book/list

進入:

點擊 “添加圖書”:

進入:

我們隨便輸入名稱,點擊“提交”,

選擇剛才添加的測試圖書,進行修改

轉(zhuǎn)發(fā)執(zhí)行到列表頁面,然后點“修改”,

進入修改頁面,修改下名稱,點擊“提交”,

選擇測試圖書,進行刪除操作

再次轉(zhuǎn)發(fā)到列表頁面,我們點擊“刪除”,

刪掉數(shù)據(jù)后,再次轉(zhuǎn)發(fā)到列表頁面;

OK完成!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    這篇文章主要介紹了springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-12-12
  • Java算法之快速排序舉例詳解

    Java算法之快速排序舉例詳解

    這篇文章主要介紹了Java算法之快速排序的相關資料,快速排序是一種高效的排序算法,通過遞歸的方式將待排序數(shù)組分成小部分進行排序,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-04-04
  • Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解

    Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解

    這篇文章主要介紹了Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解,有些朋友可能不了解過濾器、監(jiān)聽器和攔截器的區(qū)別,本文就來詳細講一下,相信看完你會有所收獲,需要的朋友可以參考下
    2024-01-01
  • Java實現(xiàn)動態(tài)數(shù)字時鐘

    Java實現(xiàn)動態(tài)數(shù)字時鐘

    這篇文章主要為大家詳細介紹了Java實現(xiàn)動態(tài)數(shù)字時鐘,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • java中全排列的生成算法匯總

    java中全排列的生成算法匯總

    本文給大家匯總介紹了常見的6種全排列的生成算法,包括字典序法、遞增進位數(shù)制法、遞減進位數(shù)制法、鄰位交換法、遞歸類算法、元素增值法,有需要的小伙伴可以參考下
    2015-07-07
  • Spring Web項目spring配置文件隨服務器啟動時自動加載

    Spring Web項目spring配置文件隨服務器啟動時自動加載

    這篇文章主要介紹了Spring Web項目spring配置文件隨服務器啟動時自動加載,加載spring的配置文件,并且只加載一次,從而提高程序效率。具體內(nèi)容詳情大家通過本文一起學習吧
    2018-01-01
  • Java基礎:徹底搞懂java多線程

    Java基礎:徹底搞懂java多線程

    篇文章主要介紹了Java多線程的相關資料,幫助大家更好的理解和學習Java線程相關知識,感興趣的朋友可以了解下,希望能給你帶來幫助
    2021-08-08
  • 淺談Java中真的只有值傳遞么

    淺談Java中真的只有值傳遞么

    這篇文章主要介紹了淺談Java中真的只有值傳遞么?文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • 十分鐘速懂java知識點 System類

    十分鐘速懂java知識點 System類

    這篇文章主要介紹了java知識點System類,根據(jù)一次面試總結的,可以十分鐘速懂System類,感興趣的小伙伴們可以參考一下
    2015-12-12
  • JAVA多線程Thread和Runnable的實現(xiàn)

    JAVA多線程Thread和Runnable的實現(xiàn)

    java中實現(xiàn)多線程有兩種方法:一種是繼承Thread類,另一種是實現(xiàn)Runnable接口。
    2013-03-03

最新評論

同仁县| 霍林郭勒市| 明溪县| 尼勒克县| 通河县| 苏尼特右旗| 措美县| 承德市| 盈江县| 盐亭县| 全南县| 镇赉县| 宁晋县| 阆中市| 屯留县| 梧州市| 金湖县| 水城县| 太康县| 东丽区| 江川县| 东至县| 灵山县| 广西| 镇远县| 壶关县| 平邑县| 西吉县| 盱眙县| 马鞍山市| 璧山县| 白沙| 凌源市| 开封县| 安阳县| 射洪县| 天等县| 新竹市| 体育| 英超| 通州区|