SpringBoot+jpa配置根据实体类自动创建表

1.配置文件application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/bootTable?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

2.pom.xml引入包

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

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

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

3.编写实体类

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;


@Entity
//声明实体类
public class User implements Serializable {
    @Id
    //声明了实体唯一标识对应的属性
    @GeneratedValue
    //自增
    private Integer id;
    @Column(nullable = false, unique = true, length = 32)
    //长度32,唯一索引,nullable表示true可以为空,false不可以
    //用来声明实体属性的表字段的定义
    private String userName;

    private String passWord;

    private String email;

    private String nickName;

    private String regTime;

    @Transient
    //不映射成列的字段
    private String desc;
    //省略get和set方法
}

4.运行项目,启动即可生成

5.针对项目启动以后数据库并未生成数据库表问题:

包导的不对: import javax.persistence.*;
配置文件不对: spring.jpa.hibernate.ddl-auto=update
注解写的不对:不要忘记@Entity

还可能有一种原因:
Sprint的入口文件在子目录里了,应该比其他诸如service、dao、controller、entity高一级。
例如:service文件所在为com.demo.metaService,那么入口文件xxxApplication.java应该在com.demo下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 和 JPA(Java Persistence API)结合通常用于简化企业级应用的开发,它们提供了强大的数据库操作能力和自动配置的功能。以下是创建一个基本的 Spring Boot + JPA 项目的步骤: 1. **设置环境**: - 安装 Java SDK 和 Maven 或 Gradle 构建工具。 - 创建一个新的 Spring Boot 项目,可以通过 IntelliJ IDEA、Eclipse 或命令行工具如 `spring Initializr` 来创建。 2. **添加依赖**: 在你的 `pom.xml` (Maven) 或 `build.gradle` (Gradle) 文件中,添加以下 JPA 和数据源相关的依赖: ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Gradle --> implementation 'org.springframework.boot:spring-boot-starter-data-jpa' ``` 3. **配置数据库**: - 在 `application.properties` 或 `application.yml` 中提供数据库连接信息,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myuser spring.datasource.password=mypassword spring.jpa.hibernate.ddl-auto=update ``` 4. **实体类定义**: 创建一个或多个代数据库实体类(`MyEntity.java`),继承自 `JpaRepository` 或具体的 Entity 接口,例如: ```java @Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // ...字段和getter/setter方法 } interface MyRepository extends JpaRepository<MyEntity, Long> {} ``` 5. **配置扫描包**: 在主类上添加 `@EnableJpaRepositories` 注解,指定扫描 Repository 接口的位置: ```java @SpringBootApplication @EnableJpaRepositories(basePackages = "com.example.myapp.repository") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 6. **运行应用**: 使用构建工具构建并运行项目,Spring Boot 将自动配置 JPA 并创建数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖行骗老中医

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值