zoukankan      html  css  js  c++  java
  • @Query 注解实现查询(二十四)

    为了节约时间使得各位看官看起来更加简单舒适,这一节把测试方法和测试代码放在一起。

    测试方法:

    // ------------------------------------ 使用 @Query 注解
    // 没有参数的查询
    @Query("select p from Person p where p.id = (select max(p2.id) from Person p2)")
    Person getMaxIdPerson();
    
    /**
     * 参数名称和参数顺序耦合
     * @param lastName
     * @param email
     * @return
     */
    @Query("select p from Person p where lastName=?1 and email=?2")
    Person readPersonByLastNameAndEmail(String lastName,String email);
    
    @Query("select p from Person p where email=:email and  lastName=:name")
    Person readPersonByLastNameAndEmailThroughName(@Param("name") String lastName,@Param("email") String email);
    
    // 使用 like
    @Query("select p from Person p where lastName like ?1")
    Person readPersonByLike(String likeName);
    
    // @Query 注解支持使用百分号
    @Query("select p from Person p where lastName like %?1%")
    Person readPersonByLike2(String likeName);
    
    // @Query 注解支持使用百分号
    @Query("select p from Person p where lastName like %:lastName%")
    Person readPersonByLike3(@Param("lastName")String name);
    
    // 使用原生的 SQL
    @Query(value="select * from jpa_person p1 where p1.last_name like %:lastName%",nativeQuery=true)
    Person getPersonUsingOriginSQL(@Param("lastName")String lastName);

    测试代码:

    // 以下测试 @Query 注解
    @Test
    public void testQueryAnnotationWithoutParam(){
        Person person = personRepository.getMaxIdPerson();
        System.out.println(person);
    }
    
    @Test
    public void testQueryAnnotationWithParam(){
        Person person = personRepository.readPersonByLastNameAndEmail("liwei","liwei@sina.com");
        System.out.println(person);
    }
    
    @Test
    public void testQueryAnnotationWithParamThroughName(){
        Person person = personRepository.readPersonByLastNameAndEmailThroughName("zhouguang","zhouguang@163.com");
        System.out.println(person);
    }
    
    @Test
    public void testQueryAnnotationWithParamThroughLike(){
        Person person = personRepository.readPersonByLike("%zhou%");
        System.out.println(person);
    }
    
    @Test
    public void testQueryAnnotationWithParamThroughLike2(){
        Person person = personRepository.readPersonByLike2("hu");
        System.out.println(person);
    }
    
    @Test
    public void testQueryAnnotationWithParamThroughLike3(){
        Person person = personRepository.readPersonByLike3("wei");
        System.out.println(person);
    }
    
    @Test
    public void testQueryAnnotationWithParamThroughLike4(){
        Person person = personRepository.getPersonUsingOriginSQL("wei");
        System.out.println(person);
    }

    注意到:如果我们使用原生的 SQL,控制台打印的语句也会是原生 SQL 的,例如上面我们最后一个测试方法控制台打印:

    这里写图片描述

  • 相关阅读:
    Java操作excel,读取及导出
    vue 在package.json配置对外暴露访问地址(手机端访问本地项目地址)
    element UI upload组件上传附件格式限制
    linux之vim/vi快速复制多行内容的快捷键
    使用vant的Toast组件时提示not defined
    如何使用js判断当前页面是pc还是移动端打开的
    JavaScript 保留两位小数函数
    Linux其他命令
    linux学习ls的三个选项 lha的作用和隐藏文件的知识
    vue+ element-ui el-table组件自定义合计(summary-method)坑
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/8177288.html
Copyright © 2011-2022 走看看