spring data jpa查詢

學無止路發表於2020-10-20

spring data jpa查詢

1 spring Data JPA查詢呼叫介面方法查詢(count,exists)
編寫測試統計查詢測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 測試CRUD的操作
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class CustomerDaoTest {
    // 宣告CustomerDao業務物件
    @Autowired   // 自動注入
    private CustomerDao customerDao;
    /**
     * 測試統計查詢:查詢客戶的總數量
     *      count:統計總條數
     */
    @Test
    public void testCount() {
        // 查詢全部的客戶數量
        long count = customerDao.count();
        System.out.println(count);
    }
}

執行結果如圖所示:在這裡插入圖片描述
編寫測試客戶是否存在的程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 測試CRUD的操作
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class CustomerDaoTest {
    // 宣告CustomerDao業務物件
    @Autowired   // 自動注入
    private CustomerDao customerDao;
    /**
     * 測試:判斷id為5的客戶是否存在
     *      1. 可以查詢以下id為5的客戶
     *          如果值為空,代表不存在,如果不為空,代表存在
     *      2. 判斷資料庫中id為5的客戶的數量
     *          如果數量為0,代表不存在,如果大於0,代表存在
     */
    @Test
    public void  testExists() {
        boolean exists = customerDao.exists(5l);
        System.out.println("id為5的客戶 是否存在:"+exists);
    }
}

執行結果如圖所示:在這裡插入圖片描述
2 spring Data JPA查詢呼叫介面方法查詢(findOne和getOne的區別)
編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import com.txw.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
 * 測試CRUD的操作
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class CustomerDaoTest {
    // 宣告CustomerDao業務物件
    @Autowired   // 自動注入
    private CustomerDao customerDao;
    /**
     * 根據id從資料庫查詢
     *      @Transactional : 保證getOne正常執行
     *  findOne:
     *      em.find()           :立即載入
     *  getOne:
     *      em.getReference     :延遲載入
     *      * 返回的是一個客戶的動態代理物件
     *      * 什麼時候用,什麼時候查詢
     */
    @Test
    @Transactional
    public void  testGetOne() {
        Customer customer = customerDao.getOne(5l);
        System.out.println(customer);
    }
}

執行結果如圖所示:在這裡插入圖片描述
3 JPQL查詢引入
在繼承JpaRepository,和JpaRepository介面後,我們就可以使用介面中定義的方法進行查詢。
繼承JpaRepository後的方法列表在這裡插入圖片描述
繼承JpaSpecificationExecutor的方法列表在這裡插入圖片描述
jpql的查詢方式
jpql:jpa query language (jpq查詢語言)
特點:語法或關鍵字和sql語句類似。
查詢的是類和類中的屬性。
需要將JPQL語句配置到介面方法上
1.特有的查詢:需要在dao介面上配置方法
2.在新新增的方法上,使用註解的形式配置jpql查詢語句
3.註解 : @Query
使用Spring Data JPA提供的查詢方法已經可以解決大部分的應用場景,但是對於某些業務來說,我們還需要靈活的構造查詢條件,這時就可以使用@Query註解,結合JPQL的語句方式完成查詢。
@Query 註解的使用非常簡單,只需在方法上面標註該註解,同時提供一個JPQL查詢語句即可。
此外,也可以通過使用 @Query 來執行一個更新操作,為此,我們需要在使用 @Query 的同時,用 @Modifying 來將該操作標識為修改查詢,這樣框架最終會生成一個更新的操作,而非查詢。
4 使用jpql完成基本查詢
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 案例:根據客戶名稱查詢客戶
     *      使用jpql的形式查詢
     *  jpql:from Customer where custName = ?
     *  配置jpql語句,使用的@Query註解
     */
    @Query(value="from Customer where custName = ?")
    public Customer findJpql(String custName);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import com.txw.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 根據客戶名稱查詢
     */
    @Test
    public void  testFindJPQL() {
        Customer customer = customerDao.findJpql("學無止路");
        System.out.println(customer);
    }
}

