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

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

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

package com.ytkj.entity;

import javax.persistence.*;
import java.io.Serializable; /**
* @Entity
* 作用:指定当前类是实体类。
* @Table
* 作用:指定实体类和表之间的对应关系。
* 属性:
* name:指定数据库表的名称
* @Id
* 作用:指定当前字段是主键。
* @GeneratedValue
* 作用:指定主键的生成方式。。
* 属性:
* strategy :指定主键生成策略。
* @Column
* 作用:指定实体类属性和数据库表之间的对应关系
* 属性:
* name:指定数据库表的列名称。
* unique:是否唯一
* nullable:是否可以为空
* inserttable:是否可以插入
* updateable:是否可以更新
* columnDefinition: 定义建表时创建此列的DDL
* secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字搭建开发环境[重点]
*
* 客户实体类
* 配置映射关系
* 实体类和表映射
* 实体类属性和表字段映射
*/
@Entity
@Table(name = "cst_customer")
public class Customer implements Serializable {
/**
* 声明主键配置
*/
@Id
/**
* 配置主键的生成策略
*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
/**
* 指定实体类属性和数据库表之间的对应关系
*/
@Column(name ="cust_id")
private Long custId;//客户主键
@Column(name = "cust_name")
private String custName;//客户名称
@Column(name ="cust_source" )
private String custSource;//客户来源
@Column(name = "cust_industry")
private String custIndustry;//客户行业
@Column(name ="cust_level")
private String custLevel;//客户级别
@Column(name ="cust_address")
private String custAddress;//客户地址
@Column(name = "cust_phone")
private String custPhone;//客户电话 public Long getCustId() {
return custId;
} public void setCustId(Long custId) {
this.custId = custId;
} public String getCustName() {
return custName;
} public void setCustName(String custName) {
this.custName = custName;
} public String getCustSource() {
return custSource;
} public void setCustSource(String custSource) {
this.custSource = custSource;
} public String getCustIndustry() {
return custIndustry;
} public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
} public String getCustLevel() {
return custLevel;
} public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
} public String getCustAddress() {
return custAddress;
} public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
} public String getCustPhone() {
return custPhone;
} public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
} @Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custAddress='" + custAddress + '\'' +
", custPhone='" + custPhone + '\'' +
'}';
}
}

  


package com.ytkj.dao;

import com.ytkj.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository; /**
* JpaRepository<实体类类型,主键类型>:用来完成基本CRUD操作
* JpaSpecificationExecutor<实体类类型>:用于复杂查询(分页等查询操作)
*/
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
/**
* 根据客户名称查询
* 使用jpql查询:
* from Customer where custName=?
* 使用注解
*/ @Query(value = "from Customer where custName=?")
Customer findByName(String custName); /**
* jpql占位符
* 根据id和名称查询
* 多个占位符的位置默认情况下需要和方法中的参数位置保持一致,
* 也可以指定占位符参数的位置
* ? 索引
* @Query(value = "from Customer where custId=?2 and custName=?1")
* Customer findByNameAndId(String custName,Long id);
* @param id
* @param custName
* @return
*/
@Query(value = "from Customer where custId=? and custName=?")
Customer findByNameAndId(Long id,String custName); /**
* 根据id更新名称
*/
@Query(value = "update Customer set custName=? where custId =? ")
@Modifying//此注解表示更新操作
void updateById(String name,Long id);
}
import com.ytkj.dao.CustomerDao;
import com.ytkj.entity.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)//声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class SpringdatajpqlTest {
@Autowired
CustomerDao customerDao; /**
* 根据id查询
*/
@Test
public void findByName(){
Customer customer = customerDao.findByName("zhechaochao");
System.out.println(customer);
} /**
* 根据id和名称查询
*/
@Test
public void findByNameAndId(){
Customer customer = customerDao.findByNameAndId(1L,"zhechaochao1");
System.out.println(customer);
}
/**
* 根据id更新
*/
@Test
@Transactional//添加事务,使用jpql更新或者删除操作需要添加事务,否则或报错,默认会执行结束后,回滚事务
@Rollback(value = false)//设置是否自动回滚
public void updateById(){
customerDao.updateById("者超超",1L); } }

