SpringBoot中使用spring-data-jpa 数据库操作(上)

Java客户端使用Spring-Data-Jpa这个组件。



Spring-Data-Jpa就是Spring对Hibernate的一个整合。


选择create在运行的时候它会自动帮我们创建一个表。
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: create
show-sql: true

Fri May :: CST WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
-- ::14.313 ERROR --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool- - Exception during pool initialization. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'dbgirl'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:) ~[na:1.8.0_144]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:) ~[na:1.8.0_144]
at java.lang.reflect.Constructor.newInstance(Constructor.java:) ~[na:1.8.0_144]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:) ~[mysql-connector-java-5.1..jar:5.1.]
at com.mysql.jdbc.Util.getInstance(Util.java:) ~[mysql-connector-java-5.1..jar:5.1.]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:) ~[mysql-connector-java-5.1..jar:5.1.]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:

创建完数据库dbgirl之后再跑一次项目工程。还是空白,数据库dbgirl下没有东西。不需要建表,而是建一个类Girl,给它一些属性。Girl是跟数据库对应的一个类,这些属性值会映射成数据库的字段。这里就使用到jpa的特性了。
新建的类girl:
package com.imooc.girl; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; /**
* Created by zhongzh
* 2018-05-20 11:16
*/
@Entity //表明类Girl对应数据库里面的一个表
public class Girl {
@Id
@GeneratedValue//Id一般都用作自增的。
private Integer id; private String cupSize;//罩杯 private Integer age;
// 必须要选一个无参的构造方法,不然待会数据库操作的时候会报错
public Girl() {
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getCupSize() {
return cupSize;
} public void setCupSize(String cupSize) {
this.cupSize = cupSize;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
再跑一次项目

没有写SQL语句它就自动帮我们创建一个表了。

jpa的配置是create。如果数据库增加一条记录呢,

再次运行项目,数据没了
jpa的配置create是每次程序跑的时候它都会创建一个空的表。如果你之前有这个表的话它会帮你先删掉。

它首先把表删了再创建一个空表。
-- ::47.585 INFO --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: drop table if exists girl
Hibernate: drop table if exists hibernate_sequence
Hibernate: create table girl (id integer not null, age integer, cup_size varchar(), primary key (id)) engine=MyISAM
Hibernate: create table hibernate_sequence (next_val bigint) engine=MyISAM
Hibernate: insert into hibernate_sequence values ( )
jpa配置换成update试试,update也是很常用的jpa配置方式。
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
update第一次运行的时候也会创建对应的表结构,不同于create的是你里面有数据的话它是不会删掉的,它会保留着。

明显看到表girl的数据还保留着。
ddl-auto有五种配置方式。

create-drop是应用停下来的时候它会帮你把表给删掉。none就是默认的什么都不做。validate会验证类里面的属性跟表是否一致。如果不一致的话它会报错。
SpringBoot中使用spring-data-jpa 数据库操作(上)的更多相关文章
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...
- springboot(五):spring data jpa的使用
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- SpringBoot(五) :spring data jpa 的使用
原文出处: 纯洁的微笑 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法 ...
- 最近项目中使用Spring data jpa 踩过的坑
最近在做一个有关OA项目中使用spring data JPA 操作数据库,结果遇到了补个不可思议的麻烦.困惑了好久. 首先看一下问题吧,这就是当时测试“设置角色时,需要首先删除该用户已经拥有的角色时” ...
- SpringBoot系列之Spring Data Jpa集成教程
SpringBoot系列之Spring Data Jpa集成教程 Spring Data Jpa是属于Spring Data的一个子项目,Spring data项目是一款集成了很多数据操作的项目,其下 ...
- SpringBoot入门:Spring Data JPA 和 JPA(理论)
参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...
- Hibernate中使用Spring Data JPA
一.配置文件的方式 1.pom.xml中引入相关依赖 <properties> <project.build.sourceEncoding>UTF-8</project. ...
- spring spring data jpa save操作事务
整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...
- SpringBoot总结之Spring Data Jpa
一.Spring Data Jpa简介 JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate.TopLink等. Spring D ...
- Spring Data Jpa 更新操作
第一步,通过Repository对象把实体根据ID查询出来 第二部,往查出来的实体对象进行set各个字段 第三步,通过Repository接口的save方法进行保存 保存和更新方式(已知两种) 第一种 ...
随机推荐
- 定义一个复数类Complex
#include<iostream> #include<math.h> using namespace std; class Complex{ public: Complex( ...
- BNUOJ 7697 Information Disturbing
Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...
- bzoj 1430 小猴打架 prufer 性质
小猴打架 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 709 Solved: 512[Submit][Status][Discuss] Descri ...
- Balance POJ - 1837 地推
Gigel has a strange "balance" and he wants to poise it. Actually, the device is different ...
- UVA 437_The Tower of Babylon
题意: 一堆石头,给定长宽高,每种石头均可以使用无数次,问这堆石头可以叠放的最高高度,要求下面的石头的长和宽分别严格大于上面石头的长和宽. 分析: 采用DAG最长路算法,由于长宽较大,不能直接用于表示 ...
- HDU——3072 Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Codeforces Round #247 (Div. 2) B
B. Shower Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 编程算法 - n个骰子的点数(递归) 代码(C)
n个骰子的点数(递归) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把n个骰子仍在地上, 全部骰子朝上一面的点数之和为s. 输入n, 打印出 ...
- 蓝桥杯 地宫寻宝 带缓存的DFS
历届试题 地宫取宝 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 X 国王有一个地宫宝库. 是 n x m 个格子的矩阵. 每一个格子放一件宝贝. 每一个宝贝贴着价 ...
- Cracking the Coding Interview 150题(一)
1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...