引自B站楠哥:https://www.bilibili.com/video/BV137411B7vB

一、新建Springboot项目

​ pom.xml文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.southwind</groupId>
<artifactId>springboottest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>springboottest</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

application.yml文件

spring:
datasource:
url: jdbc:mysql://localhost:3306/db20201107_demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
server:
port: 8088

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringboottestApplication { public static void main(String[] args) {
SpringApplication.run(SpringboottestApplication.class, args);
} }

实体类Book

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
@Data
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String author;
}

二、在数据库中创建对应的book表

create table `book` (
`id` int (10),
`name` varchar (60),
`author` varchar (60),
`publish` varchar (60),
`pages` int (10),
`price` float ,
`bookcaseid` int (10),
`abled` int (10)
);
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('1','解忧杂货店','东野圭吾','电子工业出版社','102','27.30','9','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('2','追风筝的人','卡勒德·胡赛尼','中信出版社','330','26.00','1','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('3','人间失格','太宰治','作家出版社','150','17.30','1','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('4','这就是二十四节气','高春香','电子工业出版社','220','59.00','3','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('5','白夜行','东野圭吾','南海出版公司','300','27.30','4','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('6','摆渡人','克莱儿·麦克福尔','百花洲文艺出版社','225','22.80','1','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('7','暖暖心绘本','米拦弗特毕','湖南少儿出版社','168','131.60','5','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('8','天才在左疯子在右','高铭','北京联合出版公司','330','27.50','6','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('9','我们仨','杨绛','生活.读书.新知三联书店','89','17.20','7','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('10','活着','余华','作家出版社','100','100.00','6','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('11','水浒传','施耐庵','三联出版社','300','50.00','1','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('12','三国演义','罗贯中','三联出版社','300','50.00','2','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('13','红楼梦','曹雪芹','三联出版社','300','50.00','5','1');
insert into `book` (`id`, `name`, `author`, `publish`, `pages`, `price`, `bookcaseid`, `abled`) values('14','西游记','吴承恩','三联出版社','300','60.00','3','1');

三、项目分层

3.1 BookRepository

import com.southwind.springboottest.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository; public interface BookRepository extends JpaRepository<Book,Integer> {
}

3.2 BookController

import com.southwind.springboottest.entity.Book;
import com.southwind.springboottest.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private BookRepository bookRepository; @GetMapping("/findAll/{page}/{size}")
public Page<Book> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
PageRequest request = PageRequest.of(page-1,size);//此处第一个参数是从0开始计数,所以用page-1
System.out.println("request="+request.toString());
return bookRepository.findAll(request);
} @PostMapping("/save")
public String save(@RequestBody Book book){
Book result = bookRepository.save(book);
if(result != null){
return "success";
}else{
return "error";
}
} @GetMapping("/findById/{id}")
public Book findById(@PathVariable("id") Integer id){
return bookRepository.findById(id).get();
} @PutMapping("/update")
public String update(@RequestBody Book book){
Book result = bookRepository.save(book);
if(result != null){
return "success";
}else{
return "error";
}
} @DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") Integer id){
bookRepository.deleteById(id);
}
}

SpringBoot JPA简单使用的更多相关文章

  1. 简单才是美! SpringBoot+JPA

    SpringBoot 急速构建项目,真的是用了才知道,搭配JPA作为持久层,一简到底!下面记录项目的搭建,后续会添加NOSQL redis,搜索引擎elasticSearch,等等,什么不过时就加什么 ...

  2. 补习系列(19)-springboot JPA + PostGreSQL

    目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...

  3. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限

    上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...

  4. 带着新人学springboot的应用08(springboot+jpa的整合)

    这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...

  5. SpringBoot JPA + H2增删改查示例

    下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...

  6. SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy

    问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...

  7. IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统

    先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...

  8. Kotlin + SpringBoot + JPA 服务端开发

    Kotlin + SpringBoot + JPA 服务端开发 本篇主要介绍一下 kotlin + springboot的服务端开发环境搭建 1.概述 Kotlin 是一个基于JVM的编程语言, 是I ...

  9. SQLite数据库和JPA简单介绍

    SQLite数据库和JPA简单介绍 一.SQLite简单使用 SQLite是遵循ACID的关系数据库管理系统,它的处理速度很快,它的设计目标是嵌入式的,只需要几百K的内存就可以了. 1.下载SQLit ...

随机推荐

  1. Web服务器-并发服务器-协程 (3.4.2)

    @ 目录 1.分析 2.代码 关于作者 1.分析 随着网站的用户量越来愈多,通过多进程多线程的会力不从心 使用协程可以缓解这一问题 只要使用gevent实现 2.代码 from socket impo ...

  2. MySQL(二):快速理解MySQL数据库索引

    索引 基本概念:索引是在存储引擎层实现的,而不是在服务器层实现的,所以不同存储引擎具有不同的索引类型和实现. 数据结构 Tree 指的是 Balance Tree,也就是平衡树.平衡树是一颗查找树,并 ...

  3. Liunx运维(五)-信息显示与搜索文件命令

    文档目录: 一.uname:显示系统信息 二.hostname:显示或设置系统的主机名 三.dmesg:系统启动异常诊断 四.stat:显示文件或文件系统状态 五.du:统计磁盘空间使用情况 六.da ...

  4. net core 3.1使用ElasticSearch 全文搜索引擎

    ElasticSearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene 基础之上. Lucene 可以说是当下最先进.高性能.全功能的搜索引擎库,无论是开源还是私有. ...

  5. python初学者-使用for循环用四位数组成不同的数

    digits = (1,2,3,4) for i in digits: for j in digits: if j==i: continuefor k in digits: if k==i or k= ...

  6. Android面试系列一

    什么是ANR,如何避免它 ​ ANR是应用程序无响应(Application Not Responding)的的英文缩写: ​ 当Android 手机在一段时间响应不够灵敏,系统会向用户展示一个对话框 ...

  7. 微信小程序--页面与组件之间如何进行信息传递和函数调用

    微信小程序--页面与组件之间如何进行信息传递和函数调用 ​ 这篇文章我会以我自己开发经验从如下几个角度来讲解相关的内容 页面如何向组件传数据 组件如何向页面传数据 页面如何调用组件内的函数 组件如何调 ...

  8. 磁盘IO工作机制

    磁盘IO工作机制 ref: <深入分析java web 技术内幕> by:许令波 几种访问文件的方式 文件读取和写入的 IO 操作都是调用操作系统提供的接口,因为磁盘设备是由操作系统管理的 ...

  9. Oracle中除数为0的两种解决办法(decode与nullif)

    Oracle中Decode函数,语句DECODE(tag,''ZCGS'',0,1)=decode(''@corp-No@'',''6010'',1,0) decode(字段或字段的运算,值1,值2, ...

  10. C#自定义TemplateImage使用模板底图,运行时根据用户或产品信息生成海报图(1)

    由于经常需要基于固定的一个模板底图,生成微信小程序分享用的海报图,如果每次都调用绘图函数,手动编写每个placeholder的填充,重复而且容易出错,因此,封装一个TemplateImage,用于填充 ...