zoukankan      html  css  js  c++  java
  • 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 25 11:08:13 CST 2018 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.
    2018-05-25 11:08:14.313 ERROR 21664 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - 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:62) ~[na:1.8.0_144]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_144]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_144]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar:5.1.46]
        at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.46.jar:5.1.46]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944) ~[mysql-connector-java-5.1.46.jar:5.1.46]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976

    创建完数据库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是每次程序跑的时候它都会创建一个空的表。如果你之前有这个表的话它会帮你先删掉。

    它首先把表删了再创建一个空表。

    2018-05-25 11:39:47.585  INFO 21112 --- [           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(255), primary key (id)) engine=MyISAM
    Hibernate: create table hibernate_sequence (next_val bigint) engine=MyISAM
    Hibernate: insert into hibernate_sequence values ( 1 )

    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会验证类里面的属性跟表是否一致。如果不一致的话它会报错。

  • 相关阅读:
    ibmmq 性能测试
    zabbix-agent 安装
    关于dubbo接口性能测试
    关于vyos 防火墙配置
    appium自动化的工作原理(1)
    unittest如何在循环遍历一条用例时生成多个测试结果
    在Linux中#!/usr/bin/python之后把后面的代码当成程序来执行。 但是在windows中用IDLE编程的话#后面的都是注释,之后的代码都被当成文本了。 该怎么样才能解决这个问题呢?
    Cookie和Session的区别详解
    点单登录原理和java实现简单的单点登录
    new一个JAVA对象的时候,内存是怎么分配的?
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/9065374.html
Copyright © 2011-2022 走看看