package cn.itcast.dao;
import cn.itcast.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 java.util.List;
/*
* 符合SpringDataJpa的dao層接口規(guī)范
* JpaRepository<操作的實體類泛型,實體類中主鍵屬性的類型>
* *封裝了基本增刪改查操作
* JpaSpecificationExecutor<操作的實體類類型>
* *封裝了復(fù)雜查詢(分頁)
* */
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
/*
* 案例复凳,根據(jù)客戶名稱查詢客戶
* 使用jpql的形式查詢
* jpql:from Customer where custName=?
* 配置jpql語句口柳,使用@Query注解
* */
@Query(value="from Customer where custName=?")
public Customer findJpql(String custName);
/*
* 案例功氨,根據(jù)客戶名稱和客戶id查詢用戶
* jpql:from Customer where custName=镜会? and custId=炫刷?
* 對于多個占位符參數(shù)
* 賦值的時候毕荐,默認(rèn)情況下翘地,占位符的位置需要和方法參數(shù)中的位置保持一致
*可以指定占位符參數(shù)的位置
* 添祸?索引的方式滚粟,指定此占位的取值來源
*
* */
@Query(value = "from Customer where custName=?1 and custId=?2")
public Customer findCustNameAndId(String name,Long id);
/*
* 使用jpql完成更新操作
* 案例: 根據(jù)id更新,客戶的名稱
* 更新7號客戶的名稱膝捞,將名稱改為“很nb”
* sql: update cst_customer set cust_name=? where cust_id=?
* jpql: update Customer set custName=? where custId=?
*
* @Query:代表進(jìn)行查詢
* *聲明此方法是用來更新操作
* @Modifying
* *當(dāng)前執(zhí)行的是一個更新的操作
* */
@Query(value = "update Customer set custName=?2 where custId=?1")
@Modifying
public void updateCustName(long custId,String custName);
/**
*使用sql的形式查詢
* 查詢?nèi)坑脩? * sql : select * from cst_customer
* @Query:配置sql查詢
* value:sql語句
* nativeQuery:查詢方式
* true:sql查詢
* false:jpql查詢
*/
// @Query(value = "select * from cst_customer",nativeQuery = true)
@Query(value = "select * from cst_customer where cust_name like ?1",nativeQuery = true)
public List<Object[]>findSql(String name);
/**
* 方法名的約定
* findBy:查詢
* 對象中的屬性名(首字母大寫):查詢的條件
* CustName
* *默認(rèn)情況 使用 等于的方式查詢
* 特殊的查詢方式
* findByCustName -- 根據(jù)客戶名稱查詢
* 在springdataJpa的運行階段
* 會根據(jù)方法名稱進(jìn)行解析 findBy from xxx(實體類)
* 屬性名 where custName=
*
* 1.findBy+屬性名稱(根據(jù)屬性名稱進(jìn)行完成匹配的查詢=)
* 2.findBy+屬性名稱+查詢方式(like|isnull)
* findByCustNameLike
* 3.多條件查詢
* findBy+屬性名+“查詢方式”+'多條件的連接符(and|or)'+屬性名+“查詢方式”
*/
public Customer findByCustName(String custName);
public List<Customer> findByCustNameLike(String custName);
//使用客戶名稱模糊匹配和客戶所屬行業(yè)精準(zhǔn)匹配的查詢
public Customer findByCustNameLikeAndCustIndustry(String name,String custIndustry);
}
package cn.itcast.test;
import cn.itcast.dao.CustomerDao;
import cn.itcast.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.ArrayList;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)//聲明spring提供的單元測試環(huán)境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class JpqlTest {
@Autowired
private CustomerDao customerDao;
@Test
public void testFindJPQL(){
Customer jpql = customerDao.findJpql("肇事");
System.out.println("輸出的是:"+jpql);
}
@Test
public void testFindCustNameAndId(){
Customer customer = customerDao.findCustNameAndId("SS1111", 2l);
System.out.println(customer);
}
/*
* 測試jpql的更新操作
* *springDataJpa中使用jpql完成坦刀,更新/刪除操作
* *需要手動添加事務(wù)支持
* *默認(rèn)會執(zhí)行結(jié)束之后,回滾事務(wù)
* */
@Test
@Transactional//添加事務(wù)的支持
@Rollback(value = false)//設(shè)置自動回滾 false|true
public void testUpdateCustomer(){
customerDao.updateCustName(7l,"很nb");
}
@Test
public void testFindSql(){
List<Object[]> sql = customerDao.findSql("%東%");
for (Object[] objects:sql){
System.out.println(Arrays.toString(objects));
}
}
/**
* 測試方法命名規(guī)則的查詢
*/
@Test
public void testName(){
Customer customer = customerDao.findByCustName("劉強東");
System.out.println(customer);
}
@Test
public void testFindByNameLike(){
List<Customer> list = customerDao.findByCustNameLike("%強%");
for (Customer customer : list){
System.out.println(customer);
}
}
@Test
public void testFindByCustNameLikeAndCustIndustry(){
Customer customer1 = customerDao.findByCustNameLikeAndCustIndustry("%強%", "教育");
System.out.println(customer1);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者