jpql介绍。JPQL全称Java Persistence Query Language。语法或关键字和sql语句类似,查询的是类中的属性
查询步骤:

  • 创建query查询对象
  • 对参数进行赋值
  • 查询,并得到返回结果

使用:

  • 特有的查询:需要在dao接口上配置的方法
  • 在新添加的方法上,使用注解的形式配置jpql查询语句
  • 注解:@Query

代码:

/**
 * 符合SpringDataJpa的dao层接口规范
 *      JpaRepository < 操作的实体类类型,实体类中主键属性的类型>
 *          * 封装了基本CRUD操作
 *      JpaSpecificationExecutor< 操作的实体类类型 >
 *          * 封装了复杂查询(分页)
 */
public interface CustomerDao extends JpaRepository<Customer,Long> ,JpaSpecificationExecutor<Customer> {
    /**
     * 案例:根据客户名称查询客户
     *  使用jpql的形式查询
     *jpql:from Customer where custName = ?
     */
    @Query(value = "from Customer where custName = ?1")
    public Customer findJpql(String custName);
    /**
     *案例:根据客户名称和客户id查询客户
     *      jpql:from Customer where custName = ? and custId = ?
     */
    @Query(value = "from Customer where custName = ?1 and   custId = ?2")
    public Customer findCustNameAndId(String name,Long id);
    /**
     * 使用jpql完成更新操作
     *  案例:根据ID更新,客户的名称
     *  更新3号客户的名称,改为Google
     *  SQL:update cst_customer set cus_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 id,String name);
    /**
     * 使用SQL的形式查询
     * 查询全部的客户
     * SQL:select * from cst_customer
     * query:配置SQL查询
     * value:SQL语句
     * nativequery:查询方式
     *  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<Object []> findSql(String name);
    /**
     *方法名的约定:
     *  findBy:查询
     *      对象中的属性名(首字母大写):查询的条件
     *      CustName
     *          *默认情况:使用等于的方式查询
     *              特殊的查询方式
     *
     *  findByCustName  ---  根据客户名称查询
     *
     *  再springDataJpa的运行阶段
     *          会根据方法名称进行解析 findBy from xxx(实体类)
     *                                          属性名称 where custName =
     *   findBy + 属性名称(根据属性名称进行完成匹配的查询=)
     *      findBy + 属性名称 + “查询方式(like | isNull)”
     *          findByCustNameLike
     *
     *    多条件查询
     *      findBy+属性名+“查询方式”+“多条件的连接符(and|or)”+属性名+“查询方式”
     */
    public Customer findByCustName(String custName);
    public List<Customer> findByCustNameLike(String custName);
    //使用客户名称模糊匹配和客户所属行业精准匹配的查询
    public Customer findByCustNameLikeAndCustIndustry(String name,String custIndustry);
}

测试

@RunWith(SpringJUnit4ClassRunner.class)//声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class JpqlTest {
    @Autowired
    private CustomerDao customerDao;


    @Test
    public void testFindJPQL(){
        Customer cus = customerDao.findJpql("阿里");
        System.out.println(cus);

    }

    @Test
    public void testFindCustNameAndId(){
        Customer cu = customerDao.findCustNameAndId("阿里", 1l);
        System.out.println(cu);
    }

    /**
     * 测试jpql的更新操作
     *  *springdata jpa中使用jpql完成更新或者删除操作,需要手动添加事务的支持
     *  *默认会执行结束之后,回滚事务
     *  @Rollback:设置是否自动回滚
     */
    @Test
    @Transactional
    @Rollback(value = false)
    public void testUpdateCustomer(){
        customerDao.updateCustomer(3l,"google");
    }

    /**
     * 测试SQL查询
     */
    @Test
    public void testFindSql(){
        List<Object[]> list = customerDao.findSql("goo%");
        for (Object[] obj :list){
            System.out.println(Arrays.toString(obj));
        }
    }


    /**
     * 测试方法命名规则的查询
     */
    @Test
    public void testNaming(){
        Customer custName = customerDao.findByCustName("google");
        System.out.println(custName);

    }
    /**
     * 测试方法命名规则的查询
     */
    @Test
    public void testfindByCustNameLike(){
        List<Customer> list = customerDao.findByCustNameLike("goo%");
        for (Customer customer :list){
            System.out.println(customer );
        }
    }


    /**
     * 测试方法命名规则的查询
     */
    @Test
    public void testfindByCustNameLikeAndCustIndustry(){
        Customer customer = customerDao.findByCustNameLikeAndCustIndustry("goo%", "go开发");
        System.out.println(customer);
    }
}