2017-08-14 113 views
0
public static Specification<Model> listSpec(Query query) { 
    return (root, query, cb) -> { 
     List<Predicate> predicates = new ArrayList<>();  
     if (null != query.getKeyWord()) { 
      predicates.add(cb.like(root.get("title"), "\'%" + query.getKeyWord() + "%\'")); 
     } 
     return cb.and(predicates.toArray(new Predicate[predicates.size()])); 
    }; 

有调试春数据JPA标准像

Hibernate: select sell0_.cfid as cfid1_2_, sell0_.address as address2_2_, sell0_.area as area3_2_, sell0_.area_id as area_id4_2_, sell0_.browse as browse5_2_, sell0_.cf_thumb as cf_thumb6_2_, sell0_.cf_type as cf_type7_2_, sell0_.city as city8_2_, sell0_.city_id as city_id9_2_, sell0_.collection as collect10_2_, sell0_.constructionarea as constru11_2_, sell0_.creat_time as creat_t12_2_, sell0_.is_delete as is_dele13_2_, sell0_.foundertype as founder14_2_, sell0_.frontage as frontag15_2_, sell0_.industry as industr16_2_, sell0_.landarea as landare17_2_, sell0_.is_license as is_lice18_2_, sell0_.license_mark as license19_2_, sell0_.officearea as officea20_2_, sell0_.oldamountland as oldamou21_2_, sell0_.paymoney as paymone22_2_, sell0_.paystatus as paystat23_2_, sell0_.paytime as paytime24_2_, sell0_.phone as phone25_2_, sell0_.phonedisplay as phonedi26_2_, sell0_.point_x as point_x27_2_, sell0_.point_y as point_y28_2_, sell0_.propertysituation as propert29_2_, sell0_.province as provinc30_2_, sell0_.provinceid as provinc31_2_, sell0_.push_time as push_ti32_2_, sell0_.push_type as push_ty33_2_, sell0_.report as report34_2_, sell0_.sell_tag as sell_ta35_2_, sell0_.status as status36_2_, sell0_.taxation as taxatio37_2_, sell0_.title as title38_2_, sell0_.totalprice as totalpr39_2_, sell0_.transfermode as transfe40_2_, sell0_.unitprice as unitpri41_2_, sell0_.update_time as update_42_2_, sell0_.userid as userid43_2_, sell0_.workshoparea as worksho44_2_ from cf_sell sell0_ where sell0_.is_delete=0 and sell0_.is_license=1 and sell0_.status=1 and (sell0_.title like ?) order by sell0_.browse desc limit ?, ? 
+0

“调试后”告诉人们什么是SQL生成的,或者那些实体是什么,所以你正在执行的等效JPQL ... –

回答

1
predicates.add(cb.like(root.get("title"), "%" + query.getKeyWord() + "%")); 

尝试排除'字符后,如果您还没有包含前缘和后'字符标题没有匹配的结果。

+0

我试图删除单引号字符,但他们仍然没有工作 – Salt