spring配置mysql数据库_spring-boot连接mysql数据库并进行数据插入

这篇博客介绍了如何在Spring Boot应用中配置MySQL数据库,包括pom.xml中添加依赖,建立实体类Car和Owner,定义repository接口,以及application.properties配置数据库连接。还涉及到数据插入和测试的过程。
摘要由CSDN通过智能技术生成

书上说的是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下,略过不表

六,运行测试

a3c7ca83b1ec

2020-10-08 21_57_50-mvnw.png

a3c7ca83b1ec

2020-10-08 21_58_32-car @cardb (localhost3306) - 表 - Navicat for MySQL.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值