SpringBoot JPA简单使用
一、新建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简单使用的更多相关文章
- 简单才是美! SpringBoot+JPA
SpringBoot 急速构建项目,真的是用了才知道,搭配JPA作为持久层,一简到底!下面记录项目的搭建,后续会添加NOSQL redis,搜索引擎elasticSearch,等等,什么不过时就加什么 ...
- 补习系列(19)-springboot JPA + PostGreSQL
目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- 带着新人学springboot的应用08(springboot+jpa的整合)
这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...
- SpringBoot JPA + H2增删改查示例
下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...
- SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy
问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...
- IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统
先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...
- Kotlin + SpringBoot + JPA 服务端开发
Kotlin + SpringBoot + JPA 服务端开发 本篇主要介绍一下 kotlin + springboot的服务端开发环境搭建 1.概述 Kotlin 是一个基于JVM的编程语言, 是I ...
- SQLite数据库和JPA简单介绍
SQLite数据库和JPA简单介绍 一.SQLite简单使用 SQLite是遵循ACID的关系数据库管理系统,它的处理速度很快,它的设计目标是嵌入式的,只需要几百K的内存就可以了. 1.下载SQLit ...
随机推荐
- Thymeleaf的th
th:action 定义后台控制器路径,类似<form>标签的action属性. <form id="login-form" th:action="@{ ...
- [TSCTF-J] relax
[TSCTF-J] relax 1.源码审计 利用扫描器可以扫到robots.txt 进入发现三个文件 flag.php heicore.php relax.php 我们只能进入relax.php 发 ...
- 注解 @CrossOrigin
在Controller中看到@CrossOrigin ,这是什么?有什么用?为什么要用? what? @CrossOrigin是用来处理跨域请求的注解 先来说一下什么是跨域: (站在巨人的肩膀上) 跨 ...
- s2-061 漏洞复现
0x00 漏洞简介 Apache Struts2框架是一个用于开发Java EE网络应用程序的Web框架.Apache Struts于2020年12月08日披露 S2-061 Struts 远程代码执 ...
- BST和DST简单的matlab程序(图的广度和深度遍历)
图的广度和深度遍历,具体内容教材有 clc;clear all;close all; %初始化邻接压缩表compressTable=[1 2;1 3;1 4;2 4;2 5;3 6;4 6;4 7]; ...
- pixi.js 简单交互事件(点击、缩放、平移)
注意:本文代码使用的Pixi.js版本为PixiJS 5.3.3 pixi中常用的鼠标交互事件: //兼容鼠标和触摸屏的共同触发 type InteractionPointerEvents = &qu ...
- [.NET] - 基础知识 - 如何debug一个.NET application
1.可以使用Debug/Trace类来将runtime信息输出到控制台窗口: https://msdn.microsoft.com/en-us/library/bs4c1wda.aspx https: ...
- ArrayList的删除姿势你都知道了吗
引言 前几天有个读者由于看了<ArrayList哪种遍历效率最好,你真的弄明白了吗?>问了个问题普通for循环ArrayList为什么不能删除连续重复的两个元素?其实这个描述是不正确的.正 ...
- SpringBoot+Vue 前后端合并部署
前后端分离开发项目 前端vue项目 服务端springboot项目 如何将vue的静态资源整合到springboot项目里,通过启动jar包的方式部署服务. 前端项目执行npm run build 命 ...
- mybatis实现MySQL数据库的增删改查之二
这里直接附上代码: 1 package com.qijian.pojo; 2 3 import org.apache.ibatis.type.Alias; 4 5 6 public class Use ...