jap @Query多条件灵活查询

阅读 128

2022-03-26

1. MySQL使用if进行条件判空



@Query(nativeQuery=true,value="select * from company where if(:code is not null and :code != '' , code =:code,1=1)")
List<Company> getCompanyList(@Param("code") String code);


2. 使用case when 进行条件判空


@Query(value = "select * from company where (1=(case when :code is null then 1 else 0 end) or code =:code ))",nativeQuery = true)
List<Company> getCompanyList(@Param("code") String code);

测试sqlServer和mysql都支持

精彩评论(0)

0 0 举报