Spring Boot学习扎要1_2019.2.10-基于新版本微服务时代Spring Boot企业微信点餐系统

1.报错:Table 'sell.hibernate_sequence' doesn't exist

错误信息:Table 'sell.hibernate_sequence' doesn't exist

错误原因:实体主键没有配置主键自增长

完整配置如下
 /**主键id*/
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

2.Entity实体类注意事项

@Entity

//可以在更新数据时可以更新时间
@DynamicUpdate

//在Entity类中用@Data注解可以省略setXx\getXx、xxtoString方法
@Data
public class ProductCategory {
//    类目
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

3.测试类中

    @Test

//应用此注解在测试类中测试后不会存在数据库中,与数据库事务不同
    @Transactional

    public void saveTest(){
        ProductCategory productCategory = new ProductCategory("学生最爱",3);
        ProductCategory result = repository.save(productCategory);
        Assert.assertNotNull(result);
//        Assert.assertNotEquals(null,result);
    }

 

4.@Autowired

Field injection is not recommended 
 Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies"

https://www.cnblogs.com/wang-yaz/p/9340156.html

 

原来的书写方式

@Autowired
private ProductCategoryRepository repository;

通过构造器注入解决:

private ProductCategoryRepository repository;
@Autowired
public CategoryServiceImpl(ProductCategoryRepository repository){
    this.repository = repository;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值