SpringBoot运用Spring-Data-Jpa实现CRUD实现
下面来演示下SpringBoot下,实用Spring-Data-Jpa来实现CRUD操作,视图层采用Freemarker
这里我们先把application.properties修改成application.yml 主流格式
内容也改成yml规范格式:
1 server: 2 port: 8888 3 context-path: / 4 5 helloWorld: spring Boot\u4F60\u597D 6 7 msyql: 8 jdbcName: com.mysql.jdbc.Driver 9 dbUrl: jdbc:mysql://localhost:3306/db_diary 10 userName: root 11 password: 123456 12 13 spring: 14 datasource: 15 driver-class-name: com.mysql.jdbc.Driver 16 url: jdbc:mysql://localhost:3306/db_book 17 username: root 18 password: passwd 19 jpa: 20 hibernate.ddl-auto: update 21 show-sql: true
yml格式有个注意点 冒号后面一定要加个空格
还有我们把context-path改成/方便开发应用
先写一个BookDao接口
1 package com.hik.dao; 2 3 import org.springframework.data.jpa.repository.JpaRepository; 4 5 import com.hik.entity.Book; 6 7 /** 8 * 图书Dao接口 9 * @author jed 10 * 11 */ 12 public interface BookDao extends JpaRepository<Book, Integer>{ 13 14 }
要求实现JpaRepository,JpaRepository是继承PagingAndSortingRepository,PagingAndSortingRepository是继承CrudRepository。CrudRepository实现了实体增删改查操作
1 /* 2 * Copyright 2008-2011 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.springframework.data.repository; 17 18 import java.io.Serializable; 19 20 /** 21 * Interface for generic CRUD operations on a repository for a specific type. 22 * 23 * @author Oliver Gierke 24 * @author Eberhard Wolff 25 */ 26 @NoRepositoryBean 27 public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { 28 29 /** 30 * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the 31 * entity instance completely. 32 * 33 * @param entity 34 * @return the saved entity 35 */ 36 <S extends T> S save(S entity); 37 38 /** 39 * Saves all given entities. 40 * 41 * @param entities 42 * @return the saved entities 43 * @throws IllegalArgumentException in case the given entity is {@literal null}. 44 */ 45 <S extends T> Iterable<S> save(Iterable<S> entities); 46 47 /** 48 * Retrieves an entity by its id. 49 * 50 * @param id must not be {@literal null}. 51 * @return the entity with the given id or {@literal null} if none found 52 * @throws IllegalArgumentException if {@code id} is {@literal null} 53 */ 54 T findOne(ID id); 55 56 /** 57 * Returns whether an entity with the given id exists. 58 * 59 * @param id must not be {@literal null}. 60 * @return true if an entity with the given id exists, {@literal false} otherwise 61 * @throws IllegalArgumentException if {@code id} is {@literal null} 62 */ 63 boolean exists(ID id); 64 65 /** 66 * Returns all instances of the type. 67 * 68 * @return all entities 69 */ 70 Iterable<T> findAll(); 71 72 /** 73 * Returns all instances of the type with the given IDs. 74 * 75 * @param ids 76 * @return 77 */ 78 Iterable<T> findAll(Iterable<ID> ids); 79 80 /** 81 * Returns the number of entities available. 82 * 83 * @return the number of entities 84 */ 85 long count(); 86 87 /** 88 * Deletes the entity with the given id. 89 * 90 * @param id must not be {@literal null}. 91 * @throws IllegalArgumentException in case the given {@code id} is {@literal null} 92 */ 93 void delete(ID id); 94 95 /** 96 * Deletes a given entity. 97 * 98 * @param entity 99 * @throws IllegalArgumentException in case the given entity is {@literal null}. 100 */ 101 void delete(T entity); 102 103 /** 104 * Deletes the given entities. 105 * 106 * @param entities 107 * @throws IllegalArgumentException in case the given {@link Iterable} is {@literal null}. 108 */ 109 void delete(Iterable<? extends T> entities); 110 111 /** 112 * Deletes all entities managed by the repository. 113 */ 114 void deleteAll(); 115 }
再写一个BookController类
1 package com.hik.Controller; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.PathVariable; 8 import org.springframework.web.bind.annotation.PostMapping; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RequestMethod; 11 import org.springframework.web.servlet.ModelAndView; 12 13 import com.hik.dao.BookDao; 14 import com.hik.entity.Book; 15 16 /** 17 * Book控制类 18 * @author jed 19 * 20 */ 21 @Controller 22 @RequestMapping("/book") 23 public class BookController { 24 25 @Resource 26 private BookDao bookDao; 27 28 /** 29 * 查询所有图书 30 * @return 31 */ 32 @RequestMapping(value="/list") 33 public ModelAndView list() { 34 ModelAndView mav = new ModelAndView (); 35 mav.addObject("bookList", bookDao.findAll()); 36 mav.setViewName("bookList"); 37 return mav; 38 } 39 40 /** 41 * 添加图书 42 * @param book 43 * @return 44 */ 45 @RequestMapping(value="/add", method=RequestMethod.POST) 46 public String add(Book book) { 47 bookDao.save(book); 48 return "forward:/book/list"; 49 } 50 51 @GetMapping(value="/preUpdate/{id}") 52 public ModelAndView preUpdate(@PathVariable("id") Integer id) { 53 ModelAndView mav = new ModelAndView(); 54 mav.addObject("book", bookDao.getOne(id)); 55 mav.setViewName("bookUpdate"); 56 return mav; 57 } 58 59 /** 60 * 修改图书 61 * @param book 62 * @return 63 */ 64 @PostMapping(value="/update") 65 public String update(Book book) { 66 bookDao.save(book); 67 return "forward:/book/list"; 68 } 69 70 /** 71 * 删除图书 72 * @param id 73 * @return 74 */ 75 @RequestMapping(value="/delete",method = RequestMethod.GET) 76 public String delete(Integer id) { 77 bookDao.delete(id); 78 return "forward:/book/list"; 79 } 80 }
实现了 CRUD
这里的@GetMapping(value="xxx") 类似 @RequestMapping(value="xxx",method=RequestMethod.GET)
以及@PostMapping(value="xxx") 类似 @RequestMapping(value="xxx",method=RequestMethod.POST)
bookList.ftl 展示数据
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>图书管理页面</title> 6 </head> 7 <body> 8 <a href="/bookAdd.html">添加图书</a> 9 <table> 10 <tr> 11 <th>编号</th> 12 <th>图书名称</th> 13 <th>操作</th> 14 </tr> 15 <#list bookList as book> 16 <tr> 17 <td>${book.id}</td> 18 <td>${book.bookName}</td> 19 <td> 20 <a href="/book/preUpdate/${book.id}">修改</a> 21 <a href="/book/delete?id=${book.id}">删除</a> 22 </td> 23 </tr> 24 </#list> 25 </table> 26 </body> 27 </html>
bookAdd.html 图书添加页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>图书添加页面</title> 6 </head> 7 <body> 8 <form action="book/add" method="post"> 9 图书名称:<input type="text" name="bookName"/><br/> 10 <input type="submit" value="提交"/> 11 </form> 12 </body> 13 </html>
bookUpdate.ftl图书修改页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>图书修改页面</title> 6 </head> 7 <body> 8 <form action="/book/update" method="post"> 9 <input type="hidden" name="id" value="${book.id}"/> 10 图书名称:<input type="text" name="bookName" value="${book.bookName}"/><br/> 11 <input type="submit" value="提交"/> 12 </form> 13 </body> 14 </html>
浏览器请求:http://localhost:8888/book/list
进入:
点击 “添加图书”:
进入:
我们随便输入名称,点击“提交”,
选择刚才添加的测试图书,进行修改
转发执行到列表页面,然后点“修改”,
进入修改页面,修改下名称,点击“提交”,
选择测试图书,进行删除操作
再次转发到列表页面,我们点击“删除”,
删掉数据后,再次转发到列表页面;
OK完成!