001 Springboot使用jpa连接mysql


第一步:在pom.xml导入相关依赖

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		

第二步:编写配置文件application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/base?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
  jpa:
    hibernate:
    # 更新或创建数据库表结构
      ddl-auto: update
    # 在控制台显示SQL
      show-sql: true

第三步:创建映射关系

package com.yuanlrc.base.entity;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "yuanlrc_operator_log")
public class OperaterLog {

    @Column(name = "id", nullable = false, length = 11)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;//id

    @Column(name = "operator", nullable = false, length = 18)
    private String operator;//操作者

    @Column(name = "content", nullable = false,  length = 128)
    private String content;//操作内容

    @Column(name = "create_name",nullable = false)
    private Date createTime;//操作时间

其中的位置关系需要注意:否则无法创建数据库文件(放在base之下,和启动文件BaseApplication一个层级)

 第四步:创建对应的数据库表

第五步;直接运行程序即可

相关