博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
商品类目dao+service
阅读量:6910 次
发布时间:2019-06-27

本文共 3487 字,大约阅读时间需要 11 分钟。

类名驼峰式对应表名下划线

1、创建ProductCategory

@Entitypublic class ProductCategory {        /** 类目id. */    @Id    @GeneratedValue    private Integer categoryId;    /** 类目名字. */    private String categoryName;    /* 类目编号. */    private Integer categoryType;} +get set方法

2、建立ProductCategoryRepository

public interface ProductCategoryRepository extends JpaRepository
{ List
findByCategoryTypeIn(List
categoryTypeList);}

3、对 ProductCategoryRepository 接口测试

@RunWith(SpringRunner.class)@SpringBootTestpublic class ProductCategoryRepositoryTest {    @Autowired    private ProductCategoryRepository repository;    @Test    public void findOneTest() {        ProductCategory productCategory = repository.findById(1).get();        System.out.println(productCategory.toString());    }    @Test    @Transactional //不会污染数据库    public void saveTest(){        ProductCategory productCategory = new ProductCategory("男生最爱",4);        ProductCategory result = repository.save(productCategory);        Assert.assertNotNull(result);        //Assert.assertNotEquals(null,result);    }    @Test        public void findByCategoryTypeInTest(){            List
list = Arrays.asList(2,3,4); List
result = repository.findByCategoryTypeIn(list); Assert.assertNotEquals(0,result.size()); }}

 4、service层

建立CategoryService:

public interface CategoryService {    ProductCategory findOne(Integer categoryId);    List
findAll(); List
findByCategoryTypeIn(List
categoryTypeList); ProductCategory save(ProductCategory productCategory);}

建立CategoryServiceImpl:

@Servicepublic class CategoryServiceImpl implements CategoryService {    @Autowired    private ProductCategoryRepository repository;    @Override    public ProductCategory findOne(Integer categoryId) {        return repository.findById(categoryId).get();    }    @Override    public List
findAll() { List
productCategoryList = repository.findAll(); return repository.findAll(); } @Override public List
findByCategoryTypeIn(List
categoryTypeList) { return repository.findByCategoryTypeIn(categoryTypeList); } @Override public ProductCategory save(ProductCategory productCategory) { return repository.save(productCategory); }}

5、对Service测试

@RunWith(SpringRunner.class)@SpringBootTestpublic class CategoryServiceImplTest {    @Autowired    private CategoryServiceImpl categoryService;    @Test    public void findOne() {        ProductCategory productCategory = categoryService.findOne(7);        Assert.assertEquals(new Integer(7),productCategory.getCategoryId());    }    @Test    public void findAll() {        List
productCategoryList = categoryService.findAll(); Assert.assertNotEquals(0,productCategoryList.size()); } @Test public void findByCategoryTypeIn() { List
productCategoryList = categoryService.findByCategoryTypeIn(Arrays.asList(4,5,6)); Assert.assertNotEquals(0,productCategoryList.size()); } @Test public void save() { ProductCategory productCategory = new ProductCategory("男生专享的",12); ProductCategory result = categoryService.save(productCategory); Assert.assertNotNull(result); }}

 

转载于:https://www.cnblogs.com/Evangenia/p/10065014.html

你可能感兴趣的文章
计算机软件著作权查询网址
查看>>
一起谈.NET技术,.Net4.0 Parallel编程(四)Task 上
查看>>
自定义Status Bar的基本方法
查看>>
react动画难写?试试react版transformjs
查看>>
Chrome(12)中使用getComputedStyle获取透明度(opacity)返回字符串不同于其它浏览器...
查看>>
【汉字乱码】IE下GET形式传递汉字。
查看>>
SmartImageView
查看>>
《FineUI秘密花园》在线阅读与完整PDF版
查看>>
android 混淆相关 proguard
查看>>
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
查看>>
android TDD平台插入双卡时,查看允许返回发送报告的选项,去掉勾选,不起作用...
查看>>
2013年8月第2个周结
查看>>
(转)C的代码是如何变成程序的
查看>>
Udp SocketAsyncEventArgs SocketAsyncDataHandler
查看>>
音频处理平台
查看>>
jQuery(function(){})与(function(){})(jQuery)的区别
查看>>
android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
查看>>
为什么需要在TypedArray后调用recycle
查看>>
安装windows7、windows8.1提示无法创建新的分区
查看>>
SpringAOP
查看>>