SpringMVC之JPA關於Specification複雜查詢、 Example實例查詢及QueryDSL查詢

關於JPA與SpringMVC的整合相關配置

《!----------------------------------------applicationContext-jpa---------------------------------》

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.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
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!-- 1.引入公共配置文件 db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--2.配置數據源(druid)-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 數據庫基本信息配置 -->
        <property name="dbType" value="mysql"></property>
        <property name="driverClassName" value="${dataSource.driverClassName}"></property>
        <property name="url"      value="${dataSource.url}"></property>
        <property name="username"          value="${dataSource.username}"></property>
        <property name="password"      value="${dataSource.password}"></property>
              <property name="filters" value="${druid.filters}" />
        <!-- 最大併發連接數 -->
        <property name="maxActive" value="${druid.maxActive}" />
        <!-- 初始化連接數量 -->
        <property name="initialSize" value="${druid.initialSize}" />
        <!-- 配置獲取連接等待超時的時間 -->
        <property name="maxWait" value="${druid.maxWait}" />
        <!-- 最小空閒連接數 -->
        <property name="minIdle" value="${druid.minIdle}" />
        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
        <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="${druid.testWhileIdle}" />
        <property name="testOnBorrow" value="${druid.testOnBorrow}" />
        <property name="testOnReturn" value="${druid.testOnReturn}" />
        <property name="maxOpenPreparedStatements" value="${druid.maxOpenPreparedStatements}" />
        <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandoned" value="${druid.removeAbandoned}" />
        <!-- 1800秒,也就是30分鐘 -->
        <property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}" />
        <!-- 關閉abanded連接時輸出錯誤日誌 -->
        <property name="logAbandoned" value="${druid.logAbandoned}" />
    </bean>
    
    <!--3.配置JPA entityManagerFactory-->
    <!-- 對“實體管理器”解釋:我們知道原生的jpa的配置信息是必須放在META-INF目錄下面的,並且名字必須叫做persistence.xml,
    這個叫做persistence-unit,就叫做持久化單元,放在這下面我們感覺不方便,不好,於是Spring提供了 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean這樣一個類,
    可以讓你的隨心所欲的起這個配置文件的名字,也可以隨心所欲的修改這個文件的位置,只需要在這裏指向這個位置就行。然而更加方便的做法是,直接把配置信息就寫在這裏更好,於是就有了這實體管理器這個bean。
    使用 <property name="packagesToScan" value="your entity package" /> !-->
    <!--定義實體的工廠bean-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--實體類位置-->
        <property name="packagesToScan" value="com.demo.entity"/>
        <!--持久化單元名-->
        <property name="persistenceUnitName" value="TestJPA" />
        <!--JPA提供者-->
        <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
        <!--JPA屬性-->
        <property name="jpaProperties">
            <props>
                <!--配置方言-->
                <prop key="hibernate.dialect" >${hibernate.dialect}</prop>    
                <!--激活查詢日誌功能-->            
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <!--優雅地輸出Sql-->
                 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <!--添加一條解釋型標註-->
                <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
                <prop key="hibernate.max_fetch_depth">2</prop>
            </props>
        </property>
    </bean>

    <!--第三步-->
    <!--定義事務管理器-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!--第四步-->
    <!--定義repository接口的存放目錄-->
    <!--定義接口實現的後綴,通常用Impl-->
    <!--定義實體工廠的引用-->
    <!--定義事務管理器的引用-->
    <jpa:repositories base-package="com.demo.dao"
                      repository-impl-postfix="Impl"
                      entity-manager-factory-ref="entityManagerFactory"
                      transaction-manager-ref="transactionManager"/>

    <!--第五步-->
    <!--聲明採用註解的方式申明事務-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

<!--------------------------------------------applicationContext-core------------------------------------->

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自動掃描的包 -->
    <context:component-scan base-package="com.demo.service">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 啓動AOP切面自動代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
</beans>

 

<!--------------------------------------------db.properties-------------------------------------------->

dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&&useSSL=false
dataSource.username=root
dataSource.password=gzkahjl
#druid start
druid.filters=stat
druid.maxActive=20
druid.initialSize=1
druid.maxWait=60000
druid.minIdle=10
druid.timeBetweenEvictionRunsMillis=60000
druid.minEvictableIdleTimeMillis=300000
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false
druid.maxOpenPreparedStatements=20
druid.removeAbandoned=true
druid.removeAbandonedTimeout=1800
druid.logAbandoned=true

hibernate.connection.pool_size=1
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
#hibernate.current_session_context_class=thread
#hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.cache.provider_class=org.hibernate.cache.internal.NoCacheProvider
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
hibernate.use_sql_comments=false

