书上说的是mariadb,直接连mysql是可以的。但改成mysql了,报错了,作了一下调整。
一,pom.xml配置
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
com.h2database
h2
runtime
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
二,建立实体(包含一对多和多对一)
Car.java
package com.packt.cardatabase.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.FetchType;
@Entity
public class Car {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String brand, model, color, registerNumber;
private int year, price;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "owner")
private Owner owner;
public Car() {
}
public Car(String brand, String model, String color, String registerNumber, int year, int price, Owner owner) {
super();
this.brand = brand;
this.model = model;
this.color = color;
this.registerNumber = registerNumber;
this.year = year;
this.price = price;
this.owner = owner;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public String getColor() {
return color;
}
public String getRegisterNumber() {
return registerNumber;
}
public int getYear() {
return year;
}
public int getPrice() {
return price;
}
public Owner getOwner() {
return owner;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setModel(String model) {
this.model = model;
}
public void setColor(String color) {
this.color = color;
}
public void setRegisterNumber(String registerNumber) {
this.registerNumber = registerNumber;
}
public void setYear(int year) {
this.year = year;
}
public void setPrice(int price) {
this.price = price;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
}
Owner.java
package com.packt.cardatabase.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import java.util.List;
@Entity
public class Owner {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long ownerId;
private String firstname, lastname;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private List cars;
public Owner() {
}
public Owner(String firstname, String lastname) {
super();
this.firstname = firstname;
this.lastname = lastname;
}
public long getOwnerId() {
return ownerId;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public List getCars() {
return cars;
}
public void setOwnerId(long ownerId) {
this.ownerId = ownerId;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setCars(List cars) {
this.cars = cars;
}
}
三,建立repository接口
CarRepository.java
package com.packt.cardatabase.domain;
import org.springframework.data.repository.CrudRepository;
public interface CarRepository extends CrudRepository {
}
OwnerRepository.java
package com.packt.cardatabase.domain;
import org.springframework.data.repository.CrudRepository;
public interface OwnerRepository extends CrudRepository{
}
四,application.properties配置
logging.level.root=INFO
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cardb?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
五,建立数据库
mysql下,略过不表
六,运行测试
2020-10-08 21_57_50-mvnw.png
2020-10-08 21_58_32-car @cardb (localhost3306) - 表 - Navicat for MySQL.png