DAO:ProductCategory。Service可以简化一些,叫CategoryService。

  1. package com.imooc.sell.service;
  2.  
  3. import com.imooc.sell.dataobject.ProductCategory;
  4.  
  5. import java.util.List;
  6.  
  7. /**
  8. * 类目
  9. * Created by zhongzh
  10. * 2018-05-26 23:53
  11. */
  12. public interface CategoryService {
  13. ProductCategory findOne(Integer categoryId);//管理后台使用
  14. List<ProductCategory> findAll();//管理后台用的
  15. List<ProductCategory> findByCategoryTypeInTest(List<Integer> categoryTypeList);
  16. //新增和更新都是save方法
  17. ProductCategory save(ProductCategory productCategory);
  18. }
  1. package com.imooc.sell.service.impl;
  2.  
  3. import com.imooc.sell.dataobject.ProductCategory;
  4. import com.imooc.sell.repository.ProductCategoryRepository;
  5. import com.imooc.sell.service.CategoryService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import java.util.List;
  10.  
  11. /**
  12. * Created by zhongzh
  13. * 2018-05-27 0:02
  14. */
  15.  
  16. @Service //Service端要加一个注解
  17. public class CategoryServiceImpl implements CategoryService {
  18.  
  19. @Autowired
  20. private ProductCategoryRepository repository;//引入DAO
  21.  
  22. @Override
  23. public ProductCategory findOne(Integer categoryId) {
  24. return repository.getOne(categoryId);
  25. }
  26.  
  27. @Override
  28. public List<ProductCategory> findAll() {
  29. return repository.findAll();
  30. }
  31.  
  32. @Override
  33. public List<ProductCategory> findByCategoryTypeInTest(List<Integer> categoryTypeList) {
  34. return repository.findByCategoryTypeIn(categoryTypeList);
  35. }
  36.  
  37. @Override
  38. public ProductCategory save(ProductCategory productCategory) {
  39. return repository.save(productCategory);
  40. }
  41. }

com.imooc.sell.service.impl.CategoryServiceImpl的四个方法都给它测试一下

测试类

  1. package com.imooc.sell.service.impl;
  2.  
  3. import org.junit.Test;
  4.  
  5. import static org.junit.Assert.*;
  6.  
  7. public class CategoryServiceImplTest {
  8.  
  9. @Test
  10. public void findOne() {
  11. }
  12.  
  13. @Test
  14. public void findAll() {
  15. }
  16.  
  17. @Test
  18. public void findByCategoryTypeInTest() {
  19. }
  20.  
  21. @Test
  22. public void save() {
  23. }
  24. }

增加注解

  1. package com.imooc.sell.service.impl;
  2.  
  3. import com.imooc.sell.dataobject.ProductCategory;
  4. import org.junit.Assert;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10.  
  11. import static org.junit.Assert.*;
  12.  
  13. @RunWith(SpringRunner.class)
  14. @SpringBootTest
  15. public class CategoryServiceImplTest {
  16.  
  17. @Autowired
  18. private CategoryServiceImpl categoryService;
  19.  
  20. @Test
  21. public void findOne() {
  22. ProductCategory productCategory = categoryService.findOne(1);
  23. Assert.assertEquals(new Integer(1),productCategory.getCategoryId());
  24. }
  25.  
  26. @Test
  27. public void findAll() {
  28. }
  29.  
  30. @Test
  31. public void findByCategoryTypeInTest() {
  32. }
  33.  
  34. @Test
  35. public void save() {
  36. }
  37. }