<!------------------------------------------相關entity類------------------------------------》

TUser類

@Entity
@Table(name = "t_user", schema = "demo", catalog = "")
public class TUser {
    private int id;
    private String name;
    private String password;
    private int deptId;
    
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getDeptId() {
        return deptId;
    }
    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }
   
}

TDepartment類

@Entity
@Table(name = "t_dept", schema = "demo", catalog = "")
public class TDepartment {

    private int deptId;
    private String departmentName;
    
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "deptId", unique = true, nullable = false)
    public int getDeptId() {
        return deptId;
    }
    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
   
}

相關接口:

@NoRepositoryBean
public class BaseRepository {

    @PersistenceContext(unitName = "TestJPA")
    protected EntityManager em;

}

//User表示該Repository與實體User關聯,主鍵類型爲Integer
@Repository
public interface UserRepository extends PagingAndSortingRepository<TUser, Integer>,JpaRepository<TUser, Integer>, JpaSpecificationExecutor<TUser> {

    /**
     * CrudRepository:提供基本CRUD操作
     * <S extends T> S save(S entity); <S extends T>
     * Iterable<S> save(Iterable<S> entities);
     * void delete(ID id); void delete(T entity);
     * void delete(Iterable<? extends T> entities);
     * void deleteAll(); T
     * findOne(ID id);
     * Iterable<T> findAll();
     * Iterable<T> findAll(Iterable<ID> ids);
     * boolean exists(ID id);
     * long count();
     */

    /**
     * PagingAndSortingRepository:提供分頁和排序功能    
     * Iterable<T> findAll(Sort sort);
     * Page<T> findAll(Pageable pageable);
     * Page:
            int getTotalPages();
            long getTotalElements();
     */
    
    /**
      *JpaSpecificationExecutor 動態查詢接口:
     *T findOne(Specification<T> spec);
     *List<T> findAll(Specification<T> spec);
     *Page<T> findAll(Specification<T> spec, Pageable pageable);
     *List<T> findAll(Specification<T> spec, Sort sort);
     *long count(Specification<T> spec);
     */
}

 

public interface TDepartmentRepository extends CrudRepository<TDepartment,Integer> ,QueryDslPredicateExecutor<TDepartment>,TDepartmentRepositoryCustom{

}

/**
 * QueryDSL自定義複雜查詢
 *
 * @author gzkahjl
 *
 */
public interface TDepartmentRepositoryCustom {
    /**
     * 關聯查詢示例,查詢出部門和對應的用戶
     *
     * @param predicate 查詢條件
     * @return 查詢實體
     */
    public List<Tuple> findDeptAndUser(Predicate predicate);
    
    /**
     * 自定義查詢並封裝到DTO
     */
    public QueryResults<TUserDeptDTO> findUserDeptDTO(Predicate predicate, Pageable pageable);

    /**
     * 關聯查詢示例,分頁查詢出部門和對應的用戶
     *
     * @param predicate 查詢條件
     * @return 查詢實體
     */
    public QueryResults<Tuple> findDeptAndUserPage(Predicate predicate, Pageable pageable);
}

//自定義Repository

//這裏的名字必須是TDepartmentRepositoryImp,否則無法注入成功。

//參考https://blog.csdn.net/qq_36722039/article/details/81148189

@Repository
public class TDepartmentRepositoryImpl extends BaseRepository implements TDepartmentRepositoryCustom {

    /**刪除
     *queryFactory.delete(QTDepartment.tDepartment).where(QTDepartment.tDepartment.deptId.eq(3)).execute();
     */
    @Override
    public List<Tuple> findDeptAndUser(Predicate predicate) {
        // TODO Auto-generated method stub
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);

        // 典型的左外連接
        JPAQuery<Tuple> jpaQuery = queryFactory.select(QTDepartment.tDepartment, QTUser.tUser)
                .from(QTDepartment.tDepartment).leftJoin(QTUser.tUser)
                .on(QTUser.tUser.id.eq(QTDepartment.tDepartment.deptId));
        jpaQuery.where(predicate);
        return jpaQuery.fetch();
    }
    
    @Override
    public QueryResults<TUserDeptDTO> findUserDeptDTO(Predicate predicate, Pageable pageable) {
        // TODO Auto-generated method stub
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);
        JPAQuery<TUserDeptDTO> jpaQuery = queryFactory.select((Projections.constructor(TUserDeptDTO.class,QTDepartment.tDepartment.deptId,QTDepartment.tDepartment.departmentName,QTUser.tUser.name)))
                .from(QTDepartment.tDepartment).leftJoin(QTUser.tUser)
                .on(QTUser.tUser.id.eq(QTDepartment.tDepartment.deptId));
        jpaQuery.where(predicate)
        .offset(pageable.getOffset())
        .limit(pageable.getPageSize());
        return jpaQuery.fetchResults();
    }


    @Override
    public QueryResults<Tuple> findDeptAndUserPage(Predicate predicate, Pageable pageable) {
        // TODO Auto-generated method stub
        //分頁帶條件查詢
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);
        JPAQuery<Tuple> jpaQuery = queryFactory.select(QTDepartment.tDepartment, QTUser.tUser)
                .from(QTDepartment.tDepartment).leftJoin(QTUser.tUser)
                .on(QTUser.tUser.id.eq(QTDepartment.tDepartment.deptId))
                .where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize());
        //分頁結果
        return jpaQuery.fetchResults();
    }

}

