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 数据库操作(上)的更多相关文章

  1. SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

    软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...

  2. springboot(五):spring data jpa的使用

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

  3. SpringBoot(五) :spring data jpa 的使用

    原文出处: 纯洁的微笑 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法 ...

  4. 最近项目中使用Spring data jpa 踩过的坑

    最近在做一个有关OA项目中使用spring data JPA 操作数据库,结果遇到了补个不可思议的麻烦.困惑了好久. 首先看一下问题吧,这就是当时测试“设置角色时,需要首先删除该用户已经拥有的角色时” ...

  5. SpringBoot系列之Spring Data Jpa集成教程

    SpringBoot系列之Spring Data Jpa集成教程 Spring Data Jpa是属于Spring Data的一个子项目,Spring data项目是一款集成了很多数据操作的项目,其下 ...

  6. SpringBoot入门:Spring Data JPA 和 JPA(理论)

    参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...

  7. Hibernate中使用Spring Data JPA

    一.配置文件的方式 1.pom.xml中引入相关依赖 <properties> <project.build.sourceEncoding>UTF-8</project. ...

  8. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  9. SpringBoot总结之Spring Data Jpa

    一.Spring Data Jpa简介 JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate.TopLink等. Spring D ...

  10. Spring Data Jpa 更新操作

    第一步,通过Repository对象把实体根据ID查询出来 第二部,往查出来的实体对象进行set各个字段 第三步,通过Repository接口的save方法进行保存 保存和更新方式(已知两种) 第一种 ...

随机推荐

  1. Python-基本图形绘制及库引用

    turtle库的使用 概述:turtle(海龟)库是turtle绘图体系的python实现 turtle库的理解: -有一只海龟,其实在窗体正中心,在画布上游走 -走过的轨迹形成了绘制的图形 -海龟由 ...

  2. A - 栈

    Description   You are given a string consisting of parentheses () and []. A string of this type is s ...

  3. 【01】emmet系列之基础介绍

    [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 相关网址 官网:http: ...

  4. MVC系统学习6—Filter

    Mvc的过滤器是特性类,可以使我们在执行Action之前,执行Action之后,执行Action发生异常时,编写相关的处理代码实现某些逻辑.下面是四个基本的Filter接口. 上面这四个基本的Filt ...

  5. [Zabbix] 如何实现邮件报警通知以及免费短信报警通知

    版权声明:本文为博主原创文章,未经博主允许不得转载.  前提条件: (1) zabbix服务器端已经成功安装并且运行. (2) zabbix客户端已经成功建立并且运行. 1 下载并且安装msmtp软件 ...

  6. POJ 3264 RMQ问题 用dp解决

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...

  7. HDU 4960 (水dp)

    Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. Th ...

  8. 和式 sigma的使用

    1.和式 0)艾佛森约定 艾佛森约定可以用来简化和式,艾佛森约定中的\([p(k)]\)就是一个限制条件,类似于一个\(bool\)函数,我们可以这样写 \[ \sum_{1<k<n}a_ ...

  9. POJ1422 Air Raid

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8006   Accepted: 4803 Description Consi ...

  10. UUID使用

    import java.util.UUID; public static String getUUID() { UUID uuid =UUID.randomUUID(); String str = u ...