zoukankan      html  css  js  c++  java
  • SpringData_02_JPQL查询、SQL查询和方法命名规则查询

    1.使用JPQL的方式查询

    JPQL查询:Hibernate提供的是HQL查询,而JPA提供的是JPQL查询语言

    使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件,这时就可以使用@Query注解,结合JPQL的语句方式完成查询

    @Query 注解的使用非常简单,只需在方法上面标注该注解,同时提供一个JPQL查询语句即可

     /**
         * 更具客户名称查询客户
         * 使用jqpl的形式查询
         */
        @Query(value = "from Customer where custName=? ")
        public Customer findJpql(String custName);

    此外,也可以通过使用 @Query 来执行一个更新操作,为此,我们需要在使用 @Query 的同时,用 @Modifying 来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询

    /**
         * 使用jpql完成更新操作
         * 案例 : 根据id更新,客户的名称
         * sql  :update cst_customer set cust_name = ? where cust_id = ?
         * jpql : update Customer set custName = ? where custId = ?
         *
         * @Query : 代表的是进行查询
         * * 声明此方法是用来进行更新操作
         * @Modifying * 当前执行的是一个更新操作
         */
        @Query(value = " update Customer set custName = ?2 where custId = ?1 ")
        @Modifying
        public void updateCustomer(long custId, String custName);

    2. 使用SQL语句查询

    Spring Data JPA同样也支持sql语句的查询,如下:

    查询全部

    /**
         * 使用sql的形式查询:
         *     查询全部的客户
         *  sql : select * from cst_customer;
         *  Query : 配置sql查询
         *      value : sql语句
         *      nativeQuery : 是否使用sql查询 默认false 查询方式
         *          true : sql查询
         *          false:jpql查询
         *
         */
        //@Query(value = " select * from cst_customer" ,nativeQuery = true)
        @Query(value="select * from cst_customer where cust_name like ?1",nativeQuery = true)
        public List<Customer [] > findSql(String name);

    3.方法命名规则查询

    顾名思义,方法命名规则查询就是根据方法的名字,就能创建查询。只需要按照Spring Data JPA提供的方法命名规则定义方法的名称,就可以完成查询工作。Spring Data JPA在程序执行的时候会根据方法名称进行解析,并自动生成查询语句进行查询

    按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。

    /**
         * 方法名的约定:
         *      findBy : 查询
         *            对象中的属性名(首字母大写) : 查询的条件
         *            CustName
         *            * 默认情况 : 使用 等于的方式查询
         *                  特殊的查询方式
         *
         *  findByCustName   --   根据客户名称查询
         *
         *  再springdataJpa的运行阶段
         *          会根据方法名称进行解析  findBy    from  xxx(实体类)
         *                                      属性名称      where  custName =
         *
         *      1.findBy  + 属性名称 (根据属性名称进行完成匹配的查询=)
         *      2.findBy  + 属性名称 + “查询方式(Like | isnull)”
         *          findByCustNameLike
         *      3.多条件查询
         *          findBy + 属性名 + “查询方式”   + “多条件的连接符(and|or)”  + 属性名 + “查询方式”
         */
        public Customer findByCustName(String custName);
    
        //方法名的约定:通过CustName模糊查询
        public List<Customer> findByCustNameLike(String custName);
    
        //使用客户名称模糊匹配和客户所属行业精准匹配的查询
        public Customer findByCustNameLikeAndCustIndustry(String custName,String custIndustry);

     

    方法命名规则

    Keyword

    Sample

    JPQL

    And

    findByLastnameAndFirstname

    … where x.lastname = ?1 and x.firstname = ?2

    Or

    findByLastnameOrFirstname

    … where x.lastname = ?1 or x.firstname = ?2

    Is,Equals

    findByFirstnameIs,

    findByFirstnameEquals

    … where x.firstname = ?1

    Between

    findByStartDateBetween

    … where x.startDate between ?1 and ?2

    LessThan

    findByAgeLessThan

    … where x.age < ?1

    LessThanEqual

    findByAgeLessThanEqual

    … where x.age ⇐ ?1

    GreaterThan

    findByAgeGreaterThan

    … where x.age > ?1

    GreaterThanEqual

    findByAgeGreaterThanEqual

    … where x.age >= ?1

    After

    findByStartDateAfter

    … where x.startDate > ?1

    Before

    findByStartDateBefore

    … where x.startDate < ?1

    IsNull

    findByAgeIsNull

    … where x.age is null

    IsNotNull,NotNull

    findByAge(Is)NotNull

    … where x.age not null

    Like

    findByFirstnameLike

    … where x.firstname like ?1

    NotLike

    findByFirstnameNotLike

    … where x.firstname not like ?1

    StartingWith

    findByFirstnameStartingWith

    … where x.firstname like ?1 (parameter bound with appended %)

    EndingWith

    findByFirstnameEndingWith

    … where x.firstname like ?1 (parameter bound with prepended %)

    Containing

    findByFirstnameContaining

    … where x.firstname like ?1 (parameter bound wrapped in %)

    OrderBy

    findByAgeOrderByLastnameDesc

    … where x.age = ?1 order by x.lastname desc

    Not

    findByLastnameNot

    … where x.lastname <> ?1

    In

    findByAgeIn(Collection ages)

    … where x.age in ?1

    NotIn

    findByAgeNotIn(Collection age)

    … where x.age not in ?1

    TRUE

    findByActiveTrue()

    … where x.active = true

    FALSE

    findByActiveFalse()

    … where x.active = false

    IgnoreCase

    findByFirstnameIgnoreCase

    … where UPPER(x.firstame) = UPPER(?1)

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    Lucene教程
    ElasticSearch安装
    MySQL事务
    Java泛型
    Python学习笔记(1)
    @keyframs实现图片gif效果
    glup简单应用---gulpfile.js
    巧用CSS3伪类选择器自定义checkbox和radio的样式
    get传中文参数乱码解决方法
    自定义样式 实现文件控件input[type='file']
  • 原文地址:https://www.cnblogs.com/asndxj/p/11808498.html
Copyright © 2011-2022 走看看