com.imooc.sell.dataobject.ProductCategory要增加一个getCategoryId()方法才行

  1. package com.imooc.sell.dataobject;
  2.  
  3. //import javax.persistence.Table;
  4.  
  5. import lombok.Data;
  6. import lombok.Getter;
  7. import lombok.Setter;
  8. import lombok.ToString;
  9. import org.hibernate.annotations.DynamicUpdate;
  10. import org.hibernate.annotations.Proxy;
  11.  
  12. import javax.persistence.Entity;
  13. import javax.persistence.GeneratedValue;
  14. import javax.persistence.GenerationType;
  15. import javax.persistence.Id;
  16. import java.util.Date;
  17.  
  18. /**
  19. * 类目
  20. * Created by zhongzh
  21. * 2018-05-20 9:31
  22. * s_product_category
  23. */
  24. //@Table(name = "s_product_category")
  25. @Entity//把数据库映射成对象
  26. @Proxy(lazy = false)
  27. @DynamicUpdate //
  28. @Data//@Data包含生成getter、setter和toString()方法
  29. //@Getter//如果只是需要Getter那就引入Getter
  30. //@Setter//如果只是需要Setter那就引入Setter
  31. //@ToString//如果只是需要ToString那就引入ToString
  32. public class ProductCategory{
  33. /** 类目id. */
  34. @Id//Id是主键,自增类型的。
  35. //@GeneratedValue//相当于调用了native策略
  36. //@GeneratedValue(strategy = GenerationType.AUTO)
  37. @GeneratedValue(strategy = GenerationType.IDENTITY)
  38. private Integer categoryId;//字段名的命名方式也是一样的,把下划线改成为空。
  39. /** 类目名字. */
  40. private String categoryName;
  41.  
  42. /** 类目编号. */
  43. private Integer categoryType;
  44. /** 创建时间. */
  45. private Date createTime;
  46. /** 修改时间. */
  47. private Date updateTime;
  48. //不要忘了Getter和Setter方法
  49. /*
  50. public Integer getCategoryId() {
  51. return categoryId;
  52. }
  53.  
  54. public void setCategoryId(Integer categoryId) {
  55. this.categoryId = categoryId;
  56. }
  57.  
  58. public String getCategoryName() {
  59. return categoryName;
  60. }
  61.  
  62. public void setCategoryName(String categoryName) {
  63. this.categoryName = categoryName;
  64. }
  65.  
  66. public Integer getCategoryType() {
  67. return categoryType;
  68. }
  69.  
  70. public void setCategoryType(Integer categoryType) {
  71. this.categoryType = categoryType;
  72. }
  73.  
  74. public Date getCreateTime() {
  75. return createTime;
  76. }
  77.  
  78. public void setCreateTime(Date createTime) {
  79. this.createTime = createTime;
  80. }
  81.  
  82. public Date getUpdateTime() {
  83. return updateTime;
  84. }
  85.  
  86. public void setUpdateTime(Date updateTime) {
  87. this.updateTime = updateTime;
  88. }
  89.  
  90. @Override
  91. public String toString() {
  92. return "ProductCategory{" +
  93. "categoryId=" + categoryId +
  94. ", categoryName='" + categoryName + '\'' +
  95. ", categoryType=" + categoryType +
  96. '}';
  97. }
  98. */
  99.  
  100. public ProductCategory(String categoryName, Integer categoryType) {
  101. this.categoryName = categoryName;
  102. this.categoryType = categoryType;
  103. }
  104.  
  105. public ProductCategory() {
  106. super();
  107. }
  108.  
  109. public Integer getCategoryId() {
  110. return categoryId;
  111. }
  112. }

  1. package com.imooc.sell.service.impl;
  2.  
  3. import com.imooc.sell.dataobject.ProductCategory;
  4. import org.junit.Assert;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10.  
  11. import java.util.Arrays;
  12. import java.util.List;
  13.  
  14. import static org.junit.Assert.*;
  15.  
  16. @RunWith(SpringRunner.class)
  17. @SpringBootTest
  18. public class CategoryServiceImplTest {
  19.  
  20. @Autowired
  21. private CategoryServiceImpl categoryService;
  22.  
  23. @Test
  24. public void findOne() {
  25. ProductCategory productCategory = categoryService.findOne(10);
  26. Assert.assertEquals(new Integer(1),productCategory.getCategoryId());
  27. }
  28.  
  29. @Test
  30. public void findAll() {
  31. List<ProductCategory> productCategoryList = categoryService.findAll();
  32. Assert.assertNotEquals(0,((List) productCategoryList).size());//如果集合的总数不等于null,如果productCategoryList的size不等于0,至少是有内容的。
  33. }
  34.  
  35. @Test
  36. public void findByCategoryTypeInTest() {
  37. List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeInTest(Arrays.asList(1,2,4));
  38. Assert.assertNotEquals(0,productCategoryList.size());
  39. }
  40.  
  41. @Test
  42. public void save() {
  43. ProductCategory productCategory = new ProductCategory("男生专享",10);
  44. ProductCategory result = categoryService.save(productCategory);
  45. Assert.assertNotNull(result);
  46. }
  47. }


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

