SpringBoot JPA实践之@Query查询之参数传参方式

JPA
placeholder image
admin 发布于:2020-04-29 23:07:45
阅读:loading

@Query注解查询是一个非常强大的查询实现,基本上日常的各种复杂查询使用它均可实现,本篇文章主要介绍使用@Query查询时的参数传递实现方式,常用的参数传递有3中方式,分别是“?”、“?数字”、“:属性”,第一种最常见,但是不能用于动态个数的参数,后面两种则可以满足动态参数个数的场景,所以后面的两种更加强大,但是使用“:属性”的方式比使用“?数字”的方式更加易于阅读,所以我也建议使用第三种方式。

参数解析实现

“?”:与JDBC中的参数赋值类似,这个类型的大家肯定不会陌生,当一个参数被使用一次以上时,此种方式需要定义两个参数,并且不支持动态个数的参数;

“?数字”:可以满足一个参数被使用多次,并且某个参数类型是数组或者集合的场景,底层解析逻辑是识别到特定规则的数字时候,替换为对应的第数字个参数(猜的);

“:属性”:同上,更加易于阅读,底层解析逻辑是识别特殊规则的属性名与方法中方法参数命名的匹配(猜的);

:#{#对象名称.属性名称}:既:#{#user.name},主要用于方法参数较多的情况,由此方式可以来避免方法出现更多的参数

代码实现

Repository

/**
 * 
查询参数类型传值方式
 
*
 * @author chendd
 * @date 2020/4/30 23:26
 */
public interface UserQueryParamTypeRepository extends BaseRepository<User, String> {

    
/**
     * 
第一种参数传递,使用 占位符
     
传统预编译参数传值方式
     
*/
    
@Query(value = "select * from user where name = ? " +
            
"and email like concat(concat('%',?),'%')" , nativeQuery = true)
    List<User> queryUsersByNameAndEmailForOneType(String name , String email);

    
/**
     * 
第二种参数传递,使用 ?数字 占位符
     
* ?数字释义:?为占位符,数字为获取的参数下标
     
*/
    
@Query(value = "select * from user where (name = ?1 or ?1 is null) " +
            
"and email like concat(concat('%',?2),'%')" , nativeQuery = true)
    List<User> queryUsersByNameAndEmailForTwoType(String name , String email);

    
/**
     * 
第三种参数传递,使用 :属性名 占位符
     
用于参数个数为动态长度时,支持数组与集合
     
*/
    
@Query(value = "select * from user where name in (:nameList) and name in (:nameArray) " +
            
"and email like concat(concat('%', :email ),'%')" , nativeQuery = true)
    List<User> queryUsersByNameAndEmailForThreeType(
@Param("nameList") List<String> nameList ,
                                                   
@Param("nameArray") String nameArray[] ,
                                                   
@Param("email") String email);

}

Test

/**
 * Query
查询参数传值方式
 
*
 * @author chendd
 * @date 2020/4/30 23:25
 */
public class UserQueryParamTypeTest extends ApplicationTest {

    
@Resource
    
private UserQueryParamTypeRepository repository;

    
@Test
    
public void testQueryByNameAndEmailForOneType() {
        List<User> dataList = 
repository.queryUsersByNameAndEmailForOneType("chendd""88");
        Assert.assertNotNull(dataList);
    }

    
@Test
    
public void testQueryByNameAndEmailForTwoType() {
        List<User> dataList = 
repository.queryUsersByNameAndEmailForTwoType("chendd" "88");
        Assert.assertNotNull(dataList);
    }

    
@Test
    
public void testQueryByNameAndEmailForThreeType() {
        String nameArray[] = {
"chendd" "cdd"};
        List<String> nameList = Lists.newArrayList(nameArray);
        List<User> dataList = 
repository.queryUsersByNameAndEmailForThreeType(nameList , nameArray , "88");
        System.
out.println(dataList);
        Assert.assertNotNull(dataList);
    }

}

总结

(1)本篇文章实现了使用@Query注解查询的基本应用,包含了参数等值查询、参数模糊查询、及参数集合 in 的查询方式,其中查询 in 的时候支持传递 String[] 与 List 类型;

(2)上述三种参数传递方式,个人喜欢使用第三种“:属性”的方式,更为直观且同一个参数可被重复使用;


上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:https://www.chendd.cn/information/viewInformation/experienceShare/366.a

最后更新:2020-04-29 23:07:45

访问次数:1835

评论次数:0

点赞个数:2,[相当给力:1,囧了一回:1]

 点赞


 发表评论

当前回复:作者

 评论列表


留言区