spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)
Spring-Data-Jpa
JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cy</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <name>HelloWorld</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</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-data-jpa</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.src/main/resources/application.yml中配置server的根路径和port、数据源、showsql等:
server:
port: 80
servlet:
context-path: / spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_book
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
3.代码:
Book.java实体:
package com.cy.entity; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="t_book")
public class Book { @Id
@GeneratedValue
private Integer id; @Column(length=100)
private String name; @Column(length=50)
private String author; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} }
com.cy.dao.BookDao.java:
package com.cy.dao; import org.springframework.data.jpa.repository.JpaRepository;
import com.cy.entity.Book; public interface BookDao extends JpaRepository<Book, Integer>{ }
CRUD的BookController.java:
package com.cy.controller; import java.util.List;
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.cy.dao.BookDao;
import com.cy.entity.Book; @Controller
@RequestMapping("/book")
public class BookController { @Resource
private BookDao bookDao; /**
* 查找所有图书
*/
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mav = new ModelAndView();
List<Book> bookList = bookDao.findAll();
mav.addObject("bookList", bookList);
mav.setViewName("bookList");
return mav;
} /**
* 添加图书
*/
@RequestMapping(value="/add", method=RequestMethod.POST)
public String add(Book book){
bookDao.save(book);
return "redirect:/book/list";
} /**
* 根据id查找图书
*/
@RequestMapping("/preUpdate/{id}")
public ModelAndView preUpdate(@PathVariable("id") Integer id){
ModelAndView mav = new ModelAndView();
mav.addObject("book", bookDao.getOne(id));
mav.setViewName("bookUpdate");
return mav;
} /**
* 修改图书
*/
@PostMapping("/update")
public String update(Book book){
bookDao.save(book); //依然是save,有id就修改,没有id就添加
return "redirect:/book/list";
} /**
* 删除图书
*/
@GetMapping("/delete")
public String delete(Integer id){
bookDao.deleteById(id);
return "redirect:/book/list";
}
}
src/main/resources/templates/bookList.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理</title>
</head>
<body>
<a href="/bookAdd.html">添加</a>
<table>
<tr>
<th>编号</th>
<th>图书名称</th>
<th>图书作者</th>
<th>操作</th>
</tr>
<#list bookList as book>
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>
<a href="/book/preUpdate/${book.id}">修改</a>
<a href="/book/delete?id=${book.id}">删除</a>
</td>
</tr>
</#list>
</table>
</body>
</html>
添加图书页面src/main/webapp/bookAdd.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/book/add" method="post">
图书名称:<input type="text" name="name"/><br/>
图书作者:<input type="text" name="author"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
修改图书页面:src/main/resources/templates/bookUpdate.ftl:
要注意修改这边要把book的id带过去,不然就成添加了;
<!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="name" value="${book.name}"/><br/>
图书作者:<input type="text" name="author" value="${book.author}"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
mysql> show create table t_book;
+--------+--------------------------------------
------------------------------------------------
| Table | Create Table +--------+--------------------------------------
------------------------------------------------
| t_book | CREATE TABLE `t_book` (
`id` int(11) NOT NULL auto_increment,
`author` varchar(50) default NULL,
`name` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+--------------------------------------
------------------------------------------------
1 row in set (0.00 sec)
mysql> desc t_book;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| author | varchar(50) | YES | | NULL | |
| name | varchar(100) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
2.http://localhost/book/list,显示:

进行增、删、改、list操作正常;
spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)的更多相关文章
- spring boot学习(5) SpringBoot 之Spring Data Jpa 支持(2)
第三节:自定义查询@Query 有时候复杂sql使用hql方式无法查询,这时候使用本地查询,使用原生sql的方式: 第四节:动态查询Specification 使用 什么时候用呢?比如搜索有很多条 ...
- Spring Boot学习(一)——Spring Boot介绍
Spring Boot介绍 Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式 ...
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- spring boot 学习(十)SpringBoot配置发送Email
SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...
- spring boot学习(6) SpringBoot 之事务管理
两个操作要么同时成功,要么同时失败: 事务的一致性: 以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来 ...
- spring boot学习(2) SpringBoot 项目属性配置
第一节:项目内置属性 application.properties配置整个项目的,相当于以前的web.xml: 注意到上一节的访问HelloWorld时,项目路径也没有加:直接是http://loca ...
- spring boot学习(7) SpringBoot 之表单验证
第一节:SpringBoot 之表单验证@Valid 是spring-data-jpa的功能: 下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁. 贴下代码吧: Student实体: ...
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...
随机推荐
- CCF CSP 201703
CCF CSP 2017·03 做了一段时间的CCF CSP试题,个人感觉是这样分布的 A.B题基本纯暴力可满分 B题留心数据范围 C题是个大模拟,留心即可 D题更倾向于图论?(个人做到的D题基本都是 ...
- Ubuntu16.04怎样安装Python3.6
Ubuntu16.04默认安装了Python2.7和3.5 请注意,系统自带的python千万不能卸载! 输入命令python
- 3.2 shell输入输出
shell输入与输出: read : read语句可以从键盘或者文件的某一行文本中读入信息,并将其赋值给一个变量. read var1 var2 ... 若只指定了一个变量,那么read将 ...
- Android Kernel save defalut config
/********************************************************************************* * Android Kernel ...
- linux-performance
1. top 2. cat /proc/meminfo nvidia@tegra-ubuntu:~/zrj/laneseg_TRT$ cat /proc/meminfo MemTotal: kB Me ...
- 查看camera设备-linux
前言 本文介绍如何在linux平台查看是否有camera外设. 操作过程 1.打开shell,输入以下命令: ls /dev/video* 即可查看是否有camera外设: 2.如果确实连接了came ...
- 从 0 到 1 合理高效使用 GitHub 的资料
来自:https://github.com/xirong/my-git/blob/master/how-to-use-github.md 说明 作为一名开发者,Github上面有很多东西值得关注学习, ...
- Linux基础和网络管理上机试题 - imsoft.cnblogs
一.(使用at命令实现任务的的自动化,要求用一条条的指令完成) 找出系统中任何以txt为后缀名的文档,并且进行打印.打印结束后给用户foxy发出邮件通知取件.指定时间为十二月二十五日凌晨两点 ...
- CodeForces - 1073E :Segment Sum (数位DP)
You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...
- JavaScript高级程序设计——闭包
前言 有很多人搞不清匿名函数和闭包这两个概念,经常混用.闭包是指有权访问另一个函数作用域中的变量的函数.匿名函数就是没有实际名字的函数. 闭包 概念 闭包,其实是一种语言特性,它是指的是程序设计语言中 ...