SpringData JPA使用JPQL的方式查询和使用SQL语句查询
使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件,
这时就可以使用@Query注解,结合JPQL的语句方式完成查询
持久层接口:
/**
* 客户持久层接口
* JpaRepository<实体类类型,主键类型>:用来完成基本CRUD操作
* JpaSpecificationExecutor<实体类类型>:用于复杂查询(分页等查询操作)
*/
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> { /**
* 根据客户名称查询客户
* 使用jpql的形式查询
* jpql:from Customer where custName = ?
* 配置jpql语句,使用的@Query注解
*/
@Query("from Customer where custName = ?")
Customer findJpql(String custName); /**
* 根据客户名称和客户id查询客户
* jpql: from Customer where custName = ? and custId = ?
* 对于多个占位符参数
* 赋值的时候,默认的情况下,占位符的位置需要和方法参数中的位置保持一致
* 也可以指定占位符参数的位置
* ?索引的方式,指定此占位的取值来源
*/
@Query("from Customer where custName = ?2 and custId = ?1")
Customer findCustNameAndId(Long id, String name); /**
* 使用jpql完成更新操作
* 根据id更新,客户的名称
* sql :update cst_customer set cust_name = ? where cust_id = ?
* pql : update Customer set custName = ? where custId = ?
* @Query : 代表的是进行查询
* @Modifying : 声明此方法是用来进行更新操作
*/
@Query(value = "update Customer set custName = ? where custId = ?")
@Modifying
@Transactional // springdata jpa使用jpql执行插入,更新,删除需要手动提交事务
@Rollback(false) // 默认在执行之后,回滚事务,这里设置不回滚
void updateCustomer(String custName, long custId); /**
* 使用sql的形式查询:查询全部的客户
* sql:select * from cst_customer
* nativeQuery : true 使用本地sql的方式查询 false(默认) jpql查询
*/
@Query(value = "select * from cst_customer", nativeQuery = true)
List<Customer> findSql();
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JpqlTest {
@Autowired
private CustomerDao customerDao; @Test
public void testFindJpql() {
Customer c = customerDao.findJpql("小明");
System.out.println(c);
} @Test
public void testFindCustNameAndId() {
Customer c = customerDao.findCustNameAndId(6L, "小倩");
System.out.println(c);
} @Test
public void testUpdateCustomer() {
customerDao.updateCustomer("lily", 4L);
} @Test
public void testFindSql() {
List<Customer> customers = customerDao.findSql();
for (Customer customer : customers) {
System.out.println(customer);
}
}
}
SpringData JPA使用JPQL的方式查询和使用SQL语句查询的更多相关文章
- 如何查找MySQL中查询慢的SQL语句
如何查找MySQL中查询慢的SQL语句 更多 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow ...
- 使用sql语句查询日期在一定时间内的数据
使用sql语句查询日期在一周内的数据 select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天日期在一周年 ...
- 如何查找MySQL中查询慢的SQL语句(转载)
转载自https://www.cnblogs.com/qmfsun/p/4844472.html 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那 ...
- 使用Query进行HQL语句查询和SQL语句查询
HQL的语法比较简单,与普通SQL的区别之处是针对对象的不同,在查询语句中将sql中的表名替换成了sql中的持久化类名,因为hibernate机制是基于对象进行查询的. 不带参数的查询,语句是“fro ...
- 通过sql语句查询出来的结果字段没有到对应实体类时的处理方法
通过sql语句查询出来的结果字段没有到对应实体类时的处理方法,对于Person类获取用户第一个名字和年龄两个字段,常见的有两种方式: 1.在创建一个与查询结果字段对应的类,然后通过构造函数实现: Qu ...
- Mysql中 查询慢的 Sql语句的记录查找
Mysql中 查询慢的 Sql语句的记录查找 慢查询日志 slow_query_log,是用来记录查询比较慢的sql语句,通过查询日志来查找哪条sql语句比较慢,这样可以对比较慢的sql可以进行优化. ...
- sql语句查询
1. sql语句查询某位数字或者某几位数字开头的数据,字段类型为数字类: %’: 2. sql搜索以4开头和含有李字的数据: select * from wlzbpre_user where real ...
- phpcmsv9自定义sql语句查询模型实现
在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底 ...
- 怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表? --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...
随机推荐
- 三级联动的区域选择器 iOS组件
在iOS开发中,多级联动选择器非常常见,一般用于条件筛选,区域选择等. 实现了一个找房 APP 的筛选功能,效果如下: 代码如下:https://github.com/zhangtibin/Condi ...
- Could not find a version that satisfies the requirement win32api (from versions: ) No matching distribution found for win32api
pip install win32api pip install pywin32 都会提示错误,如下: Could not find a version that satisfies the requ ...
- 关于定时执行任务:Crontab的20个例子
关于定时执行任务:Crontab的20个例子 LeeLom 关注 2016.09.28 19:53* 字数 713 阅读 9186评论 6喜欢 15 简介 Linux crontab和Windows ...
- Keras的TimeDistributed层
Keras的TimeDistributed层主要用途是在时间维度上进行全连接. 比如Faster RCNN,1张图生成了16个ROI,需要对每一个ROI进行分类和回归,ROI的维度是7×7×512,长 ...
- jqgrid自适应宽度
https://blog.csdn.net/duzhanxiaosa/article/details/78922660
- Cosmetic Sprayer Structure Anatomy
What shape of spray is sprayed by the cosmetic spray pump head? Plastic Sprayers Manufacturer ...
- element-ui的el-table的表头与列不对齐
最好加到全局样式中: body .el-table th.gutter{ display: table-cell!important; }
- 爬虫模拟cookie自动登录(人人网自动登录)
什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...
- JAVA中final关键字的作用
一.final关键字的功能概述 final关键字可以用来修饰引用.方法和类. 1.用来修饰一个引用 如果引用为基本数据类型,则该引用为常量,该值无法修改: 如果引用为引用数据类型,比如对象.数组,则该 ...
- Electromagnetic
1. 电磁辐射 2. 电磁频谱 3. 可见光 4. 微波 5. 更多相关链接 1. 电磁辐射 https://en.wikipedia.org/wiki/Electromagnetic_radiati ...