用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); } }