idea自動生成spring實體

建立Spring Boot項目

idea建立新項目

File->New->Project選擇Spring Initializr默認使用官方的https://start.spring.io/選擇下一步。java

groupId: com.axd
artifactId: spring_boot_jpa
name: spring_boot_jpamysql

Type: Maven Project
Java Version: 1.8
packaging: Jarweb

點擊下一步選擇web,建立一個新的項目spring

在pom.xml配置jap的依賴

<!-- Spring Boot jpa 依賴 -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.axd</groupId>
    <artifactId>spring_boot_jpa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_boot_jpa</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

!pom.xml具體配置()sql

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

配置自動生成目錄

添加數據庫

idea添加mysql

配置
配置好以後測試連接成功數據庫

persistence添加

View->Tool Windows->persistence,默認找不到,須要設置一下。apache

添加Hibernate以後,若是沒有就下載。
app

再次打開就能夠看到persistence
maven

生成實體類
ide

若是粗線這種報錯,則須要增長一個配置

增長

從新建立

建立Repository

繼承JpaRepository,不須要寫實現,默認一些增刪改查

controller調用

請求localhost:8080/advert/content/all

返回目錄

相關文章
相關標籤/搜索