zoukankan      html  css  js  c++  java
  • Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包。

    1 <dependency>
    2   <groupId>org.springframework.boot</groupId>
    3   <artifactId>spring-boot-starter-data-jpa</artifactId>
    4 </dependency>
    5 
    6 <dependency>
    7   <groupId>mysql</groupId>
    8   <artifactId>mysql-connector-java</artifactId>
    9 </dependency>

    新建dbpeople的一个数据库,用户名root,密码123456

    修改application.yml文件

     1 spring:
     2   profiles:
     3     active: prod
     4   datasource:
     5     driver-class-name: com.mysql.jdbc.Driver
     6     url: jdbc:mysql://localhost:3306/dbpeople
     7     username: root
     8     password: 123456
     9   jpa:
    10     hibernate:
    11       ddl-auto: create #update
    12     show-sql: true

    添加一个people类People.java

     1 package com.example.demo;
     2 
     3 import javax.persistence.Entity;
     4 import javax.persistence.GeneratedValue;
     5 import javax.persistence.Id;
     6 
     7 //@Entity对应数据库中的一个表
     8 @Entity
     9 public class People {
    10 
    11     //@Id配合@GeneratedValue,表示id自增长
    12     @Id
    13     @GeneratedValue
    14     private Integer id;
    15     private String name;
    16     private Integer age;
    17 
    18     //必须要有一个无参的构造函数,不然数据库会报错
    19     public People() {
    20     }
    21 
    22     public Integer getId() {
    23         return id;
    24     }
    25 
    26     public void setId(Integer id) {
    27         this.id = id;
    28     }
    29 
    30     public String getName() {
    31         return name;
    32     }
    33 
    34     public void setName(String name) {
    35         this.name = name;
    36     }
    37 
    38     public Integer getAge() {
    39         return age;
    40     }
    41 
    42     public void setAge(Integer age) {
    43         this.age = age;
    44     }
    45 }

    运行报错:

    Thu Nov 16 10:29:29 CST 2017 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.

    是因为高版本的MySQL连接需要useSSL=true,在application.yml文件中修改MySQL连接的url为

    url: jdbc:mysql://localhost:3306/dbpeople?useSSL=true

    运行成功刷新数据库发现生成了一个people的表

    在application.yml中的ddl-auto设置为create表示每次运行都会删掉以前的表,再建一张新表

    jpa:
        hibernate:
            ddl-auto: create 
            show-sql: true        

    如果设置成update,如果已经有表不会删掉原来的表,如果没有则新建一张表。

    jpa:
        hibernate:
            ddl-auto: update
            show-sql: true
  • 相关阅读:
    【题解】P2262 [HNOI2004]FTP服务器
    关于大模拟
    CodeForces 666E Forensic Examination
    Bzoj3233 [Ahoi2013]找硬币
    Bzoj4350 括号序列再战猪猪侠
    UOJ#31 【UR #2】猪猪侠再战括号序列
    UOJ#21 【UR #1】缩进优化
    51nod1667 概率好题
    [CodeChef
    51nod1245 Binomial Coefficients Revenge
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7843132.html
Copyright © 2011-2022 走看看