在開發中,經常會涉及到動態拼接sql,以下就是JPA使用三元運算符的方式拼接條件案例:
@Query(value = "select * from user a where if(?1 !='',id=?1 ,1=1 ) ",nativeQuery = true)
public List<User> findPersonById(String id);
寫了一個簡單的根據id查找用戶的案例,首先不輸入id,則返回所有數據,如下:
如果輸入id則只返回對應的用戶:
在開發中,經常會涉及到動態拼接sql,以下就是JPA使用三元運算符的方式拼接條件案例:
@Query(value = "select * from user a where if(?1 !='',id=?1 ,1=1 ) ",nativeQuery = true)
public List<User> findPersonById(String id);
寫了一個簡單的根據id查找用戶的案例,首先不輸入id,則返回所有數據,如下:
如果輸入id則只返回對應的用戶: