当前位置:   article > 正文

Service层代码的编写_service层怎么写

service层怎么写
  1. Service层学习总结:
  2. 1、在包下新增Service包
  3. 2、在包类添加CategoryService接口,在接口中添加需要用到的方法。
  4. 3、添加CategoryServiceImpl类,并实现CategoryService的方法,在类上添加注解@Service;注入repository.
  5. 4、测试类:在测试类中注入CategoryServiceImpl,写Service中的各个方法测试
  6. //Service接口
  7. package com.dairy.sell.service;
  8. import com.dairy.sell.dataobject.ProductCategory;
  9. import java.util.List;
  10. public interface CategoryService {
  11. ProductCategory findOne(Integer categoryId);
  12. List<ProductCategory> findAll();
  13. List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeId);
  14. ProductCategory save(ProductCategory productCategory);
  15. }
  16. //Service层实现
  17. package com.dairy.sell.service.impl;
  18. import com.dairy.sell.dataobject.ProductCategory;
  19. import com.dairy.sell.repository.ProductCategoryRepository;
  20. import com.dairy.sell.service.CategoryService;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import java.util.List;
  24. @Service
  25. public class CategoryServiceImpl implements CategoryService {
  26. @Autowired
  27. private ProductCategoryRepository productCategoryRepository;
  28. @Override
  29. public ProductCategory findOne(Integer categoryId) {
  30. return productCategoryRepository.findByCategoryId(categoryId);
  31. }
  32. @Override
  33. public List<ProductCategory> findAll() {
  34. return productCategoryRepository.findAll();
  35. }
  36. @Override
  37. public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeId) {
  38. return productCategoryRepository.findByCategoryTypeIn(categoryTypeId);
  39. }
  40. @Override
  41. public ProductCategory save(ProductCategory productCategory) {
  42. return productCategoryRepository.save(productCategory);
  43. }
  44. }
  45. //测试类
  46. package com.dairy.sell.service.impl;
  47. import com.dairy.sell.dataobject.ProductCategory;
  48. import org.junit.Assert;
  49. import org.junit.Test;
  50. import org.junit.runner.RunWith;
  51. import org.springframework.beans.factory.annotation.Autowired;
  52. import org.springframework.boot.test.context.SpringBootTest;
  53. import org.springframework.test.annotation.Rollback;
  54. import org.springframework.test.context.junit4.SpringRunner;
  55. import org.springframework.transaction.annotation.Transactional;
  56. import java.util.Arrays;
  57. import java.util.Date;
  58. import java.util.List;
  59. import static org.junit.Assert.*;
  60. @RunWith(SpringRunner.class)
  61. @SpringBootTest
  62. public class CategoryServiceImplTest {
  63. @Autowired
  64. private CategoryServiceImpl categoryService;
  65. @Test
  66. public void findOne() {
  67. ProductCategory productCategory = categoryService.findOne(1);
  68. Assert.assertNotEquals(new Integer(2), productCategory.getCategoryId());
  69. }
  70. @Test
  71. public void findAll() {
  72. List<ProductCategory> productCategory = categoryService.findAll();
  73. Assert.assertNotEquals(0, productCategory.size());
  74. }
  75. @Test
  76. public void findByCategoryTypeIn() {
  77. List<Integer> list = Arrays.asList(1, 2, 3, 4);
  78. List<ProductCategory> productCategory = categoryService.findByCategoryTypeIn(list);
  79. Assert.assertNotEquals(0, productCategory.size());
  80. }
  81. @Test
  82. @Transactional
  83. @Rollback(true)
  84. public void save() {
  85. ProductCategory productCategory = new ProductCategory(new Date(), new Date(), "鞋子", new Integer(02));
  86. ProductCategory productCategory1 = categoryService.save(productCategory);
  87. Assert.assertNotNull(productCategory1);
  88. }
  89. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/313518
推荐阅读
相关标签
  

闽ICP备14008679号