idea自动生成spring实体


创建Spring Boot项目

idea创建新项目

File->New->Project选择Spring Initializr默认使用官方的https://start.spring.io/选择下一步。

groupId: com.axd
artifactId: spring_boot_jpa
name: spring_boot_jpa

Type: Maven Project
Java Version: 1.8
packaging: Jar

点击下一步选择web,创建一个新的项目

在pom.xml配置jap的依赖


<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    com.axd
    spring_boot_jpa
    0.0.1-SNAPSHOT
    spring_boot_jpa
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
            8.0.13
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-cache
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



!pom.xml具体配置()

application.properties配置

spring.datasource.url=jdbc:mysql://192.168.80.129:3306/db_linkbiz
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

配置自动生成目录

添加数据库


配置好之后测试链接成功

persistence添加

View->Tool Windows->persistence,默认找不到,需要设置一下。

添加Hibernate之后,如果没有就下载。

再次打开就可以看到persistence

生成实体类

如果粗线这种报错,则需要增加一个配置

增加

重新创建

创建Repository

继承JpaRepository,不需要写实现,默认一些增删改查

controller调用

请求localhost:8080/advert/content/all

返回目录