导致这个问题的原因很多,这里记录一下我碰到的问题和解决方法。
网上有说时 pageNo要从0开始,我的不是这个问题。
在使用springboot jpa时,发现使用 t.ip like %?5% 语句,如果数据库记录的ip is null时,将查询不到该数据。更换为t.ip like concat(‘%’, ?5, ‘%’) )后,可以解决此问题。
@Query(value = " select t from SignAuditRequireLogEntity t where 1 = 1 " +
" and ( ?1 = 'null' or ?1 = '' or t.proposalNo = ?1 ) " +
" and ( ?2 = 'null' or ?2 = '' or t.proposalStatus like %?2% ) " +
" and ( ?3 = 'null' or ?3 = '' or t.userNo like %?3% ) " +
" and ( ?4 = 'null' or ?4 = '' or t.userName like %?4% ) " +
" and ( ?5 = 'null' or ?5 = '' or t.ip like concat('%', ?5, '%') ) " +
" order by t.createDate desc ")
Page<SignAuditRequireLogEntity> findByPage(String proposalNo, String proposalStatus
, String userNo, String userName, String ip, Pageable pageable);
ps:以后在JPA中使用like要用 like concat(‘%’, ?5, ‘%’) )方式
例子1
数据如下:
java代码:
@Query(value = " select t from SignAuditRequireLogEntity t where 1 = 1 " +
" and ( ?1 = 'null' or ?1 = '' or t.proposalNo = ?1 ) " +
" and ( ?2 = 'null' or ?2 = '' or t.proposalStatus like %?2% ) " +
" and ( ?3 = 'null' or ?3 = '' or t.userNo like %?3% ) " +
" and ( ?4 = 'null' or ?4 = '' or t.userName like %?4% ) " +
" and ( ?5 = 'null' or ?5 = '' or t.ip like concat('%', ?5, '%') ) " +
" order by t.createDate desc ")
Page<SignAuditRequireLogEntity> findByPage(String proposalNo, String proposalStatus
, String userNo, String userName, String ip, Pageable pageable);
效果如下:
例子2
我们把例子1中的proposal_status字段改为空字符串。
数据如下:
java代码:
@Query(value = " select t from SignAuditRequireLogEntity t where 1 = 1 " +
" and ( ?1 = 'null' or ?1 = '' or t.proposalNo = ?1 ) " +
" and ( ?2 = 'null' or ?2 = '' or t.proposalStatus like %?2% ) " +
" and ( ?3 = 'null' or ?3 = '' or t.userNo like %?3% ) " +
" and ( ?4 = 'null' or ?4 = '' or t.userName like %?4% ) " +
" and ( ?5 = 'null' or ?5 = '' or t.ip like concat('%', ?5, '%') ) " +
" order by t.createDate desc ")
Page<SignAuditRequireLogEntity> findByPage(String proposalNo, String proposalStatus
, String userNo, String userName, String ip, Pageable pageable);
效果如下:我们就能查到数据了