1.在maven的pom文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <!-- druid start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- druid end -->
<!-- mysql start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version><!--$NO-MVN-MAN-VER$ -->
</dependency>
<!-- mysql end -->
2.在springboot的配置文件yml文件中配置数据库链接信息
spring:
#数据库链接信息配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://192.168.1.25:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
#JPA配置
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
database-platform: org.hibernate.dialect.MySQL55Dialect
#请求编码配置
http:
encoding:
charset: UTF-8
force: true
enabled: true
#日志配置
logging:
file: /logs/springBootTest.log
pattern:
level: debug
3.具体编码
3.1第一步:构建实体类Customer
package com.example.myapplication.customer;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.example.myapplication.order.Order;
@Table
@Entity
public class Customer implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4340240628311870193L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(columnDefinition="int unsigned")
private Long id;
@Column(nullable=false)
private String name;
@Column(name="borthday",nullable=false)
private Date borthDay;
@Column(nullable=false)
private String add