//測試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-core.xml", "classpath:applicationContext-jpa.xml" })
@Transactional
public class TestMyDao {

   ..................

    /**
     * JPA常用查詢
     */
    @Test
    public void test1() {
        Iterable<TUser> userList = userDao.findAll();
        userList.forEach((e) -> {
            System.out.println(e.getId() + "----" + e.getName());
        });
        /*
         * TUser tUser = null; List<TUser> list = new ArrayList<TUser>(0); for (int i =
         * 0; i < 10; i++) { tUser = new TUser(); tUser.setName("gzkahjl"+i);
         * list.add(tUser); } userDao.save(list);
         */
    }

    /**
     * Specification複雜查詢 使用JpaSpecificationExecutor高級查詢 分頁查詢
     */
    @Test
    public void test2() {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Iterable<TUser> findAll = userDao.findAll(new Specification<TUser>() {

            @Override
            public Predicate toPredicate(Root<TUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // TODO Auto-generated method stub
                List<Predicate> list = new ArrayList<Predicate>(0);
                Predicate c1 = cb.like(root.get("name").as(String.class), "%gzkahjl%");
                list.add(c1);
                Predicate[] p = new Predicate[list.size()];
                // 4.CriteriaBuilder的and 函數組裝 查詢條件數組
                return cb.and(list.toArray(p));
            }
        }, new PageRequest(0, 5, sort));

        findAll.forEach((e) -> {
            System.out.println(e.getId() + "----" + e.getName());
        });
    }

 

    /**
     * 學習:https://blog.csdn.net/qq_30054997/article/details/79420141 Example實例查詢
     * 創建一個ExampleMatcher對象,最後再用Example的of方法構造相應的Example對象並傳遞給相關查詢方法
     */
    @Test
    public void test3() {
        TUser user = new TUser();
        user.setName("gzk");
        ExampleMatcher matcher = ExampleMatcher.matching()
                // .withStringMatcher(StringMatcher.CONTAINING) //改變默認字符串匹配方式:即%{name}%
                // .withIncludeNullValues() //改變“Null值處理方式”:包括
                .withMatcher("name", GenericPropertyMatchers.contains()) // 姓名採用“開始匹配”的方式查詢,即{name}%
                // .withIgnorePaths("int"); //忽略屬性:是否關注。因爲是基本類型,需要忽略掉
                .withIgnorePaths("id");
        Example<TUser> example = Example.of(user, matcher); // Example根據域對象和配置創建一個新的ExampleMatcher
        List<TUser> findAll = userDao.findAll(example); // 忽略其他屬性
        findAll.forEach((e) -> {
            System.out.println(e.getId() + "----" + e.getName());
        });
    }

 

/**
     * https://www.jianshu.com/p/69dcb1b85bbb QueryDsl查詢
     */
    @Test
    public void test4() {
        // 簡單查詢
        QTDepartment tdepartment = QTDepartment.tDepartment;
        Iterable<TDepartment> findAll = deptDao.findAll(tdepartment.departmentName.like("%技術%"));
        findAll.forEach((e) -> {
            System.out.println(e.getDeptId() + "----" + e.getDepartmentName());
        });

        // 別一種查詢
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(tdepartment.departmentName.like("%商務%"));
        Iterable<TDepartment> findAll2 = deptDao.findAll(builder);
        findAll2.forEach((e) -> {
            System.out.println(e.getDeptId() + "----" + e.getDepartmentName());
        });

        // 帶條件排序查詢
        OrderSpecifier<Integer> order = new OrderSpecifier<Integer>(com.querydsl.core.types.Order.DESC,
                tdepartment.deptId);
        deptDao.findAll(tdepartment.departmentName.like("%技術%"), order);

    }

