MVC - service:
service包含接口interface和接口的实现implements
interface中主要是接口的方法声明。声明出前端和后端可能使用的接口方法。
implements中主要是实现interface中声明的方法。
要点:
1.需要在类头加@Service
2.implements后跟interface类名
3.@Autowired引入Repository存储库操作类,然后实现各个interface中声明的方法
4.command+n 弹出Generate,选择override method,选中全部需要实现的方法
5.单元测试时测试实现implements类
其他知识点:
Integer对象化 : new Integer(1)
IDEA快速换行快捷键:command + shift + 回车
代码
package com.gang.sell.service;
/*
* 类目
*
* */
import com.gang.sell.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);
}
package com.gang.sell.service.impl;
import com.gang.sell.dataobject.ProductCategory;
import com.gang.sell.repository.ProductCategoryRepository;
import com.gang.sell.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();
}
@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);
}
}
网友评论