创建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的依赖
<!-- 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具体配置()
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