Example.of查询的条件控制,怎么进行where查询?

【问题描述-】

使用Example.of(POJO)和findAll(example)查询时一直没有数据,返回的List集合的size()为0,导致重复的数据不断插入。

经搜索已知findAll(example)查询时会自动的忽略掉POJO中值为NULL的属性

【问题分析-】

设置spring.jpa.show-sql=true
观察日志生成SQL语句
select 

product0_.id as id1_0_, 
product0_.endtime as endtime2_0_, 
product0_.proauthor as proautho3_0_, 
product0_.probackcount as probackc4_0_, product0_.probackermoney as probacke5_0_, 
product0_.proid as proid6_0_, 
product0_.proimgpath as proimgpa7_0_, product0_.promoneypercent as promoney8_0_, 
product0_.prostatus as prostatu9_0_,
product0_.protitle as protitl10_0_, 
product0_.protype as protype11_0_, 
product0_.starttime as startti12_0_
 from md_item product0_ 

where product0_.id=0 and product0_.proid=?
可以注意到where后面有两个条件
  findAll(example)查询时会自动的忽略掉POJO中值为NULL的属性,给与Example.of的POJO的确只设置了一个值
//如果实体的属性是null,它就会忽略它,这里只传一个proId参数就好
Product proExample=new Product();
proExample.setProid(proId);
//查询并判断数据是否存在
List<Product> examples = productService.findAll(proExample);
System.out.println("list有无数据:"+examples.size());
if(examples.size()>0){
   System.out.println("===数据已存在===");
   continue;
}

那么product0_.id=0是怎么出现的呢?

 

【问题解决】

找到POJO类,原本POJO类的id属性对应数据库的属性,被设置为long型,所以出现了默认值0;

最终修改为String 问题解决。

findAll(Example)可以进行些许的String转换,故和数据库列数据类型不同也不会有问题。

posted @ 2021-09-06 11:49  YFEYI  阅读(715)  评论(0编辑  收藏  举报