1.新建项目,点击Next
2.对项目进行必要的配置,这个随意,根据自己需要来,不过Type还是选Maven Project
3.选择需要的选项,这在后面需要用到,点击Next
4.点击finish
5.这是整个项目初始结构
6.这是pom.xml
<?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 https://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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhou</groupId>
<artifactId>springboot-jpa-mysql-maven</artifactId>
<version>0.0.1</version>
<name>springboot-jpa-mysql-maven</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-data-jdbc</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>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7.这是我新加的两个文件,一个实体类,一个全局配置
8.User类
package com.zhou.springbootjpamysqlmaven.pojo;
import javax.persistence.*;
/**
* 使用JPA注解配置映射关系
*/
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "user1") //@Table(name="user1") 意思是数据库对应得表名是user1,如果没有(name="user1")默认类名为表名 名称默认小写 user
public class User {
@Id //表示这个属性是数据表中的主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //还是一个自增的主键
private Integer id;
@Column(name="UserName",length = 50) //name="UserName" 数据库列名会变成UserName,length=50是定义其长度
private String name;
@Column //省略的情况,默认列名就是属性名
private String email;
}
9.application.yml
spring:
datasource:
# jdbc:mysql://localhost/my localhost就是本地/my就是数据库的名称
driverClassName: com.mysql.cj.jdbc.Driver #net.sf.log4jdbc.sql.jdbcapi.DriverSpy #net.sf.log4jdbc.DriverSpy
url: jdbc:mysql://localhost/my?characterEncoding=UTF8&serverTimezone=UTC&useSSL=false&useUnicode=true
username: root
password: 123456
jpa:
database: MySQL
show-sql: true #控制台显示sql语句
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ddl-auto: update #更新创建表结构
dialect: org.hibernate.dialect.MySQL5Dialect
10.上面步骤完成后点击运行,这是运行的时候数据的日志,证明表成功了
Hibernate: create table user1 (id integer not null auto_increment, email varchar(255), UserName varchar(50), primary key (id)) engine=MyISAM
11.打开表看确实成功了
12.以上就是一个实体类通过JPA生成一个表,后面继续写一个通过JPA增删改查的操作