引自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. 算法(Java实现)—— KMP算法

    KMP算法 应用场景 字符串匹配问题 有一个字符串str1 = " hello hello llo hhello lloh helo" 一个子串str2 = "hello ...

  2. 爱普生 L4160 Serveies 网络打印机配置(问题解决)

    一.爱普生网络打印机固定IP地址 用网络打印机过程中,偶尔会出现打印机脱机的状况,大多数原因是打印机的IP地址在路由器重启过后重新分配了IP地址导致的.此时,为了减少不必要的麻烦就需要固定打印机的IP ...

  3. Spring-IOC注解编程

    这里的注解是最初级的一些注解,掌握了之后再学习其它的注解 注解扫描 <?xml version="1.0" encoding="UTF-8"?> & ...

  4. spring传播机制注意点

    在同一个类里面spring的传播机制是不起作用的比如说在执行saveA方法的时候调用C方法插入C设置的传播属性是不使用事物 但是执行的效果是saveA方法抛出异常后导致C的记录回滚了也就是说明C方法设 ...

  5. 阿里云Ubuntu配置安装MQTT服务器

    先来说说mqtt协议: MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽.不可靠的网络的进行远程 ...

  6. WEBSERVICE之CXF框架开发webservice

    之前学习了使用jdk开发webservice服务,现在开始学习使用框架(cxf)开发webservice. 1.准备工作 A.使用cxf开发webservice服务,需要用到apache-cxf-3. ...

  7. JUC包-原子类(AtomicInteger为例)

    目录 JUC包-原子类 为什么需要JUC包中的原子类 原子类原理(AtomicInteger为例) volatile CAS CAS的缺点 ABA问题 什么是ABA问题 ABA问题的解决办法 JUC包 ...

  8. git 知识点积累

    1.初始化指定目录作为git目录  git init newpro 2.git add xx.uve 3.克隆项目 git clone git clone git://github.com/schac ...

  9. LeetCode 124 二叉树中最大路径和

    题目: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 思路:递归 分为三部分,根节点,左子树,右 ...

  10. #2使用html+css+js制作网站教程 测试

    #2使用html+css+js制作网站教程 测试 本系列链接 1 测试 1.1 运行 1.2 审查 1.3 审查技巧 1.4 其他 引言: 编写完代码后就要上机测试代码,获得用户体验,筛选bug 笔者 ...