mysql数据库的连接--买家类目(上)

在pom.xml文件中加mysql数据库的依赖

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

配置application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: admin123
    url: jdbc:mysql://47.115.79.211:3307/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true

下面根据数据库中的表创建java类
在这里插入图片描述

可以看到,数据库中的表名称为product_category,如果我们要创建对应的java类,不能完全跟表名一样,要有特殊的改变

1.首字母大写.

2.如上面的表名称,有下划线的,要去掉下划线,然后原下划线后面的首字母大写.

根据上面的规则,数据库名称为product_category的,对应的类名为ProductCategory

如果不想遵守上面规则,则要用到@Table注解

@Table(name="表中名称")

例如,我现在表中名称为s_product_category,但我java类的名称我想用ProductCategory

就可以@Table(name="s_product_category")

在这里插入图片描述

我这边就用的默认规则,首字母大写,去下划线,原下划线首字母大写,这种规则来创建与数据库表对应的java类

ProductCategory

package com.lbl.dataObject;

import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;

/**
 * 类目
 * Created by 廖师兄
 * 2017-05-07 14:30
 */
@Entity
@DynamicUpdate
@Data
public class ProductCategory {

    /** 类目id. */
    @Id
    @GeneratedValue
    private Integer categoryId;

    /** 类目名字. */
    private String categoryName;

    /** 类目编号. */
    private Integer categoryType;

    private Date createTime;

    private Date updateTime;

    public ProductCategory() {
    }

    public ProductCategory(String categoryName, Integer categoryType) {
        this.categoryName = categoryName;
        this.categoryType = categoryType;
    }
}

我来对上面类中的注解做一下解析

@Entity 表明该类 (UserEntity) 为一个实体类,它默认对应数据库中的表名是user_entity。这里也可以写成@Entity(name = “xwj_user”)

@Id 标注用于声明一个实体类的属性映射为数据库的主键列。

@GeneratedValue 用于标注主键的生成策略,通过strategy 属性指定。默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:SqlServer对应identity,MySQL 对应 auto increment。 (我这里是mysql,所以是auto increment自增)

数据库中product_category表的数据:

在这里插入图片描述

ProductCategoryRepository

package com.lbl.repository;

import com.lbl.dataObject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by 李柏霖
 * 2020-10-09 21:34
 */
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
    
    


//    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}

ProductCategoryRepositoryTest

package com.lbl.repository;

import com.lbl.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;

/**
 * Created by 李柏霖
 * 2020-10-09 21:35
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {


    @Autowired
    private ProductCategoryRepository repository;

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37924905/article/details/108987357