WeChat Ordering System Chapter 4 - Buyer Category

1. Add dependencies

1. Added Mysql driver (Mysql dependency)

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

2. Tools for operating the database (spring-boot-starter-data-jpa)

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

3. Tools to automatically write getter and setter methods

 <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
 </dependency>

Need to be used with the lombok plugin
insert image description here

Second, configure the database information

1. Find the application.properties file under the resources file and modify its suffix to .yml
Note: Why use the .yml format instead of the .properties format?
Because the .yml format can make the configuration more concise
insert image description here

insert image description here
2. Configure database information

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

insert image description here

3. Objects mapped from the new data table

insert image description here

insert image description here

package com.bdu.dataobject;


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

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


@Entity
@DynamicUpdate
@Data
public class ProductCategory {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer categoryId;
  private String categoryName;
  private Integer categoryType;

  public ProductCategory() {
  }

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

4. Dao layer

1. Create a new package named com.bdu.repository
insert image description here
2. Create a new ProductCategoryRepository interface under the repository package to inherit JpaRepository
3. Write code

package com.bdu.repository;

import com.bdu.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author wanqing
 * @date 2022/1/17 17:26
 */
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
//            查看类目列表
    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryType);
}

Five, service layer

1. Create a new package named com.bdu.service
insert image description here
2. Create a new CategoryService interface in the service package
insert image description here

package com.bdu.service;

import com.bdu.dataobject.ProductCategory;

import java.util.List;

public interface CategoryService {

    //查询一条记录
    ProductCategory findOne(Integer categoryId);

    //查看类目列表
    List<ProductCategory> findAll();

    //买家端
    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);

    //卖家端-新增、更新
    ProductCategory save(ProductCategory productCategory);

}


3. Create a new com.bdu.service.impl package to store the interface implementation class
insert image description here
4. Create a new implementation class CategoryServiceImpl of the CategoryService interface in the com.bdu.service.impl package
insert image description here

package com.bdu.service.impl;

import com.bdu.dataobject.ProductCategory;
import com.bdu.repository.ProductCategoryRepository;
import com.bdu.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private ProductCategoryRepository repository;

    @Override
    public ProductCategory findOne(Integer categoryId) {
//        return repository.findById(categoryId).get();

        return repository.findById(categoryId).get();
    }

    @Override
    public List<ProductCategory> findAll() {
        return repository.findAll();
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return repository.findByCategoryTypeIn(categoryTypeList);
    }

    @Override
    public ProductCategory save(ProductCategory productCategory) {
        return repository.save(productCategory);
    }
}

6. Errors encountered

1) java: The method findOne in the interface org.springframework.data.repository.query.QueryByExampleExecutor cannot be applied to the given type;
insert image description here

2) Reason: ProductCategoryRepositoryTest class is not a public class

  1. The class com.bdu.repository.ProductCategoryRepositoryTest is not public.
  2. Test class should have exactly one public constructor insert image description here
    after modification
    insert image description here

3) Table 'sell.hibernate_sequence' doesn't exist
insert image description here
Before modification:
insert image description here
After modification:
insert image description here

Guess you like

Origin blog.csdn.net/Silly011/article/details/122543447