当前位置: 移动技术网 > IT编程>数据库>MSSQL > JPA动态拼接where条件

JPA动态拼接where条件

2020年06月28日  | 移动技术网IT编程  | 我要评论
在开发中,经常会涉及到动态拼接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拼接条件案例:两种方法大同小异,只不过语法方面不同

mysql: 使用三元运算符的方式

    @Query(value = "select * from user a where if(?1 !='',id=?1 ,1=1  ) ",nativeQuery = true)
    public List<User> findPersonById(String id);

orcal:使用decode函数拼接

    @Query(value = "select * from prpsuser a where id=decode(?1 , null,id, ?1 ) ",nativeQuery = true)
    public List<User> findPersonById(String id);

 

写了一个简单的根据id查找用户的案例,首先不输入id,则返回所有数据,如下:

如果输入id则只返回对应的用户:

 

 

本文地址:https://blog.csdn.net/jungeCSND/article/details/106993922

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网