4-3 买家类目-service
DAO:ProductCategory。Service可以简化一些,叫CategoryService。
package com.imooc.sell.service; import com.imooc.sell.dataobject.ProductCategory; import java.util.List; /** * 类目 * Created by zhongzh * 2018-05-26 23:53 */ public interface CategoryService { ProductCategory findOne(Integer categoryId);//管理后台使用 List<ProductCategory> findAll();//管理后台用的 List<ProductCategory> findByCategoryTypeInTest(List<Integer> categoryTypeList); //新增和更新都是save方法 ProductCategory save(ProductCategory productCategory); }
package com.imooc.sell.service.impl; import com.imooc.sell.dataobject.ProductCategory; import com.imooc.sell.repository.ProductCategoryRepository; import com.imooc.sell.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by zhongzh * 2018-05-27 0:02 */ @Service //Service端要加一个注解 public class CategoryServiceImpl implements CategoryService { @Autowired private ProductCategoryRepository repository;//引入DAO @Override public ProductCategory findOne(Integer categoryId) { return repository.getOne(categoryId); } @Override public List<ProductCategory> findAll() { return repository.findAll(); } @Override public List<ProductCategory> findByCategoryTypeInTest(List<Integer> categoryTypeList) { return repository.findByCategoryTypeIn(categoryTypeList); } @Override public ProductCategory save(ProductCategory productCategory) { return repository.save(productCategory); } }
com.imooc.sell.service.impl.CategoryServiceImpl的四个方法都给它测试一下

测试类
package com.imooc.sell.service.impl; import org.junit.Test; import static org.junit.Assert.*; public class CategoryServiceImplTest { @Test public void findOne() { } @Test public void findAll() { } @Test public void findByCategoryTypeInTest() { } @Test public void save() { } }
增加注解
package com.imooc.sell.service.impl; import com.imooc.sell.dataobject.ProductCategory; import org.junit.Assert; 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; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class CategoryServiceImplTest { @Autowired private CategoryServiceImpl categoryService; @Test public void findOne() { ProductCategory productCategory = categoryService.findOne(1); Assert.assertEquals(new Integer(1),productCategory.getCategoryId()); } @Test public void findAll() { } @Test public void findByCategoryTypeInTest() { } @Test public void save() { } }
com.imooc.sell.dataobject.ProductCategory要增加一个getCategoryId()方法才行
package com.imooc.sell.dataobject; //import javax.persistence.Table; import lombok.Data; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.Proxy; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.util.Date; /** * 类目 * Created by zhongzh * 2018-05-20 9:31 * s_product_category */ //@Table(name = "s_product_category") @Entity//把数据库映射成对象 @Proxy(lazy = false) @DynamicUpdate // @Data//@Data包含生成getter、setter和toString()方法 //@Getter//如果只是需要Getter那就引入Getter //@Setter//如果只是需要Setter那就引入Setter //@ToString//如果只是需要ToString那就引入ToString public class ProductCategory{ /** 类目id. */ @Id//Id是主键,自增类型的。 //@GeneratedValue//相当于调用了native策略 //@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer categoryId;//字段名的命名方式也是一样的,把下划线改成为空。 /** 类目名字. */ private String categoryName; /** 类目编号. */ private Integer categoryType; /** 创建时间. */ private Date createTime; /** 修改时间. */ private Date updateTime; //不要忘了Getter和Setter方法 /* public Integer getCategoryId() { return categoryId; } public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } public String getCategoryName() { return categoryName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public Integer getCategoryType() { return categoryType; } public void setCategoryType(Integer categoryType) { this.categoryType = categoryType; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "ProductCategory{" + "categoryId=" + categoryId + ", categoryName='" + categoryName + '\'' + ", categoryType=" + categoryType + '}'; } */ public ProductCategory(String categoryName, Integer categoryType) { this.categoryName = categoryName; this.categoryType = categoryType; } public ProductCategory() { super(); } public Integer getCategoryId() { return categoryId; } }

package com.imooc.sell.service.impl; import com.imooc.sell.dataobject.ProductCategory; import org.junit.Assert; 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; import java.util.Arrays; import java.util.List; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class CategoryServiceImplTest { @Autowired private CategoryServiceImpl categoryService; @Test public void findOne() { ProductCategory productCategory = categoryService.findOne(10); Assert.assertEquals(new Integer(1),productCategory.getCategoryId()); } @Test public void findAll() { List<ProductCategory> productCategoryList = categoryService.findAll(); Assert.assertNotEquals(0,((List) productCategoryList).size());//如果集合的总数不等于null,如果productCategoryList的size不等于0,至少是有内容的。 } @Test public void findByCategoryTypeInTest() { List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeInTest(Arrays.asList(1,2,4)); Assert.assertNotEquals(0,productCategoryList.size()); } @Test public void save() { ProductCategory productCategory = new ProductCategory("男生专享",10); ProductCategory result = categoryService.save(productCategory); Assert.assertNotNull(result); } }

因为类目并没有给买家端提供专门的接口,所以类目相关Controller层不用再写了。而是商品,下一节开发商品相关的功能。
浙公网安备 33010602011771号