執行結果如圖所示:在這裡插入圖片描述
5 多佔位符的賦值
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 案例:根據客戶名稱和客戶id查詢客戶
     *      jpql: from Customer where custName = ? and custId = ?
     *  對於多個佔位符引數
     *      賦值的時候,預設的情況下,佔位符的位置需要和方法引數中的位置保持一致
     *  可以指定佔位符引數的位置
     *      ? 索引的方式,指定此佔位的取值來源
     */
    @Query(value = "from Customer where custName = ?2 and custId = ?1")
    public Customer findCustNameAndId(Long id,String name);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import com.txw.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    @Test
    public void testFindCustNameAndId() {
        // Customer customer = customerDao.findCustNameAndId(5l, "Adair");
        Customer customer =  customerDao.findCustNameAndId(5l,"Adair");
        System.out.println(customer);
    }
}

執行結果如圖所示:在這裡插入圖片描述
6 使用jpql完成更新操作
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.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;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 使用jpql完成更新操作
     *      案例 : 根據id更新,客戶的名稱
     *          更新7號客戶的名稱,將名稱改為“學無止路”
     *  sql  :update cst_customer set cust_name = ? where cust_id = ?
     *  jpql : update Customer set custName = ? where custId = ?
     *  @Query : 代表的是進行查詢
     *      * 宣告此方法是用來進行更新操作
     *  @Modifying
     *      * 當前執行的是一個更新操作
     */
    @Query(value = " update Customer set custName = ?2 where custId = ?1 ")
    @Modifying
    public void updateCustomer(long custId,String custName);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
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;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 測試jpql的更新操作
     *  * springDataJpa中使用jpql完成 更新/刪除操作
     *         * 需要手動新增事務的支援
     *         * 預設會執行結束之後,回滾事務
     *   @Rollback : 設定是否自動回滾
     *          false | true
     */
    @Test
    @Transactional //新增事務的支援
    @Rollback(value = false)
    public void testUpdateCustomer() {
        customerDao.updateCustomer(7l,"學無止路");
    }
}

執行之前資料庫的資料如圖所示:在這裡插入圖片描述
執行結果如圖所示:在這裡插入圖片描述
執行之後資料庫的資料如圖所示:在這裡插入圖片描述
7 查詢全部
sql語句的查詢
1.特有的查詢:需要在dao介面上配置方法
2.在新新增的方法上,使用註解的形式配置sql查詢語句
3.註解 :@Query
value :jpql語句 | sql語句
nativeQuery :false(使用jpql查詢) | true(使用本地查詢:sql查詢)
是否使用本地查詢
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 使用sql的形式查詢:
     *     查詢全部的客戶
     *  sql : select * from cst_customer;
     *  Query : 配置sql查詢
     *      value : sql語句
     *      nativeQuery : 查詢方式
     *          true : sql查詢
     *          false:jpql查詢
     */
    //@Query(value = " select * from cst_customer" ,nativeQuery = true)
    public List<Object [] > findSql();
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.List;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 測試sql查詢
     */
    @Test
    public void testFindSql() {
        List<Object[]> list = customerDao.findSql();
        for(Object [] obj : list) {
            System.out.println(Arrays.toString(obj));
        }
    }
}

執行結果如圖所示:在這裡插入圖片描述
8 條件全部
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 使用sql的形式查詢:
     *     查詢全部的客戶
     *  sql : select * from cst_customer;
     *  Query : 配置sql查詢
     *      value : sql語句
     *      nativeQuery : 查詢方式
     *          true : sql查詢
     *          false:jpql查詢
     */
    @Query(value="select * from cst_customer where cust_name like ?1",nativeQuery = true)
    public List<Object [] > findSql(String name);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.List;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 測試sql模糊配置查詢
     */
    @Test
    public void testFindSql() {
        List<Object[]> list = customerDao.findSql("Adair%");
        for(Object [] obj : list) {
            System.out.println(Arrays.toString(obj));
        }
    }
}

