用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. The 13th Chinese Northeast Collegiate Programming Contest(B C E F H J)

    B. Balanced Diet 思路:把每一块选C个产生的价值记录下来,然后从小到大枚举C. #include<bits/stdc++.h> using namespace std; ; ...

  2. python多线程的几种情形分析-三种情况

    情形一:默认情况 默认情况,只开启线程,那么,主线程结束,其他子线程可能还没结束. 只使用t=threading.Thead(target=fun),t.start(). import threadi ...

  3. SSOJ 317 Fast Ride

    317. Fast Ride Time limit per test: 0.25  second(s) Memory limit: 65536 kilobytes input: standard ou ...

  4. SpringMVC学习(5):数据绑定2 @PathVariable、@CookieValue、@RequestHeader、@ModelAttribute..

    在系列(4)中我们介绍了如何用@RequestParam来绑定数据,下面我们来看一下其它几个数据绑定注解的使用方法. 1.@PathVariable 用来绑定URL模板变量值,这个我们已经在系列(3) ...

  5. 一、spring的基本认识

    Spring的认识 Spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用 Spring是于2003 年兴起的一个轻量级的J ...

  6. NSQ消息队列

    前面的总结中提到过这个玩意,所以简单说说,win上面的测试验证 网上有比较合适的博文,我先推荐几篇 https://blog.csdn.net/a2247889821/article/details/ ...

  7. SQL数据库—<1>SQL语言

    关系数据库.SQL语言简单.学习软件介绍 SQL:Structured Query Language 结构化查询语言 数据库分为:层次型,网状型,关系型. 关系型数据库:是一个二维表的集合,可以用来存 ...

  8. java synchronized的四种用法

    一 修饰方法 Synchronized修饰一个方法很简单,就是在方法的前面加synchronized,synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起来 ...

  9. layui table 中固定列的行高和table行高不一致

    解决方法:只需在done回调函数执行以下方法 done: function(res, curr, count){ $(".layui-table-main tr").each(fu ...

  10. 2018-2-13-win10-uwp-绑定密码

    title author date CreateTime categories win10 uwp 绑定密码 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...