微信点餐 spring-boot -商品类目
1/问题数据库中的字段和映射中的不一致 常见错误
数据库中列名 catagory_type 实体中的属性categoryType,数据库中的列名改为category_type
2/关于:Table'XXX.hibernate_sequence' doesn't exist
将ID生成略组改成@GeneratedValue(strategy = GenerationType.IDENTITY).
3/实体类的名称和表明不一致
@Table(name="s_product_category")
public class ProductCategory {
4/更新字段时自动更新时间
见表语句
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP , `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
实体类:注解
@DynamicUpdate
ublic class ProductCategory { //类目id @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer categoryId; //类目名称 private String categoryName; //类目编号 private Integer categoryType; private Date createTime; private Date updateTime;}
使用注解@DynamicUpdate的时候要把createtime和updateime去掉
方法是先查询在更改
4/lombok 插件使用
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
@DATA 在实体类中会生成setter和getter方法以及tostring()
5/测试方法里面不要存到数据库我们可以使用@Transactional标签,这个事情完全回滚不会在数据库中留下记录
@Test @Transactional public void saveTest(){ ProductCategory productCategory=new ProductCategory("女生最爱",12); ProductCategory save = productCategoryRepository.save(productCategory); Assert.assertNotNull(save); }
6/springjpa 构造自定义的查询方法时一定要在实体类有对应的无参的构造方法
springjpa repositroy方法命名有要求findbyCategoryType会报如下错误(findByCategoryType)
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findbyCategoryType found for type ProductCategory!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findbyCategoryType found for type ProductCategory!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358)