Springboot根据pojo实体类自动生成数据库表

本文介绍了如何利用Springboot的springboot-data-jpa依赖和实体类注解,自动创建数据库表。步骤包括添加相关依赖,配置propertiys或yml文件,编写带有@Entity、@Id和@GeneratedValue注解的实体类,最后启动项目检查数据库表是否生成。若遇到'Failed to configure a DataSource'错误,可能需要调整MySQL驱动与数据库版本的匹配。
部署运行你感兴趣的模型镜像

第一步:添加springboot-data-jpa和数据库的依赖关系

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

第二步:编写propertiys文件或者yml文件的配置

application.properties:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jackson.serialization.indent_output=false

或者application.yml:两者选一个就行

spring:
    datasource:
        driver-class-name:  com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/facemap
        username: root
        password: root

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

第三步:编写实体类

  • @Entity 实体类的注解
  • @Id 映射到表格中id的属性
  • @Gernertervalue 添加其自增的属性;

注意一定要添加这几个注解

package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;


@Entity //实体类的注解
public class Girl {

    @Id //@id注意选择这个javax.persistence
    @GeneratedValue
    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;
    }
}

第四步:启动Springboot项目,查看数据库中是否生成了表。

PS

如果启动项目了之后报了下面这个错误,可能是mysql的版本和驱动的版本不匹配

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured

将application.properties里面的url改成下面的即可

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jackson.serialization.indent_output=false

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值