Springboot 2.x 开发过程中的注意点

1.@Transactional
public void saveTest(){
ProductCategory productCategory = new ProductCategory("男生最爱",4);
ProductCategory result = repository.save(productCategory);
Assert.assertNotNull(result);
}

当进行测试后 不想在数据库中留下测试数据 可以添加注解@Transcational

2.端口8080被占用
直接cmd 输入 netstat -ano|findstr 8080 然后会有个ID出来 直接KILL taskkill /f /t /im XXXX

3.回顾一整个查询
  1)dependency的依赖
  2)application.yml的配置
  3)创建数据库的对象(其实就是表的映射)
  4)DAO层的应用
  5)进行单元测试(提到了Lombok插件的使用)

4.顺序 DAO --> repository --> service(写一个接口) --> implements(实现接口)

5.service端需要写一个@Service注解 还需要引入
@Autowired
private ProductCategoryRepository repository;

6.写完implements 全部测试下
go to test 然后几个接口都写上
新的test要写上注释
@RunWith(SpringRunner.class)
@SpringBootTest

7.关于测试
1)findOne:
ProductCategory productCategory = repository.getOne(2);
Assert.assertEquals(new Integer(2),productCategory.getCategoryId());
先去getOne(2), Assert.assertEquals() 前面参数为期待的值,后面的值为实际的值,如果一样, 则证明找到
2)findAll:
List<ProductCategory> productCategoryList = categoryService.findAll();
Assert.assertNotEquals(0,((List) productCategoryList).size());
通过这个list的size()去判断 如果大于0 就说明有
3)findByCategoryTypeIn:
List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(Arrays.asList(1,2,3));
Assert.assertNotEquals(0,productCategoryList.size()); 只要大于0 就说明有
4)save:
ProductCategory productCategory = new ProductCategory("男生不喜欢",10);
ProductCategory result = categoryService.save(productCategory);
Assert.assertNotNull(result);只要result不是空,就说明成功插入了
 
原文地址:https://www.cnblogs.com/superblog/p/10441229.html