spring data jpa 使用JPQL的方式查询的更多相关文章

  1. SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

    软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...

  2. spring data jpa 使用方法命名规则查询

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

  3. 【hql】spring data jpa中 @Query使用hql查询 问题

    spring data jpa中 @Query使用hql查询 问题 使用hql查询, 1.from后面跟的是实体类 不是数据表名 2.字段应该用实体类中的字段 而不是数据表中的属性 实体如下 hql使 ...

  4. SpringData JPA使用JPQL的方式查询和使用SQL语句查询

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

  5. 使用Spring Data JPA的Specification构建数据库查询

    Spring Data JPA最为优秀的特性就是可以通过自定义方法名称生成查询来轻松创建查询SQL.Spring Data JPA提供了一个Repository编程模型,最简单的方式就是通过扩展Jpa ...

  6. Spring Data JPA 复杂/多条件组合查询

    1: 编写DAO类或接口  dao类/接口 需继承 public interface JpaSpecificationExecutor<T> 接口: 如果需要分页,还可继承 public ...

  7. Spring Data JPA 实现多表关联查询

    本文地址:https://liuyanzhao.com/6978.html 最近抽出时间来做博客,数据库操作使用的是 JPA,相对比 Mybatis 而言,JPA 单表操作非常方便,增删改查都已经写好 ...

  8. spring data jpa删除的使用方式

    public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(Str ...

  9. Spring data Jpa 分页从1开始,查询方法兼容 Mybatis,分页参数兼容Jqgrid

    废话少说 有参数可以设置 在org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties 中 /** * Whethe ...

随机推荐

  1. 使用UI Automation实现自动化测试--5-7

    使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...

  2. SQL语句优化方式--进阶篇

    MYSQL性能 最大数据量 抛开数据量和并发数,谈性能都是扯淡(扯的最疼的那种).Mysql没有限制单表最大记录数,它取决与操作系统对文件大小的限制 文件系统 单文件大小限制 FAT32(采用32位二 ...

  3. flink批处理中的source以及sink介绍

    一.flink在批处理中常见的source flink在批处理中常见的source主要有两大类: 1.基于本地集合的source(Collection-based-source) 2.基于文件的sou ...

  4. 杯子(glass)

    题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...

  5. 从零开始做一个Jmeter性能测试

    安装Jmeter 1.下载地址http://jmeter.apache.org/download_jmeter.cgi 2.解压下载文件,然后将bin目录添加到系统环境变量PATH里. 3.确保已安装 ...

  6. 第一节 初识RabbitMQ

    原文:第一节 初识RabbitMQ 版权声明:未经本人同意,不得转载该文章,谢谢 https://blog.csdn.net/phocus1/article/details/87280120 1.什么 ...

  7. 2019HDU多校训练第三场 Planting Trees 暴力 + 单调队列优化

    题意:有一个n * n的网格,每个网格中间有一颗树,你知道每棵树的高,你可以选择一个矩形区域把里面的树都围起来,但是矩形区域里面任意两棵树的高度差的绝对值不超过m,问这个矩形的最大面积是多少? 思路: ...

  8. Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力

    题意及思路:https://www.cnblogs.com/Als123/p/11061147.html 代码: #include <bits/stdc++.h> #define LL l ...

  9. Java网络编程:IP地址和端口号

    1)IP地址 用来标志网络中的一个通信实体的地址.通信实体可以是计算机,路由器等. 2)IP地址分类 IPV4:32位地址,以点分十进制表示,如192.168.0.1 IPV6:128位(16个字节) ...

  10. pycharm查找替换快捷键

    查找:CTRL + F 替换:CTRL + R 如果想删除,替换那一栏不填就可以了