SpringBoot使用Spring-Data-Jpa实现CRUD操作

本文演示了SpringBoot下,实用Spring-Data-Jpa来实现CRUD操作,视图层采用Freemarker

路北ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

这里我们先把application.properties修改成application.yml 主流格式

内容也改成yml规范格式:

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改成/方便开发应用

先写一个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{

}

要求实现JpaRepository,JpaRepository是继承PagingAndSortingRepository,PagingAndSortingRepository是继承CrudRepository。CrudRepository实现了实体增删改查操作

/*
 * 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 extends Repository {

 /**
  * 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 save(S entity);

 /**
  * Saves all given entities.
  * 
  * @param entities
  * @return the saved entities
  * @throws IllegalArgumentException in case the given entity is {@literal null}.
  */
  Iterable save(Iterable 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 findAll();

 /**
  * Returns all instances of the type with the given IDs.
  * 
  * @param ids
  * @return
  */
 Iterable findAll(Iterable 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";
 }
}

实现了 CRUD

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

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

bookList.ftl 展示数据





图书管理页面


添加图书
 
  <#list bookList as book>  
  
编号 图书名称 操作
${book.id} ${book.bookName} 修改 删除

bookAdd.html 图书添加页面





图书添加页面


图书名称:

bookUpdate.ftl图书修改页面





图书修改页面


图书名称:

浏览器请求:http://localhost:8888/book/list

进入:

SpringBoot使用Spring-Data-Jpa实现CRUD操作

点击 “添加图书”:

进入:

SpringBoot使用Spring-Data-Jpa实现CRUD操作

我们随便输入名称,点击“提交”,

SpringBoot使用Spring-Data-Jpa实现CRUD操作

选择刚才添加的测试图书,进行修改

SpringBoot使用Spring-Data-Jpa实现CRUD操作

转发执行到列表页面,然后点“修改”,

SpringBoot使用Spring-Data-Jpa实现CRUD操作

进入修改页面,修改下名称,点击“提交”,

选择测试图书,进行删除操作

SpringBoot使用Spring-Data-Jpa实现CRUD操作

再次转发到列表页面,我们点击“删除”,

SpringBoot使用Spring-Data-Jpa实现CRUD操作

删掉数据后,再次转发到列表页面;

OK完成!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享题目:SpringBoot使用Spring-Data-Jpa实现CRUD操作
本文链接:http://lszwz.com/article/gicgep.html

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款