天天看點

項目day01-SpringDataJpa內建一.什麼是SpringDataJpa二.項目導包三 內建SpringDataJpa3.2 內建SpringDataJpa四.完成的CRUD功能五.JpaSpecificationExecutor六.jpa-spec七.Query抽取

一.什麼是SpringDataJpa

  • 它是Spring的一個子架構
  • 內建Jpa,讓咱們操作資料庫變得更加的簡單

二.項目導包

  • 能夠看懂這裡導入了哪些包
  • 也可以了解這些包是做什麼的
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.itsource</groupId>
  <artifactId>aisell</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>aisell Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>4.2.5.RELEASE</org.springframework.version>
    <org.hibernate.version>4.3.8.Final</org.hibernate.version>
    <spring-data-jpa.version>1.9.0.RELEASE</spring-data-jpa.version>
    <com.fasterxml.jackson.version>2.5.0</com.fasterxml.jackson.version>
    <org.slf4j.version>1.6.1</org.slf4j.version>
  </properties>
  <dependencies>
    <!-- Spring的支援包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <!-- 上下文支援包(幫我們內建:模闆,郵件,任務排程...) -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- 引入web前端的支援 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <!-- SpringMCV上傳需要用到io包-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!-- 檔案上傳用到的包 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.2</version>
    </dependency>
    <!-- SpringMVC的json支援包 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <!-- hibernate的支援包 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${org.hibernate.version}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${org.hibernate.version}</version>
    </dependency>
    <!-- SpringDataJpa的支援包 -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>${spring-data-jpa.version}</version>
    </dependency>
    <!-- SpringData的擴展包 -->
    <dependency>
      <groupId>com.github.wenhao</groupId>
      <artifactId>jpa-spec</artifactId>
      <version>3.1.1</version>
      <!-- 把所有的依賴都去掉 -->
      <exclusions>
        <exclusion>
          <groupId>*</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <!--lang3:工具包 java.lang.-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.5</version>
    </dependency>
    <!-- 測試包 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <!-- 這個scope 隻能作用在編譯和測試時,同時沒有傳遞性。表示在運作的時候不添加此jar檔案 -->
      <scope>provided</scope>
    </dependency>
    <!-- 日志檔案 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${org.slf4j.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
    </dependency>
    <!-- 代碼生成器模版技術  -->
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.6</version>
    </dependency>
    <!-- shiro(權限架構)的支援包 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-all</artifactId>
      <version>1.4.0</version>
      <type>pom</type>
    </dependency>
    <!-- shiro與Spring的內建包 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.4.0</version>
    </dependency>
    <!-- poi(操作辦公軟體)支援的jar包 -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.11</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.11</version>
    </dependency>
    <!-- 圖檔壓縮功能 -->
    <!-- 縮略圖 -->
    <dependency>
      <groupId>net.coobird</groupId>
      <artifactId>thumbnailator</artifactId>
      <version>0.4.6</version>
    </dependency>
    <!-- 定時排程 -->
    <dependency>
      <groupId>quartz</groupId>
      <artifactId>quartz</artifactId>
      <version>1.5.2</version>
    </dependency>
    <!-- 郵件支援 -->
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>aisell</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.15.v20140411</version>
        <configuration>
          <stopPort>9966</stopPort>
          <stopKey>foo</stopKey>
          <webAppConfig>
            <contextPath>/</contextPath>
          </webAppConfig>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
           

三 內建SpringDataJpa

3.1 完成Spring與Jpa的內建

3.1.1 準備jdbc.propeties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///yxb
jdbc.username=root
jdbc.password=123456
           

3.2.2 準備applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--掃描service層的類-->
    <context:component-scan base-package="cn.itsource.aisell.service" />
    <!--
        db.properties -> dataSource(配置資料源[連接配接池dbcp]) -> EntityManagerFactory -> dao -> service
            -> 事務 -> controller(內建SpringMVC) -> easyui
    -->
    <!--1.讀取db.properties, 注意:不要忘了加classpath-->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!--2.配置dbcp連接配接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--
        JPA:ORM規範  -> 很多架構實作了這個規範(hibernate,openjpa,toplink,...)
        3.配置EntityManagerFactory對象
            3.1 基本配置都是寫在Spring中(四大金剛,建表政策,方言,是否顯示SQL)
            3.2 Spring來建立這個對象(準備一個domain,如果運作的時候建立了表,就代表這個對象是成功的)
            alt+insert -> JPA -> LocalContainerEntityManagerFactoryBean
            ctrl+t/f4 : 有辦法看結構
    -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--掃描JPA支援的注解-->
        <property name="packagesToScan" value="cn.itsource.aisell.domain" />
        <!--
            告訴Spring我們使用的是哪一個架構來完成JPA規範
            這裡就需要我們配置一個擴充卡
            jpaVendorAdapter:JPA需要配置的擴充卡(我們會選擇hibernate)
        -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--方言-->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                <!--
                    建表政策(已經有表有資料,不要删除了)
                -->
                <property name="generateDdl" value="false" />
                <!--是否顯示sql-->
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>
    <!--4.加上事務-->
    <!--4.1 準備事務管理器-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <!--4.2 開啟(注解)事務支援-->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>
           

3.2 內建SpringDataJpa

3.2.1 準備domain(父類)

  1. BaseDomain
jpa中父類domain必需要加上:

@MappedSuperclass

/**
 * 泛化:繼承關系
 * 在我們JPA,如果要抽取一個父類,就必需加上 @MappedSuperclass
 *  非常明确定的告訴JPA,這是一個用于映射的父類
 */
@MappedSuperclass
public class BaseDomain {
    @Id
    @GeneratedValue
    protected Long id;

   //getter,setter ...
}
           
  1. Employeee
@Entity
@Table(name = "employee")
public class Employee extends BaseDomain {

    private String username;
    private String password;
    private Integer age;
    private String email;
    //getter,setter ...
   
}

           

3.2.2 準備Repository接口

/**
 * 關于Employee對象的CRUD(分頁排序) 就寫完了
 *  泛型一:對哪一個實體做CRUD
 *  泛型二:主鍵的類型
 */
public interface EmployeeRepository extends JpaRepository<Employee,Long> {
    ...
}
           

3.2.3 掃描repository

  • repository 就是咱們過去的dao
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      ...
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      ...
      http://www.springframework.org/schema/data/jpa
      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">
   ...

    <!-- 讓SpringDataJpa去掃描repository -->
    <jpa:repositories base-package="cn.itsource.aisell.repository"
                      entity-manager-factory-ref="entityManagerFactory"
                      transaction-manager-ref="transactionManager" />

</beans>
           

四.完成的CRUD功能

4.1 基本的CRUD

  • 查詢所有:

    employeeRepository.findAll()

  • 查詢一條資料:

    employeeRepository.findOne(1L)

    • 思考題: findOne與getOne的差別?
  • 添加/修改:

    employeeRepository.save(employee)

    • 主要看資料庫中是否有這個值
  • 删除 :

    employeeRepository.delete(274L)

4.2 分頁和排序

4.2.1 排序

/**
 * 排序對象
 *  第一個參數:排序的類型(升序還是降序)
 *  第二個參數:排序的屬性
 */
Sort sort = new Sort(Sort.Direction.DESC,"age");

List<Employee> list = employeeRepository.findAll(sort);
list.forEach(e-> System.out.println(e));
           

4.2.2 分頁

注:這裡的分頁是從0開始的
/**
 * page:第幾頁(0就是第1頁)
 * size:每頁條數
 */
Pageable pageable = new PageRequest(0,10);
//分頁對象
Page<Employee> list = employeeRepository.findAll(pageable);

list.forEach(e-> System.out.println(e));
           

4.2.3 分頁+排序

Sort sort = new Sort(Sort.Direction.DESC,"age");
/**
 * page:第幾頁(0就是第1頁)
 * size:每頁條數
 * sort:排序對象
 */
Pageable pageable = new PageRequest(0,10,sort);
//分頁對象
Page<Employee> list = employeeRepository.findAll(pageable);

list.forEach(e-> System.out.println(e));
           

4.3 名稱規則(根據條件進行查詢)

詳細規則請檢視文檔
/**
 * 根據規範(SpringDataJpa)寫方法名,就可以進行查詢
 * @param username
 * @return
 *  username =?
 */
//根據使用者名查詢一個員工
Employee findByUsername(String username);

//使用者名模糊查詢 username like ?
List<Employee> findByUsernameLike(String username);

//使用者名與郵件模糊查詢 username like ? and email like ?
List<Employee> findByUsernameLikeAndEmailLike(String username,String email);
           

4.4 Query注解查詢

//根據使用者名擷取使用者
@Query("select o from Employee o where o.username =?1")
Employee query01(String username);

//使用者名與郵件模糊查詢 username like ? and email like ?
@Query("select o from Employee o where o.username  like ?1 and o.email like ?2")
List<Employee> query02(String username,String email);
//    @Query("select o from Employee o where o.username  like :username and o.email like :email")
//    List<Employee> query02(@Param("username") String username,@Param("email")  String email);

//直接寫原生的SQL
@Query(nativeQuery=true,value="select * from employee")
List<Employee> query03();
           

五.JpaSpecificationExecutor

  • 是一個JPA的規範執行者
  • JPA2.0提供的Criteria API的使用封裝
  • 需要咱們的的repository繼承JpaSpecificationExecutor接口
interface EmployeeRepository extends JpaRepository<Employee,Long>,JpaSpecificationExecutor<Employee>
           

5.1 最簡單的查詢

@Test
public void testJpaSpecificationExecutor01() throws Exception{
    /**
     * 這裡的查詢我們需要自己去定義規則:Specification
     */
    List<Employee> list = employeeRepository.findAll(new Specification<Employee>() {
        /**
         * 這個方法就是幫咱們建立規則的方法,隻要把這個方法搞定,咱們就可以完成查詢了
         * @param root(根) : 代表了可以查詢和操作的實體對象的根
         *                可以幫助我們擷取到實體對應的字段
         * @param query(查詢) : 代表一個specific的頂層查詢對象
         *                 包含查詢的各個部分,比如select,from,where,group by ,order by 等
         *                  還可以支援and ,or的功能
         * @param cb :用來建構CriteriaQuery的建構器對象(相當于條件或者說條件組合)
         *                  主要判斷關系(和這個字段是相等,大于,小于like等)
         *                  支援 and,or的功能
         * @return Predicate:表明; 闡明; 斷言
         *          你把它當成  where username like ? and email like ? and age > ? ...
         */
        @Override
        public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            //1.拿到Employee中的username字段
            Path usernamePath = root.get("username");
            //2.加上字段的判斷關系
            // 參數1:字段(表達式)  2.值
            Predicate p1 = cb.like(usernamePath, "%1%");
            return p1;
        }
    });

    list.forEach(e-> System.out.println(e));
}
           