4-3 买家类目-service的更多相关文章

  1. 4-2 买家类目-dao(下)

    查询出来的对象ProductCategory就已经有updateTime和createTime了,然而你只是把对象的categoryType给修改了一下,修改之后就执行save方法保存了.所以它还是原 ...

  2. 如何增加亚马逊listing多个类目节点

    流量是电商销售的必要因素,可以说,任何成功的电商平台都离不开流量.亚马逊listing优化做得好,不仅能提高产品的曝光率,还能提升转换率,而好的类目可以吸引大的流量.帮你快速爬升. 首先我们来了解一下 ...

  3. 干货 | 揭秘如何增加listing多个类目节点

    流量是电商销售的必要因素,可以说,任何成功的电商平台都离不开流量.亚马逊listing优化做得好,不仅能提高产品的曝光率,还能提升转换率,而好的类目可以吸引大的流量.帮你快速爬升. 首先我们来了解一下 ...

  4. JAVAEE——宜立方商城03:商品类目选择、Nginx端口或域名区分虚拟机、Nginx反向代理、负载均衡、keepalived实现高可用

    1. 学习计划 第三天: 1.商品类目选择(EasyUI的tree实现) 2.图片上传 a) 图片服务器FastDFS(Nainx部分) 2. 商品类目选择 2.1. 原型 2.2. 功能分析 展示商 ...

  5. 商品类目和商品大广告的Redis缓存

    (dubbo)主要的实现类: 商品类目的Redis缓存 com.bjsxt.ego.portal.service.impl.PortalItemCatServiceImpl package com.b ...

  6. Objective-C中的类目,延展,协议

    Objective-C中的类目(Category),延展(Extension),协议(Protocol)这些名词看起来挺牛的,瞬间感觉OC好高大上.在其他OOP语言中就没见过这些名词,刚看到这三个名词 ...

  7. OC中协议, 类目, 时间, 延展, 属性

    只有继承和协议需要引IMPORT "头文件"; 必须接受marryprotocol协议, id<marryprotocol>基于类型的限定, 才能给实例变量赋值 @pr ...

  8. Objective - C - 添加类目 - NSDate

    1.类目为系统内部的类或者是没有源代码的类添加方法,不有添加实例变量 2.添加的方法会成为原类的一部分,子类照样可以使用 3.类目的文件名为原类名+文件名 4.既可以添加实例方法,也可以添加类方法 X ...

  9. Objective-C学习笔记类目、协议

    不是所有的方法都可以被覆盖的!比如:intValue就不能被覆盖!! 原因正在查找中! 别人的电脑上却可以! 类目.h件 #import <Foundation/Foundation.h> ...

随机推荐

  1. 第十七节:Scrapy爬虫框架之item.py文件以及spider中使用item

    Scrapy原理图: item位于原理图的最左边 item.py文件是报存爬取数据的容器,他使用的方法和字典很相似,但是相比字典item多了额外的保护机制,可以避免拼写错误或者定义错误. 1.创建it ...

  2. UVa 122 树的层次遍历

    题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; st ...

  3. 将json格式转为url参数格式的方法(xjl456852整理修改)

    测试页面: <html> <head> <script type="text/javascript" src="jquery-1.11.3. ...

  4. 关于SELECT 逻辑的执行顺序问题

    不会有大多数人都和我一样的认为,是先进行的Where 剔除结果集,再进行Join的吧 SQL server 2014 逻辑执行标准: https://msdn.microsoft.com/en-us/ ...

  5. 【01】emmet系列之基础介绍

    [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 相关网址 官网:http: ...

  6. Fiddler简介与Web抓包,远程抓包(IE、360、谷歌、火狐)

    Fiddler简介以及web抓包 一.Fiddler简介 简单来说,Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯.网上简介很多,我们不多说. 二 ...

  7. msp430入门学习00

    在TI官网上找到MSP430的程序例程.数据手册.使用指南等文件.以MSP430F169为例,步骤如下: 1)进入ti官网:http://www.ti.com.cn/ 或者http://www.ti. ...

  8. hdu - 1150 Machine Schedule (二分图匹配最小点覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两种机器,A机器有n种模式,B机器有m种模式,现在有k个任务需要执行,没切换一个任务机器就需要重启一次, ...

  9. 51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先 ...

  10. MyBatis 3在Insert之后返回主键

    XML: <insert id="addUser" parameterType="User" useGeneratedKeys="true&qu ...