Spring Boot 摸索之路-1:连接MySql数据库

关于Spring_Boot框架下如何连接MySql数据库的学习过程

[toc]

Spring-Boot 三大特性之:开箱即用

连接数据库

  • 在以前如何连接数据库?

    • 首先在本地或服务器上建立数据库

    • 进入IDEA,如果使用hibernate连接的话导入hibernate的jar包,导入ehcache的jar包和logj4的jar包。

    • 新建hibernate.conf.xml

    • 配置一大堆数据库地址,用户名密码之类的标签

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <!-- Hibernate配置文件 -->
    <hibernate-configuration>
    <session-factory>
    <!--配置连接目标数据库的基本信息 -->
    <property name="connection.driver_class">
    com.mysql.cj.jdbc.Driver
    </property>
    <property name="connection.url">
    jdbc:mysql://Ip:Port/DatabaseName?serverTimezone=TimeZone
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <!-- 配置c3p0连接池 -->
    <!-- 初始化连接数量 -->
    <property name="c3p0.max_size">10</property>
    <!-- 最小连接数量 -->
    <property name="c3p0.min_size">2</property>
    <!-- 连接池中最大允许多少个语句对象 -->
    <property name="c3p0.max_statements">100</property>
    <!-- 空闲连接检测间隔 -->
    <property name="c3p0.idle_test_period">3000</property>
    <!-- 最长等待时间,超时时间 -->
    <property name="c3p0.timeout">5000</property>
    <!-- 连接池扩容步长 -->
    <property name="c3p0.acquire_increment">5</property>

    <!-- 开启二级缓存 -->
    <property name="cache.use_second_level_cache">true</property>

    <!-- 指定缓存供应商 -->
    <property name="cache.region.factory_class">
    org.hibernate.cache.ehcache.EhCacheRegionFactory
    </property>

    <!-- 开启查询缓存 -->
    <property name="cache.use_query_cache">true</property>

    <!-- 配置数据库方言 -->
    <property name="dialect">
    org.hibernate.dialect.MySQLDialect
    </property>

    <!-- 显示SQL语句 -->
    <property name="show_sql">true</property>

    <!-- 格式化SQL语句 -->
    <property name="format_sql">true</property>

    <!-- 开启自动生成DDL语句 -->
    <property name="hbm2ddl.auto">update</property>

    <!-- 批处理 -->
    <property name="hibernate.jdbc.batch_size">50</property>

    <!-- 映射目标实体类 -->
    <mapping class="packageName.EntityClass" />
    </session-factory>
    </hibernate-configuration>
  • 现在有了SpringBoot如何连接数据库?

    • 首先在本地或服务器上建立数据库

    • 进入IDEA,创建一个基于mavenSpring-Boot项目

    • pom.xml中导入一下依赖

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
         <!--
      Persit data in SQL stores with Java Persistence API
      using Spring Data and Hibernate
      -->
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>


      <!--
      MySql JDBC and R2DBC driver
      -->
      <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
      </dependency>
    • 在配置文件中输入数据库地址,用户名和密码就好了

    • application.properties

      1
      2
      3
      4
      5
      6
      7
      8
      9
      # 数据源 配置 (DataSourceAutoConfiguration & DataSourceProperties)

      spring.datasource.driver-class-name = #JDBC驱动程序的完全限定名称。默认情况下,根据URL自动检测

      spring.datasource.name = #数据源的名称

      spring.datasource.password = #登录数据库的密码

      spring.datasource.url = #数据库的JDBC url
    • application.yml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      spring:
      datasource:
      #数据库的JDBC url
      url: jdbc:mysql://Ip:Port/DatabaseName?serverTimezone=TimeZone
      #JDBC驱动程序的完全限定名称。默认情况下,根据URL自动检测
      driver-class-name: com.mysql.cj.jdbc.Driver
      #数据源的名称
      username: root
      #登录数据库的密码
      password: root

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;

import lombok.*;

/**商品实体类*/
//lombok自动产生get&set方法,构造方法等方法
@Data
@NoArgsConstructor
@AllArgsConstructor
//表示该类是一个实体类,对应一张数据库的表
@Entity
@Table(name = "item_demo")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = id)
private Long id;//商品编号,对应数据库的id

@Column(name = picture)
private String picture;//商品图片路径,对应数据库

@Column(name = name)
private String name;//商品名称,对应数据库

@Column(name = price)
private Double price;//商品价格,对应数据库
}