5.2 多個條件查詢

@Test
public void testJpaSpecificationExecutor02() throws Exception{
    //Specification:查詢規則
    List<Employee> list = employeeRepository.findAll(new Specification<Employee>() {
        // root:拿到字段(表達式)  cb:設定條件(>,<,=,like),and/or
        @Override
        public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            //拿到username并設定條件
            Path usernamePath = root.get("username");
            Predicate p1 = cb.like(usernamePath, "%1%");
            //拿到email并設定條件
            Path emailPath = root.get("email");
            Predicate p2 = cb.like(emailPath, "%2%");
            //拿到age并設定條件 gt/lt:大于/小于   ge/le:大于等于/小等等于
            Path agePath = root.get("age");
            Predicate p3 = cb.gt(agePath, 18);
            //把條件結合起來
            return cb.and(p1, p2, p3);
        }
    });

    list.forEach(e-> System.out.println(e));
}
           

5.3 查詢+分頁+排序

/**
 * 進階查詢加分頁
 * @throws Exception
 */
@Test
public void testJpaSpecificationExecutor03() throws Exception{
        //建立一個分頁對象
        Pageable pageable = new PageRequest(1,10);
        //分頁+進階查詢
        Page<Employee> page = employeeRepository.findAll(new Specification<Employee>() {
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path usernamePath = root.get("username");
                return cb.like(usernamePath, "%1%");
            }
        }, pageable);
        page.forEach(e->System.out.println(e));
}