執行結果如圖所示:在這裡插入圖片描述
9 方法命名規則查詢:基本查詢
顧名思義,方法命名規則查詢就是根據方法的名字,就能建立查詢。只需要按照Spring Data JPA提供的方法命名規則定義方法的名稱,就可以完成查詢工作。Spring Data JPA在程式執行的時候會根據方法名稱進行解析,並自動生成查詢語句進行查詢
按照Spring Data JPA 定義的規則,查詢方法以findBy開頭,涉及條件查詢時,條件的屬性用條件關鍵字連線,要注意的是:條件屬性首字母需大寫。框架在進行方法名解析時,會先把方法名多餘的字首擷取掉,然後對剩下部分進行解析。

//方法命名方式查詢(根據客戶名稱查詢客戶)
public Customer findByCustName(String custName);

具體的關鍵字,使用方法和生產成SQL如下表所示:
在這裡插入圖片描述
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 方法名的約定:
     *      findBy : 查詢
     *            物件中的屬性名(首字母大寫) : 查詢的條件
     *            CustName
     *            * 預設情況 : 使用 等於的方式查詢
     *                  特殊的查詢方式
     *  findByCustName   --   根據客戶名稱查詢
     *  再springdataJpa的執行階段
     *          會根據方法名稱進行解析  findBy    from  xxx(實體類)
     *                                      屬性名稱      where  custName =
     *      1.findBy  + 屬性名稱 (根據屬性名稱進行完成匹配的查詢=)
     *      2.findBy  + 屬性名稱 + “查詢方式(Like | isnull)”
     *          findByCustNameLike
     *      3.多條件查詢
     *          findBy + 屬性名 + “查詢方式”   + “多條件的連線符(and|or)”  + 屬性名 + “查詢方式”
     */
    public Customer findByCustName(String custName);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import com.txw.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 測試方法命名規則的查詢
     */
    @Test
    public void testNaming() {
        Customer customer = customerDao.findByCustName("Adair");
        System.out.println(customer);
    }
}

執行結果如圖所示:在這裡插入圖片描述
注意事項:custName在資料庫中資料相同就會報錯誤。
10 方法命名規則查詢:模糊匹配
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 根據客戶名進行模糊查詢
     * @param custName
     * @return
     */
    public List<Customer> findByCustNameLike(String custName);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import com.txw.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 測試方法命名規則的查詢
     */
    @Test
    public void testNaming() {
        List<Customer> list = customerDao.findByCustNameLike("學無止路");
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }
}

執行結果如圖所示:在這裡插入圖片描述
11 方法命名規則查詢:多條件查詢
編寫CustomerDao的案例的程式碼如下:

package com.txw.dao;

import com.txw.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
 *  符合SpringDataJpa的dao層介面規範
 *        JpaRepository<操作的實體類型別,實體類中主鍵屬性的型別>
 *             封裝了基本CRUD操作
 *        JpaSpecificationExecutor<操作的實體類型別>
 *            封裝了複雜查詢(分頁)
 * @author: Adair
 */
@SuppressWarnings("all")         // 註解警告資訊
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    /**
     * 使用客戶名稱模糊匹配和客戶所屬行業精準匹配的查詢
     * @param custName
     * @param custIndustry
     * @return
     */
    public Customer findByCustNameLikeAndCustIndustry(String custName,String custIndustry);
}

編寫測試程式碼如下:

package com.txw.test;

import com.txw.dao.CustomerDao;
import com.txw.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 使用jpql測試
 * @author: Adair
 */
@RunWith(SpringJUnit4ClassRunner.class) //宣告spring提供的單元測試環境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置資訊
@SuppressWarnings("all")         // 註解警告資訊
public class JpqlTest {
    // 宣告CustomerDao業務物件
    @Autowired
    private CustomerDao customerDao;
    /**
     * 測試方法命名規則的查詢
     */
    @Test
    public void testFindByCustNameLikeAndCustIndustry() {
        Customer customer = customerDao.findByCustNameLikeAndCustIndustry("學無止路%", "筆記");
        System.out.println(customer);
    }
}

執行結果如圖所示:在這裡插入圖片描述

相關文章