1. 程式人生 > 其它 >Example.of查詢的條件控制,怎麼進行where查詢?

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轉換,故和資料庫列資料型別不同也不會有問題。