/**
 * 進階查詢加分頁+排序
 * @throws Exception
 */
@Test
public void testJpaSpecificationExecutor04() throws Exception{
    Sort sort = new Sort(Sort.Direction.DESC,"age");
    //建立一個分頁對象
    Pageable pageable = new PageRequest(0,10,sort);
    //分頁+進階查詢
    Page<Employee> page = employeeRepository.findAll(new Specification<Employee>() {
        @Override
        public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Path usernamePath = root.get("username");
            return cb.like(usernamePath, "%1%");
        }
    }, pageable);
    page.forEach(e->System.out.println(e));
}
           

六.jpa-spec

  • 把JpaSpecificationExecutor變得更加簡單的
  • 使用的時候需要導包(咱們已經導入)
  • 文檔: https://github.com/wenhao/jpa-spec/blob/master/docs/3.1.0_cn.md

6.1 簡單查詢

注意:導的是:

import com.github.wenhao.jpa.Specifications;

//jpa-spec
//完成咱們的簡單查詢 username like ?
@Test
public void testJpaSpec01() throws Exception{
    Specification<Employee> spec = Specifications.<Employee>and()
            .like("username", "%1%")
            .build();
    List<Employee> list = employeeRepository.findAll(spec);
    list.forEach(e-> System.out.println(e));
}
           