    @Test
    public void test5() {
        // 查詢並將結果封裝至dto中
        QueryResults<TUserDeptDTO> findUserDeptDTO = deptDao.findUserDeptDTO(null, new PageRequest(0, 5));
        findUserDeptDTO.getResults().forEach((e)->{
            System.out.println(e.getDeptId() + "----" + e.getDepartmentName());
        });

        QTDepartment qDept = QTDepartment.tDepartment;
        QTUser qtUser = QTUser.tUser;
        BooleanExpression predicate = qDept.departmentName.like("%技術%");
        List<Tuple> result = deptDao.findDeptAndUser(predicate);
        for (Tuple row : result) {
            System.out.println("qtDept:" + row.get(qDept));
            System.out.println("qtUser:" + row.get(qtUser));
        }
        System.out.println(result);
    }

 

    @Test
    public void test6() {
        QTDepartment qDept = QTDepartment.tDepartment;
        QTUser qUser = QTUser.tUser;
        BooleanExpression predicate = qDept.departmentName.like("%技術%");
        QueryResults<Tuple> result = deptDao.findDeptAndUserPage(predicate, new PageRequest(0,5));
         //結果取出
        for (Tuple row : result.getResults()) {
            System.out.println("qtDept:"+row.get(qDept).getDepartmentName());
            System.out.println("qtUser:"+row.get(qUser));
            System.out.println("--------------------");
        }
        //取出count查詢總數
        System.out.println(result.getTotal());
    }

}

<!---------------------------------------------My POM --------------------------------------------------->

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.kdw</groupId>
  <artifactId>demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
 <properties>
    <jdk.version>1.8</jdk.version>
      <spring.version>4.3.13.RELEASE</spring.version>
      <aspectj.version>1.8.13</aspectj.version>
      
      <gson.version>2.8.5</gson.version>
      <joda.version>2.3</joda.version>
      <jsp.version>2.3.1</jsp.version>
    <servlet.version>3.1.0</servlet.version>
    <slf4j.version>1.7.25</slf4j.version>
    <logback.version>1.1.2</logback.version>
    
      <spring-data-jpa.version>1.11.9.RELEASE</spring-data-jpa.version>
    <hibernate.version>5.1.0.Final</hibernate.version>
     <querydsl.version>4.1.4</querydsl.version>
    <mysql.version>5.1.38</mysql.version>
    <druid.version>1.1.10</druid.version>
    <junit.version>4.12</junit.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
 
  <dependencies>
 
      <!-- Spring Framework -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <!-- ============================Spring ORM=============================== -->
    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>    
    <!-- AOP -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    
        <!-- Hibernate JPA -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data-jpa.version}</version>
    </dependency>
    
   <!-- Hibernate Framework-->
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    
     <!--query dsl-->
    <dependency>
      <groupId>com.querydsl</groupId>
      <artifactId>querydsl-jpa</artifactId>
      <version>${querydsl.version}</version>
    </dependency>
    <dependency>
      <groupId>com.querydsl</groupId>
      <artifactId>querydsl-apt</artifactId>
      <version>${querydsl.version}</version>
      <scope>provided</scope>
    </dependency>
    <!--query dsl end-->
    
    <!-- mysql connector-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    
    <!-- 連接池 -->        
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
    </dependency>
      <!-- ============================Spring Web=============================== -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- ============================ Web 3.1 =============================== -->
    <!-- GSON -->
    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>${gson.version}</version>
    </dependency>
    <!-- Joda Time Library -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>${joda.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>${jsp.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlet.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
     </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-access</artifactId>
        <version>${logback.version}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>${logback.version}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
    <!-- ============================Spring Test=============================== -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
        <finalName>ssh-demo</finalName>
      <plugins>
      <!--該插件可以生成querysdl需要的查詢對象,執行mvn compile即可-->
      <plugin>
        <groupId>com.mysema.maven</groupId>
        <artifactId>apt-maven-plugin</artifactId>
        <version>1.1.3</version>
        <executions>
          <execution>
            <goals>
              <goal>process</goal>
            </goals>
            <configuration>
              <outputDirectory>target/generated-sources/java</outputDirectory>
              <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
          </execution>
        </executions>
      </plugin>
      
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>${jdk.version}</source>
                  <target>${jdk.version}</target>
                  <encoding>${project.build.sourceEncoding}</encoding>
                  <showWarnings>true</showWarnings>
              </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <charset>${project.build.sourceEncoding}</charset>
                <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
                <port>8080</port>
            </configuration>
        </plugin>
        <!-- 配置打包時跳過測試  -->
         <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
         </plugin>
      </plugins>
  </build>
</project>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章