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

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

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

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

這里我們先把a(bǔ)pplication.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格式有個(gè)注意點(diǎn) 冒號(hào)后面一定要加個(gè)空格

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

先寫(xiě)一個(gè)BookDao接口

package com.hik.dao;

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

import com.hik.entity.Book;

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

}

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

/*
 * 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();
}

再寫(xiě)一個(gè)BookController類(lèi)

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控制類(lèi)
 * @author jed
 *
 */
@Controller
@RequestMapping("/book")
public class BookController {
 
 @Resource
 private BookDao bookDao;
 
 /**
  * 查詢(xún)所有圖書(shū)
  * @return
  */
 @RequestMapping(value="/list")
 public ModelAndView list() {
  ModelAndView mav = new ModelAndView ();
  mav.addObject("bookList", bookDao.findAll());
  mav.setViewName("bookList");
  return mav;
 }

 /**
  * 添加圖書(shū)
  * @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;
 }
 
 /**
  * 修改圖書(shū)
  * @param book
  * @return
  */
 @PostMapping(value="/update")
 public String update(Book book) {
  bookDao.save(book);
  return "forward:/book/list";
 }
 
 /**
  * 刪除圖書(shū)
  * @param id
  * @return
  */
 @RequestMapping(value="/delete",method = RequestMethod.GET)
 public String delete(Integer id) {
  bookDao.delete(id);
  return "forward:/book/list";
 }
}

實(shí)現(xiàn)了 CRUD

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

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

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

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>圖書(shū)管理頁(yè)面</title>
</head>
<body>
<a href="/bookAdd.html" rel="external nofollow" >添加圖書(shū)</a>
 <table>
  <tr>
   <th>編號(hào)</th>
   <th>圖書(shū)名稱(chēng)</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 圖書(shū)添加頁(yè)面

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

bookUpdate.ftl圖書(shū)修改頁(yè)面

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

瀏覽器請(qǐng)求:http://localhost:8888/book/list

進(jìn)入:

點(diǎn)擊 “添加圖書(shū)”:

進(jìn)入:

我們隨便輸入名稱(chēng),點(diǎn)擊“提交”,

選擇剛才添加的測(cè)試圖書(shū),進(jìn)行修改

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

進(jìn)入修改頁(yè)面,修改下名稱(chēng),點(diǎn)擊“提交”,

選擇測(cè)試圖書(shū),進(jìn)行刪除操作

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

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

OK完成!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解JAVA中implement和extends的區(qū)別

    詳解JAVA中implement和extends的區(qū)別

    這篇文章主要介紹了詳解JAVA中implement和extends的區(qū)別的相關(guān)資料,extends是繼承接口,implement是一個(gè)類(lèi)實(shí)現(xiàn)一個(gè)接口的關(guān)鍵字,需要的朋友可以參考下
    2017-08-08
  • java爬取并下載酷狗TOP500歌曲的方法

    java爬取并下載酷狗TOP500歌曲的方法

    這篇文章主要介紹了java爬取并下載酷狗TOP500歌曲的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2019-01-01
  • SpringBoot使用@Cacheable注解實(shí)現(xiàn)緩存功能流程詳解

    SpringBoot使用@Cacheable注解實(shí)現(xiàn)緩存功能流程詳解

    最近一直再學(xué)Spring Boot,在學(xué)習(xí)的過(guò)程中也有過(guò)很多疑問(wèn)。為了解答自己的疑惑,也在網(wǎng)上查了一些資料,以下是對(duì)@Cacheable注解的一些理解
    2023-01-01
  • Java?MyBatis傳出參數(shù)resultType和resultMap解讀

    Java?MyBatis傳出參數(shù)resultType和resultMap解讀

    這篇文章主要介紹了Java?MyBatis傳出參數(shù)resultType和resultMap解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 利用Java快速查找21位花朵數(shù)示例代碼

    利用Java快速查找21位花朵數(shù)示例代碼

    這篇文章主要給大家介紹了關(guān)于利用Java快速查找21位花朵數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • 淺析Java中的set集合類(lèi)型及其接口的用法

    淺析Java中的set集合類(lèi)型及其接口的用法

    Java本身對(duì)set集合提供了一個(gè)接口,一般的實(shí)現(xiàn)類(lèi)是HastSet和 TreeSet,這里我們先來(lái)簡(jiǎn)要淺析Java中的set集合類(lèi)型及其接口的用法:
    2016-05-05
  • Java并發(fā)編程之線(xiàn)程之間的共享和協(xié)作

    Java并發(fā)編程之線(xiàn)程之間的共享和協(xié)作

    這篇文章主要介紹了Java并發(fā)編程之線(xiàn)程之間的共享和協(xié)作,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-04-04
  • Java多線(xiàn)程volatile原理及用法解析

    Java多線(xiàn)程volatile原理及用法解析

    這篇文章主要介紹了Java多線(xiàn)程volatile原理及用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解

    Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解

    這篇文章主要介紹了Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java線(xiàn)程和操作系統(tǒng)線(xiàn)程的關(guān)系解讀

    Java線(xiàn)程和操作系統(tǒng)線(xiàn)程的關(guān)系解讀

    這篇文章主要介紹了Java線(xiàn)程和操作系統(tǒng)線(xiàn)程的關(guān)系解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評(píng)論