6_5.springboot2.x数据整合springData JPA
1、配置文件
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2、application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
# 更新或者创建数据表
ddl-auto: update
# 展示sql
show-sql: true
main:
allow-bean-definition-overriding: true
3、新建pojo
使用JPAz注解配置映射关系
package com.spoot.springboot.pojo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
//使用JPAz注解配置映射关系
//@Entity告诉JPA这是一个实体类和数据表映射的类 @Table指定和哪个表对应;如果省略表明就是user
@Entity
@Table(name = "tb_user")
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User {
//这是一个主键.同时标注这是一个自增主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "last_name",length = 255)
private String lastName;
@Column
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
fasterxml.jackson将对象转换为json
4、编写repository接口
package com.spoot.springboot.repository;
import com.spoot.springboot.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
//继承JpaRepository完成数据的操作 JpaRepository<User,Integer> 泛型操作的类和主键
public interface UserRepository extends JpaRepository<User,Integer> {
}
继承JpaRepository完成数据的操作 JpaRepository<User,Integer> 泛型操作的类和主键
5、controller
package com.spoot.springboot.controller;
import com.spoot.springboot.pojo.User;
import com.spoot.springboot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping("/user/{id}")
public User getUset(@PathVariable("id") Integer id){
User user = userRepository.getOne(id);
return user;
}
@GetMapping("/user")
public User getUset(User user){
User save = userRepository.save(user);
return save;
}
}
在yml中配置 ddl-auto: update会自动更新和创建表
测试:

OK!
6_5.springboot2.x数据整合springData JPA的更多相关文章
- 6_4.springboot2.x数据整合springData介绍
介绍 Spring Data 项目的目的是为了简化构建基于Spring 框架应用的数据访问技术,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关系数据库的访问支持. spr ...
- 【串线篇】spring boot整合SpringData JPA
一.SpringData简介 其中SpringData JPA底层基于hibernate 二.整合SpringData JPA JPA: Java Persistence API的简称,中文名Java ...
- SpringBoot整合SpringData JPA入门到入坟
首先创建一个SpringBoot项目,目录结构如下: 在pom.xml中添加jpa依赖,其它所需依赖自行添加 <dependency> <groupId>org.springf ...
- 第11章—使用对象关系映射持久化数据—SpringBoot+SpringData+Jpa进行查询修改数据库
SpringBoot+SpringData+Jpa进行查询修改数据库 JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它又不限于EJB 3.0,你可以在Web应用.甚至桌面应用 ...
- 整合SpringData JPA
ORM(Object Relational Mapping): 1).编写一个实体类(bean)和数据表进行映射,并且配置好映射关系: //使用JPA注解配置映射关系 @Entity //告诉JPA这 ...
- 6_3.springboot2.x数据整合Mybatis(注解和非注解)
1.配置文件 pom.xml 导入mybatis提供的启动器 <dependency> <groupId>org.mybatis.spring.boot</groupId ...
- 6.4 SpringData JPA的使用
引言:该文档是参考尚硅谷的关于springboot教学视屏后整理而来.当然后面还加入了一些自己从网上收集整理而来的案例! 一.SpringData JPA初步使用 1. springdata简介 2. ...
- 尚硅谷springboot学习34-整合SpringData JPA
SpringData简介
- SpringBoot图文教程12—SpringData Jpa的基本使用
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
随机推荐
- JAVA java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案
一.新建Java工程,在Java类中声明一个native的方法 新建Java项目 在新建的项目中创建packet(包),并且在包下创建一个Class(类). 接下来,在该类中添加如下代码: ? 1 2 ...
- Go const 关键字
Go const 关键字 package main import "fmt" func main() { const LENGTH int = 10 const WIDTH int ...
- Delphi中绘制圆角矩形的窗体
制作圆角矩形的窗体: 01.procedure TPortForm.FormCreate(Sender: Tobject); 02.var hr :thandle; 03.begin 04.hr:=c ...
- Annotation详解
转自:http://www.doc88.com/p-995532241886.html 首先我们定义一个简单的注解 package com.qjy.annotation; import java.la ...
- ionic-CSS:ionic 表单和输入框
ylbtech-ionic-CSS:ionic 表单和输入框 1.返回顶部 1. ionic 表单和输入框 list 类同样可以用于 input 元素.item-input 和 item 类指定了文本 ...
- LeetCode 1108. Defanging an IP Address (IP 地址无效化)
题目标签:String 题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了.所以还是走一下array,具体看code. Java ...
- django中filter()和get()的区别
在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或者查找 ...
- 【Web】浅析JQuery的apply(), call(), bind()方法
原文地址:https://blog.csdn.net/whuzxq/article/details/64166253 由于在理解this的用法的时候多次出现了这几个方法,个人对这几个方法理解的不是很透 ...
- Cesium资料大全
前言 Cesium是一个用于显示三维地球和地图的开源js库.它可以用来显示海量三维模型数据.影像数据.地形高程数据.矢量数据等等.三维模型格式支持gltf.三维瓦片模型格式支持3d tiles.矢量数 ...
- EasyMock添加行为
EasyMock使用expect()方法或expectLassCall()方法添加一个功能,一个模拟对象.请看下面的代码片段. 1 //add the behavior of calc service ...