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
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
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
3. Objects mapped from the new data table
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
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
2. Create a new CategoryService interface in the service package
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
4. Create a new implementation class CategoryServiceImpl of the CategoryService interface in the com.bdu.service.impl package
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
2) Reason: ProductCategoryRepositoryTest class is not a public class
- The class com.bdu.repository.ProductCategoryRepositoryTest is not public.
- Test class should have exactly one public constructor
after modification
3) Table 'sell.hibernate_sequence' doesn't exist
Before modification:
After modification: