spring data jpa 使用JPQL的方式查询
用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的方式查询的更多相关文章
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...
- spring data jpa 使用方法命名规则查询
按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写.框架在进行方法名解析时,会先把方法名多余的前缀 ...
- 【hql】spring data jpa中 @Query使用hql查询 问题
spring data jpa中 @Query使用hql查询 问题 使用hql查询, 1.from后面跟的是实体类 不是数据表名 2.字段应该用实体类中的字段 而不是数据表中的属性 实体如下 hql使 ...
- SpringData JPA使用JPQL的方式查询和使用SQL语句查询
使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件, 这时就可以使用@Query注解,结合JPQL的语句方式完成查询 持久 ...
- 使用Spring Data JPA的Specification构建数据库查询
Spring Data JPA最为优秀的特性就是可以通过自定义方法名称生成查询来轻松创建查询SQL.Spring Data JPA提供了一个Repository编程模型,最简单的方式就是通过扩展Jpa ...
- Spring Data JPA 复杂/多条件组合查询
1: 编写DAO类或接口 dao类/接口 需继承 public interface JpaSpecificationExecutor<T> 接口: 如果需要分页,还可继承 public ...
- Spring Data JPA 实现多表关联查询
本文地址:https://liuyanzhao.com/6978.html 最近抽出时间来做博客,数据库操作使用的是 JPA,相对比 Mybatis 而言,JPA 单表操作非常方便,增删改查都已经写好 ...
- spring data jpa删除的使用方式
public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(Str ...
- Spring data Jpa 分页从1开始,查询方法兼容 Mybatis,分页参数兼容Jqgrid
废话少说 有参数可以设置 在org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties 中 /** * Whethe ...
随机推荐
- HDU 5183 Negative and Positive (NP) (手写哈希)
题目链接:HDU 5183 Problem Description When given an array \((a_0,a_1,a_2,⋯a_{n−1})\) and an integer \(K\ ...
- Appium移动端自动化:Api接口详解
滑动操作与拖拽操作 # 滚动处理 # elementObj1 目标滚动元素,elementObj2 起始滚动元素 # 底层通过action操作,与web ui相反,origin_el为目标元素,des ...
- python3 zip 与tf.data.Data.zip的用法
###python自带的zip函数 与 tf.data.Dataset.zip函数 功能用法相似 ''' zip([iterator1,iterator2,]) 将可迭代对象中对应的元素打包成一个元祖 ...
- 学习contiki需要知道的一些概念和注意事项
概念: 1.宏:所谓宏,就是一些命令组织在一起,作为一个单独命令完成一个特定任务.Microsoft Word中对宏定义为:“宏就是能组织到一起作为一独立的命令使用的一系列word命令,它能使日常工作 ...
- JS原型与原型链终极详解 (转载)
这篇文章需要认认真真仔仔细细的看才能看懂 一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函 ...
- Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (2)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.s ...
- docker-container 操作
1.把container放后台运行 [root@localhost dockerfile3]# docker run bigni/python_flask #默认运行image,container里运 ...
- LVS _keepalived 配置
#!/bin/bash HOSTNAME=$(HOSTNAME) ETHNAME=ens34 ID=-]{,}\.[-]{,}\.[-]{,}\.[-]{,}" | awk -F . 'NR ...
- Web前端基础学习-3
bfc(block formatting context) 块级格式化上下文 生成bfc的方式: 1.根元素: 2.float属性不为none(脱离文档流): 3.position为absolute或 ...
- XMPP即时通讯协议使用(十一)——Openfire表结构汇总
行号 字段名称 字段描述 字段类型 长度 主键 说明 允许为空 用户组数据表(ofGroup) 1 groupName 组名 varchar2 50 ★ NOT NULL 2 descriptio ...