1.首先开启数据库MySQL服务,我用的是xampp管理工具

springboot项目--数据库配置(使用JpaRepository进行增删改查)_spring boot

2.使用sql-front可视化工具新建一个名为‘selldata.sql’的数据库,然后使用以下建表语句建表:

CREATE TABLE `product_category` (
`category_id`int not null AUTO_INCREMENT,
`category_name` varchar(64)not null comment'类目名字',
`category_type` decimal(8,2)not null comment'类目编号',
`create_time` timestamp not null default current_timestamp comment'创建时间',
`update_time` timestamp not null default current_timestamp on update current_timestamp
comment'修改时间',
PRIMARY KEY (`category_id`),
UNIQUE KEY `une_category_type` (`category_type`)
)comment'商品表';

下图红色圈出来就是刚才新建的表。

springboot项目--数据库配置(使用JpaRepository进行增删改查)_spring boot_02

3.在spring项目的pom.xml里添加以下依赖:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

4.在resource目录下创建application.yml文件

springboot项目--数据库配置(使用JpaRepository进行增删改查)_spring_03

用户名、密码、端口号可以改成自己的。

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: laomilaomilaomi
url: jdbc:mysql://localhost:3399/selldata?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true

5.编写封装类ProductCategory.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

//类目
@Entity //表明是一个实体类
public class ProductCategory {
@Id //主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//配置主键自增长
private Integer categoryId;//类目id
private String categoryName;//类目名字
private Integer categoryType;//类目编号

public Integer getCategoryId() {
return categoryId;
}

public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}

public String getCategoryName() {
return categoryName;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public Integer getCategoryType() {
return categoryType;
}

public void setCategoryType(Integer categoryType) {
this.categoryType = categoryType;
}

@Override
public String toString() {
return "ProductCategory{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", categoryType=" + categoryType +
'}';
}
}

6.编写接口ProductCategoryRepository.java

import com.laomi.sell.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
//javascript:void(0) JpaRepository用法,实现增删改查
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {

}

7.编写测试类

springboot项目--数据库配置(使用JpaRepository进行增删改查)_类目_04

import com.laomi.sell.dataobject.ProductCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {

@Autowired
private ProductCategoryRepository repository;

// @Test
// public void findOneTest(){
//
// ProductCategory productCategory = repository.findById(1).get();
// System.out.println(productCategory.toString());
// }

@Test
public void saveTest(){
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("最划算");
productCategory.setCategoryType(1);
repository.save(productCategory);
}
}

运行一下这个类,就会发现数据库多了内容

springboot项目--数据库配置(使用JpaRepository进行增删改查)_mysql_05