微服务时代Spring Boot企业微信点餐系统(二)------DAO层和Service层编写顺序

1.编写dataobject,对照数据库写好的sql

2.编写DAO层Repository,引入JPA

3.单元测试Repository类

4.编写Service接口

5.实现Service接口

6.单元测试Service类

 

1.com.imooc.dataobject.ProductCategory

@Entity
@DynamicUpdate
@Data
public class ProductCategory {
    /** 类目id. */
    @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;
    }
}

2.com.imooc.repository.ProductCategoryRepository

public interface ProductCategoryRepository extends JpaRepository{

    List findByCategoryTypeIn(List categoryTypeList);

}

3.com.imooc.repository.ProductCategoryRepositoryTest

@SpringBootTest
class ProductCategoryRepositoryTest {

    @Autowired
    private ProductCategoryRepository repository;

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

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

4.com.imooc.service.CategoryService

public interface CategoryService {

    ProductCategory findOne(Integer categoryId);

    List findAll();

    List findByCategoryTypeIn(List categoryTypeList);

    ProductCategory save(ProductCategory productCategory);
}

 5.com.imooc.service.impl.CategoryServiceImpl

@Service
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private ProductCategoryRepository repository;

    @Override
    public ProductCategory findOne(Integer categoryId) {
        return repository.findById(categoryId).orElse(null);
    }

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

    @Override
    public List findByCategoryTypeIn(List categoryTypeList) {
        return repository.findByCategoryTypeIn(categoryTypeList);
    }

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

 6.com.imooc.service.impl.CategoryServiceImplTest

@SpringBootTest
class CategoryServiceImplTest {

    @Autowired
    private CategoryServiceImpl categoryService;

    @Test
    void findOne() {
        ProductCategory productCategory = categoryService.findOne(1);
        Assertions.assertEquals(1, productCategory.getCategoryId());
    }

    @Test
    void findAll() {
    }

    @Test
    void findByCategoryTypeIn() {
    }

    @Test
    void save() {
        ProductCategory productCategory = new ProductCategory("男生专项", 10);
        ProductCategory result = categoryService.save(productCategory);
        Assertions.assertNotNull(result);
    }
}

 

自此product_category写完,此后的类似此过程

你可能感兴趣的:(springboot)