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;
}
发布了22 篇原创文章 · 获赞 0 · 访问量 456

猜你喜欢

转载自blog.csdn.net/qq_36956082/article/details/104252454
今日推荐