河北明迈特的网站在哪里做的/游戏代理平台哪个好
Spring Data JPA的查询方式
使用JPQL的方式查询
dao接口
使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件,这时就可以使用@Query注解,结合JPQL的语句方式完成查询
@Query 注解的使用非常简单,只需在方法上面标注该注解,同时提供一个JPQL查询语句即可
/*** 根据客户名称查询客户* 使用jpql的形式查询* jpql: from Customer where custName = ?* 配置jpql使用@Query注解* 属性value 配置jpql属性*/@Query(value = "from Customer where custName = ? ")public Customer findJpql(String custName);/*** 根据客户名称和客户id查询客户* jpql: from Customer where custName = ? and custId = ?* 多个占位符参数* 赋值的时候,默认的情况下,占位符的位置需要和方法中参数的位置保持一致* 可以指定占位符参数的位置* 可以使用 ?+索引的方式,指定占位的取值来源* 例:custName = ?2 表示使用参数中第二个的取值 custId = ?1 表示使用参数中第一个参数的取值* @Query("from Customer where custName = ?2 and custId = ?1")* public Customer findCustNameAndId(Long id,String name);*/@Query("from Customer where custName = ? and custId = ?")public Customer findCustNameAndId(String name,Long id);/*** 使用jpql完成更新操作* 需求:根据id更新客户的名称* 更新2号客户的名称 将名称改为 程序猿* sql:update cst_customer set cust_name="程序猿" where cust_id=2* jpql: update Customer set custName=? where custId= ?* @Query:代表的是进行查询* 更新不是查询 需要声明此方法是用来更新操作的* @Modifying* 代表当前执行的方法是一个更新操作*/@Query(value = "update Customer set custName= ? where custId= ?")@Modifyingpublic void updateCustomer(String custName,Long custId);
测试方法
查询
@Testpublic void testFindJpql(){Customer customer = customerDao.findJpql("星图");System.out.println(customer);}
修改
/*** 根据id修改客户姓名* springDataJpa中使用jpql完成更新/删除操作* 需要手动添加事务的支持 必须* 默认会执行结束之后回滚事务* @Rollback(value = false):设置不自动回滚 true 自动回滚* 修改需要开启事务的支持*/@Test@Transactional()@Rollback(value = false)public void testUpdateCustomer(){customerDao.updateCustomer("P图",2l);}
使用SQL语句查询
dao接口
/*** SQL语句查询方式* 特有的查询 需要在dao接口上配置方法* 在新添加的方法上,使用注解的形式配置SQL查询语句* 注解:@Query 两个参数* @Value:配置jpql 也可以配置SQL语句* nativeQuery是否使用本地查询:值 false(使用jpql查询 默认值就是false)|true(使用本地查询 SQL查询)* 需求 查询全部的客户* sql:select * from cst_customer* @Query配置SQL查询 value: SQL语句 nativeQuery: 查询方式*///@Query(value = "select * from cst_customer",nativeQuery = true)//根据用户名模糊查询@Query(value = "select * from cst_customer where cust_name like ?",nativeQuery = true)public List<Object[] > findSql(String name);
测试方法
/*** 使用SQL语句查询全部客户*/@Testpublic void testFindSql(){List<Object[]> list = customerDao.findSql("%猿%");for (Object[] obj:list){System.out.println(Arrays.toString(obj));}}
方法命名规则查询
顾名思义,方法命名规则查询就是根据方法的名字,就能创建查询。只需要按照Spring Data JPA提供的方法命名规则定义方法的名称,就可以完成查询工作。Spring Data JPA在程序执行的时候会根据方法名称进行解析,并自动生成查询语句进行查询
按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。
dao接口
/*** 方法名称命名规则:* 是对jpql的查询,更深入的一层封装。* 我们只需要按照SpringDataJpa提供的方法名称规则定义方法,不需要再去配置jpql语句完成查询*//*** 方法名的约定* findBy开头:代表查询* findBy+对象中属性名(首字母大写) CustName: 代表查询的条件 根据哪个属性进行查询* 默认情况 使用等于的方式进行查询* 特殊查询的查询方式* 例:findByCustName: 代表根据客户名称查询** 在springDataJpa的运行阶段* 会根据方法名称进行解析 把findBy翻译成 from xxx(实体类) 属性 where custName =** findBy+属性名称(根据属性名称进行完成匹配的查询 = )* findBy+属性名称+“查询方式(Like | isnull)”* findByCustNameLike 代表按照客户名称进行模糊匹配** 多条件查询* findBy+属性名+“查询方式” + 多条件查询的连接符(and,or等)+属性名+“查询方式”*//*** 根据名称查询* @param custName* @return*/public Customer findByCustName(String custName);/*** 根据名称模糊查询* @param custName* @return*/public List<Customer> findByCustNameLike(String custName);/*** 使用客户名称模糊匹配和客户所属行业精准匹配的查询* 多条件查询*/public Customer findByCustNameLikeAndCustIndustry(String custName,String custIndustry);
测试方法
/*** 测试方法命名规则的查询*/@Testpublic void testFindByCustName(){Customer customer = customerDao.findByCustName("程序猿");System.out.println(customer);}/*** 测试根据用户名模糊查询*/@Testpublic void testFindByCustNameLike(){List<Customer> list = customerDao.findByCustNameLike("P%");for (Customer customer:list) {System.out.println(customer);}}/*** 测试使用客户名称模糊匹配和客户所属行业精准匹配的查询*/@Testpublic void testFindByCustNameLikeAndCustIndustry(){Customer customers = customerDao.findByCustNameLikeAndCustIndustry("%猿", "多媒体");System.out.println(customers);}
}