6.2 多條件查詢

//jpa-spec
//完成咱們的簡單查詢 username like ? and email like ? and age>?
@Test
public void testJpaSpec02() throws Exception{
    Specification<Employee> spec = Specifications.<Employee>and()
            .like("username", "%1%")
            .like("email","%2%")
            .gt("age",18)
            .build();
    List<Employee> list = employeeRepository.findAll(spec);
    list.forEach(e-> System.out.println(e));
}
           

6.3 多條件+分頁

//jpa-spec
//完成咱們的簡單查詢 username like ? + 分頁 + 排序
@Test
public void testJpaSpec03() throws Exception{
    //建立排序對象
    Sort sort = new Sort(Sort.Direction.DESC,"age");
    //建立分頁對象
    Pageable pageable = new PageRequest(0,10,sort);
    //規則對象(查詢條件)
    Specification<Employee> spec = Specifications.<Employee>and()
            .like("username", "%1%")
            .build();
    //功能執行
    Page<Employee> page = employeeRepository.findAll(spec, pageable);

    page.forEach(e-> System.out.println(e));
}
           

七.Query抽取

  • Query是咱們的查詢對象

7.1 BaseQuery

  • 有經驗的人都會建立父類
    • 友善擴充
    • 公共的代碼
    • 制定規範
  • 目前咱們Query需要做的
    • 四個字段(currrentPage,pageSize,orderType,orderName)
    • 讓每個子類都有一個:

      createSpec()

    • 建立一個order對象
    • 解決了傳過來的是目前頁從1開始(SpringDataJpa是從0開始計算)
/**
 * 父類的作用:
 *   1.提供一些公共的屬性的方法(少寫代碼)
 *   2.對子類形成相應的規範
 *   3.為了以後代碼的擴充性
 */
public abstract class BaseQuery {

    //分頁 -> 目前第幾頁
    private int currentPage=1;
    //分頁 -> 每頁條數
    private int pageSize=10;
    //排序 -> 排序的類型 true(DESC)/false(ASC)
    private boolean orderType;
    //排序 -> 排序的字段 -> 如果這個字段為null,就代表不排序
    private String orderName;

    //建立排序對象
    public Sort createSort(){
        if(StringUtils.isNotBlank(orderName)){
            //orderName有值才做排序
            Sort sort = new Sort(orderType?Sort.Direction.DESC:Sort.Direction.ASC,orderName);
            return sort;
        }
        return null;
    }
    //要求隻要繼承了我,就必需有一個方法叫:createSpec()
    public abstract Specification createSpec();

    public int getJpaPage() {
        return currentPage-1;
    }

   //getter,setter....
   
}
           

7.2 EmployeeQuery

  • 設定目前對應的Domain的特有查詢字段
  • 實作

    createSpec()

/**
 * 員工查詢
 */
public class EmployeeQuery extends BaseQuery {

    //使用者名
    private String username;
    //郵件
    private String email;
    //年齡
    private Integer age;

    //查詢的規則應該在查詢對象中來建立
    @Override
    public Specification createSpec(){
        Specification<Employee> specification = Specifications.<Employee>and()
                .like(StringUtils.isNotBlank(username), "username", "%" + username + "%")
                .like(StringUtils.isNotBlank(email), "email", "%" + email + "%")
                .gt(age != null, "age", age)
                .build();
        return specification;
    }

    //getter,setter...
}

           

7.3 測試

/**
 * 有Query的查詢
 * @throws Exception
 */
@Test
public void testJpaSpec04() throws Exception{
    //以後咱們的查詢資料是從前台傳過來的,就會生成EmployeeQuery對象
    EmployeeQuery query = new EmployeeQuery();
//        query.setUsername("1");
//        query.setEmail("2");
//        query.setAge(18);
    query.setOrderName("age");
    query.setOrderType(true);

    //建立排序對象
    Sort sort = query.createSort();
    //建立分頁對象(分頁對象從前台傳過來)
    Pageable pageable = new PageRequest(query.getJpaPage(),query.getPageSize(),sort);
    //規則對象(查詢條件)
    Specification<Employee> spec = query.createSpec();
    //功能執行
    Page<Employee> page = employeeRepository.findAll(spec, pageable);
    page.forEach(e-> System.out.println(e));
}
           

繼續閱讀