<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 一.`JpaSpecificationExecutor` 方法列表 ~~~ 第一 Specifications動態查詢 JpaSpecificationExecutor 方法列表 //查詢單個對象 Optional<T> findOne(@Nullable Specification<T> var1); //查詢列表 List<T> findAll(@Nullable Specification<T> var1); //查詢全部,分頁 //Pageable 分頁參數 //返回值: 分頁pageBean(page:是springdatajpa提供) Page<T> findAll(@Nullable Specification<T> var1, Pageable var2); //查詢列表 //sort : 排序參數 List<T> findAll(@Nullable Specification<T> var1, Sort var2); //統計查詢 long count(@Nullable Specification<T> var1); ~~~ ***** ### 二.dao層 ~~~ package net.youworker.repository; import net.youworker.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; /** * 符合 SpringDataJpa的dao層接口規范 * JpaRepository<操作的實體類類型,實體類中主鍵屬性的類型> * 封裝了基本的CRUD操作 * JpaSpecificationExecutor<操作的實體類類型> * 封裝了復雜查詢(如分頁等) * * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-07 10:47 */ public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> { } ~~~ ***** ### 三.測試 ~~~ package net.youworker.domain; import net.youworker.repository.CustomerDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.test.context.junit4.SpringRunner; import javax.persistence.criteria.*; import java.util.ArrayList; import java.util.List; import java.util.Optional; @RunWith(SpringRunner.class) @SpringBootTest public class SpecTest { @Autowired private CustomerDao customerDao; /** * 根據條件,查詢單個對象 */ @Test public void testSpec() { //匿名內部類 /** * 自定義查詢條件: * 1.實現Specification接口(提供泛型:查詢的對象類型) * 2.實現 toPredicate方法(構造查詢條件) * 3.需要借助方法參數中的兩個參數( * root: 獲取需要查詢的對象屬性 * CriteriaBuilder: 構造查詢條件,內部封裝了很多的查詢條件(模糊匹配,精準匹配) * ) * 例子: 根據客戶名稱查詢 * 查詢條件: * 1.查詢方式 * cb對象 * 2.比較的屬性名稱 * root對象 * */ Specification<Customer> specification = new Specification<Customer>() { @Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { //1.獲取比較的屬性 Path<Object> custName = root.get("custName"); //2.構造查詢條件 : select * from cst_customer where cust_name="大腳" /** * 第一個參數:需要比較的屬性(path對象) * 第二個參數:當前需要比較的取值 */ Predicate predicate = criteriaBuilder.equal(custName, "大腳"); return predicate; } }; // Optional<Customer> customer = customerDao.findOne(specification); // System.out.println(customer.get()); List<Customer> customerList = customerDao.findAll(specification); for (Customer customer : customerList) { System.out.println(customer.getCustName()); } } /** * 多條件查詢 * 例子: 根據客戶名和客戶所屬行業查詢 */ @Test public void testSpecMoreConditon() { /** * root:獲取屬性 * 客戶名 * 所屬行業 * cb:構造查詢 * 1.構造客戶的精準匹配查詢 * 2.構造所屬行業的精準匹配查詢 * 3.將以上兩個查詢聯系起來 */ Specification<Customer> specification = new Specification<Customer>() { @Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { //1.獲取比較的屬性 Path<Object> custName = root.get("custName"); Path<Object> custIndustry = root.get("custIndustry"); //2.構造查詢條件 : //1.構造客戶名的精準匹配查詢 Predicate predicate1 = criteriaBuilder.equal(custName, "大腳"); //2.構造所屬行業的精準匹配查詢 Predicate predicate2 = criteriaBuilder.equal(custIndustry, "本科"); //3.將多個查詢條件組合到一起:組合(兩個條件都滿足:與關系;滿足其中之一條件即可:或關系) Predicate predicate = criteriaBuilder.and(predicate1, predicate2); return predicate; } }; Optional<Customer> customer = customerDao.findOne(specification); System.out.println(customer.get()); } /** * 模糊查詢 * 例子: 根據客戶名查詢 * equal: 直接得到path對象(屬性),然后進行比較即可 * gt,lt,ge,le,like:得到path對象,根據path指定比較的參數類型,再去進行比較 * 指定參數類型:path.as(類型的字節碼對象) */ @Test public void testSpecLike() { /** * root:獲取屬性 * 客戶名 * 所屬行業 * cb:構造查詢 */ Specification<Customer> specification = new Specification<Customer>() { @Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { //1.獲取比較的屬性 Path<Object> custName = root.get("custName"); //查詢方式:模糊匹配 Predicate predicate = criteriaBuilder.like(custName.as(String.class), "%大腳%"); return predicate; } }; // List<Customer> customerList = customerDao.findAll(specification); //添加排序 /** * 創建排序對象,需要調用構造方法實例化sort對象 * 第一個參數:排序的順序(倒序,正序) * * */ //單個排序 // Sort.Order order = new Sort.Order(Sort.Direction.DESC, "custId"); //多個排序 List<Sort.Order> orderList = new ArrayList<>(); Sort.Order order1 = new Sort.Order(Sort.Direction.DESC, "custId"); Sort.Order order2 = new Sort.Order(Sort.Direction.DESC, "custLevel"); orderList.add(order1); orderList.add(order2); List<Customer> customerList = customerDao.findAll(specification, Sort.by(orderList)); for (Customer customer : customerList) { System.out.println(customer.getCustId()); System.out.println(customer.getCustName()); } } /** * 分頁查詢 * Specification: 查詢條件 * Pageable:分頁參數 * 分頁參數:查詢的頁碼,每頁查詢的條數 * findAll(Specification,Pageable):帶有條件的分頁 * findAll(Pageable):沒有條件的分頁 * <p> * 返回: Page(springDataJpa為我們封裝好的pageBean對象,數據列表,總條數) */ @Test public void testPage() { Specification<Customer> specification = new Specification<Customer>() { @Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { //1.獲取比較的屬性 Path<Object> custName = root.get("custName"); //2.構造查詢條件 : select * from cst_customer where cust_name="大腳" /** * 第一個參數:需要比較的屬性(path對象) * 第二個參數:當前需要比較的取值 */ Predicate predicate = criteriaBuilder.equal(custName, "大腳"); return predicate; } }; //排序 List<Sort.Order> orderList = new ArrayList<>(); Sort.Order order1 = new Sort.Order(Sort.Direction.DESC, "custId"); orderList.add(order1); //分頁+排序 Pageable pageRequest = PageRequest.of(0, 1, Sort.by(orderList)); Page<Customer> customerPage = customerDao.findAll(specification, pageRequest); System.out.println("數據集合"); System.out.println(customerPage.getContent());//得到數據集合列表 System.out.println("總條數"); System.out.println(customerPage.getTotalElements());//得到總條數 System.out.println("總頁數"); System.out.println(customerPage.getTotalPages());//得到總頁面 System.out.println("每頁顯示數"); System.out.println(customerPage.getSize());//得到每頁顯示的數 System.out.println("-------------------"); for (Customer customer : customerPage.getContent()) { System.out.println(customer.getCustId()); System.out.println(customer.getCustName()); } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看