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

CREATE TABLE product_category (

category_idint 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’商品表’;

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

在这里插入图片描述

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


mysql

mysql-connector-java

org.springframework.boot

spring-boot-starter-data-jpa

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


在这里插入图片描述

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

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.persisten
ce.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;

//https://blog.csdn.net/fly910905/article/details/78557110/ JpaRepository用法,实现增删改查

public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {

}

7.编写测试类


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值