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

JPQL

/**
* 使用 jqpl通过用户名查询数据
* jqpl:from Customer where custName=?
*
*/
@Query(value = "from Customer where custName=?")
public Customer findJpql(String custName); /**
* jqpl:from Customer where custName=? and custId=?
* 多占位符操作
* custName=?1 取第一个参数
* custId=?2 取第二个参数
*/
@Query("from Customer where custName=?1 and custId=?2")
public Customer findCustNameAndCustId(String name, long id); /**
* jpql 完成更新
* jpql:update Customer set custName=? where custId=?;
*
* @modifying *当前是一个更新操作
*/
@Query("update Customer set custName=? where custId=?")
@Modifying
@Transactional
public void updateCustomer(String name, long id);

SQL

/**
* 使用sql形式查询
* 查询全部客户
* sql:select * from cst_customer
*
* nativeQuery :false jpql查询 默认
* true sql查询
*/
@Query(value ="select * from cst_customer",nativeQuery = true)
public List<Object[]> findSql(); /**
* sql模糊查询
* @return
*/
@Query(value ="SELECT * FROM cst_customer WHERE cust_name LIKE ?",nativeQuery = true)
public List<Object[]> likeSql(String name);

方法命名规则查询

 /**
* 约定:
* 1 *findBy:查询
* 对象中的属性名(首字母大写)。查询的条件
* findByCustName --- 就是更具客户名称精准查询
* 会进行方法名的解析
       sql:select * from cst_customer where cust_name =?;
*
* 2 *findBy + 属性名称
* *findBy + 属性名称 +查询方式(like|isnull)
* findByCustNameLike
* 3 *findBy + 属性名称 +查询方式(like|isnull)+多条件连接符(and|or)+属性名+查询方式
*/
public Customer findByCustName(String custName); //模糊查询 Like
public List<Customer> findByCustNameLike(String custName); //根据用户名模糊查询 和 所属行业精准查询
public List<Customer> findByCustNameLikeAndCustIndustry(String custName,String custIndustry);

最新文章

  1. ECMAScript5之Array
  2. 面试复习(C++)之堆排序
  3. 浅谈servlet版本
  4. Windows下常用软件工具的命令
  5. java中的if-Switch选择结构
  6. CentOS VPS创建pptpd VPN服务
  7. python 多行字符串
  8. 使用Github建立个人博客
  9. [数据预处理]-中心化 缩放 KNN(一)
  10. flutter-开发总结
  11. 完整的Django入门指南学习笔记5
  12. WebForm - cookie赋值乱码问题
  13. angular2+ 中封装调用递归tree
  14. io.Writer
  15. Log4php使用指南
  16. artTemplate 动态加载模版
  17. python基础一 day17 作业
  18. SQLSERVER金额转换成英文大写的函数
  19. _bzoj2243 [SDOI2011]染色【树链剖分】
  20. js一般用法

热门文章

  1. Flink on YARN(下):常见问题与排查思路
  2. 0919CSP-S模拟测试赛后总结
  3. SQLite wrapper
  4. VS中检测数据库链接
  5. 阿里云CentOs7上安装Tomcat
  6. 5.1_Spring Boot2.x安装Docker
  7. HYNB Round 15: PKU Campus 2019
  8. Newtonsoft.Json高级篇:TypeNameHandling设置
  9. Linux 实用指令(8)--网络配置
  10. Redis单机和集群配置(版本在5.0后)