集成Spring+SpringDataJpa

引入项目中所需要的所有包(pom.xml)


<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.0modelVersion>

  <groupId>cn.itsourcegroupId>
  <artifactId>yxbartifactId>
  <version>1.0-SNAPSHOTversion>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <org.springframework.version>4.2.5.RELEASEorg.springframework.version>
    <org.hibernate.version>4.3.8.Finalorg.hibernate.version>
    <spring-data-jpa.version>1.9.0.RELEASEspring-data-jpa.version>
    <com.fasterxml.jackson.version>2.5.0com.fasterxml.jackson.version>
    <org.slf4j.version>1.6.1org.slf4j.version>
  properties>
  <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${org.springframework.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-ormartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${org.springframework.version}version>
      <scope>testscope>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${org.springframework.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${org.springframework.version}version>
    dependency>
    
    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-ioartifactId>
      <version>1.3.2version>
    dependency>
    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.2.2version>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>${com.fasterxml.jackson.version}version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>${com.fasterxml.jackson.version}version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${com.fasterxml.jackson.version}version>
    dependency>
    
    <dependency>
      <groupId>org.hibernategroupId>
      <artifactId>hibernate-coreartifactId>
      <version>${org.hibernate.version}version>
    dependency>
    <dependency>
      <groupId>org.hibernategroupId>
      <artifactId>hibernate-entitymanagerartifactId>
      <version>${org.hibernate.version}version>
    dependency>
    
    <dependency>
      <groupId>org.springframework.datagroupId>
      <artifactId>spring-data-jpaartifactId>
      <version>${spring-data-jpa.version}version>
    dependency>
    
    <dependency>
      <groupId>com.github.wenhaogroupId>
      <artifactId>jpa-specartifactId>
      <version>3.1.1version>
      
      <exclusions>
        <exclusion>
          <groupId>*groupId>
          <artifactId>*artifactId>
        exclusion>
      exclusions>
    dependency>

    <dependency>
      <groupId>commons-dbcpgroupId>
      <artifactId>commons-dbcpartifactId>
      <version>1.2.2version>
    dependency>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.6version>
    dependency>

    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-lang3artifactId>
      <version>3.5version>
    dependency>
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>${org.slf4j.version}version>
    dependency>
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>${org.slf4j.version}version>
      <scope>runtimescope>
    dependency>
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.14version>
    dependency>
    
    <dependency>
      <groupId>org.apache.velocitygroupId>
      <artifactId>velocityartifactId>
      <version>1.6version>
    dependency>
  
   <dependency>
      <groupId>org.apache.shirogroupId>
      <artifactId>shiro-allartifactId>
      <version>1.4.0version>
      <type>pomtype>
  dependency>
    
    <dependency>
      <groupId>org.apache.shirogroupId>
      <artifactId>shiro-springartifactId>
      <version>1.4.0version>
    dependency>
    
    <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poiartifactId>
      <version>3.11version>
    dependency>
    <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poi-ooxmlartifactId>
      <version>3.11version>
    dependency>
    
    
    <dependency>
      <groupId>net.coobirdgroupId>
      <artifactId>thumbnailatorartifactId>
      <version>0.4.6version>
    dependency>
    
    <dependency>
      <groupId>quartzgroupId>
      <artifactId>quartzartifactId>
      <version>1.5.2version>
    dependency>
    
    <dependency>
      <groupId>javax.mailgroupId>
      <artifactId>mailartifactId>
      <version>1.4.1version>
    dependency>
  dependencies>

  <build>
    <finalName>yxbfinalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <configuration>
          <source>1.8source>
          <target>1.8target>
        configuration>
      plugin>
      <plugin>
        <groupId>org.mortbay.jettygroupId>
        <artifactId>jetty-maven-pluginartifactId>
        <version>8.1.15.v20140411version>
        <configuration>
          <stopPort>9966stopPort>
          <stopKey>foostopKey>
          <webAppConfig>
            <contextPath>/contextPath>
          webAppConfig>
        configuration>
      plugin>
    plugins>
  build>
project>

配置applicationContext.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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"
       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
         http://www.springframework.org/schema/data/jpa
         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    
    <context:component-scan base-package="cn.itsource.pss.service" />
    <context:property-placeholder location="classpath:jdbc.properties" />

    
    <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}" />
        
        <property name="maxActive" value="150" />
        
        <property name="minIdle" value="5" />
        
        <property name="maxIdle" value="20" />
        
        <property name="initialSize" value="30" />
        
        
        <property name="logAbandoned" value="true" />
        
        <property name="removeAbandoned" value="true" />
        
        <property name="removeAbandonedTimeout" value="10" />
        
        <property name="maxWait" value="1000" />
        
        <property name="timeBetweenEvictionRunsMillis" value="10000" />
        
        <property name="numTestsPerEvictionRun" value="10" />
        
        <property name="minEvictableIdleTimeMillis" value="10000" />
        <property name="validationQuery" value="SELECT NOW() FROM DUAL" />
    bean>

    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="packagesToScan" value="cn.itsource.aisell.domain" />
        
        
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                
                
                <property name="showSql" value="true" />
                
                <property name="generateDdl" value="false" />
                
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
            bean>
        property>
    bean>

    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    bean>
    
    <tx:annotation-driven />

    
    
    <jpa:repositories base-package="cn.itsource.pss.repository" transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactory" />
beans>

准备Domain

将id这个共同属性抽出形成一个父类

ackage cn.itsource.pss.domain;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
// 在JPA里面就表示是父类,不持久化到表
public class BaseDomain {
     
    @Id
    @GeneratedValue
    protected Long id;
    public Long getId() {
     
        return id;
    }
    public void setId(Long id) {
     
        this.id = id;
    }
}

子类Employee

@Entity
@Table(name="employee")
public class Employee extends  BaseDomain {
     

    private String username;
    private String password;
    private String email; 
    private Integer age;
   //省略getter,setter与toString(注意:Alt+Insert可以自动生成)
}

完成Repository的功能

package cn.itsource.aisell.repository;
import cn.itsource.aisell.domain.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;

public interface EmployeeRepository extends JpaRepository<Employee,Long> {
     
	//使用注解使用jpql
    @Query("select o from Employee o where o.username like:username and o.email like :email")
    List<Employee> query03(@Param("username") String username, @Param("email")  String email);
	//使用原生sql
    @Query(nativeQuery = true,value = "select count(*) from employee")
    Integer queryCount();
}

SpringDataJpa

它是JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL的查询语句 ,是属于Spring的生成体系中的一部分。
SpringDataJpa使用起来比较方便,加快了开发的效果,使开发人员不需要关心和配置更多的东西。
SpringDataJpa上手简单,开发效率高,对对象的支持非常好,还十分的灵活。

Spring Data项目是从2010年开发发展起来的,是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算(大数据)数据服务。 Spring Data 包含多个子项目:
Commons - 提供共享的基础框架,适合各个子项目使用,支持跨数据库持久化
JPA - 简化创建 JPA 数据访问层和跨存储的持久层功能 --springdata-jpa
Hadoop - 基于 Spring 的 Hadoop 作业配置和一个 POJO 编程模型的 MapReduce 作业
Key-Value - 集成了 Redis 和 Riak ,提供多个常用场景下的简单封装
Document - 集成文档数据库:CouchDB 和 MongoDB 并提供基本的配置映射和资料库支持
Graph - 集成 Neo4j 提供强大的基于 POJO 的编程模型
Graph Roo AddOn - Roo support for Neo4j
JDBC Extensions - 支持 Oracle RAD、高级队列和高级数据类型
Mapping - 基于 Grails 的提供对象映射框架,支持不同的数据库
Examples - 示例程序、文档和图数据库
Guidance - 高级文档
SpringDataJPA:对JPA进行封装处理,让我们操作jPA更加快速,简单,SpringDataJPA是属于SpringData下面的项目

SpringData的结构

Spring Data JPA的七个Repository接口:
Repository(org.springframework.data.repository.Repository)
CrudRepository(org.springframework.data.repository.CrudRepository)
PagingAndSortingRepository(org.springframework.data.repository.PagingAndSortingRepository)
JpaRepository (org.springframework.data.jpa.repository.JpaRepository)
QueryByExampleExecutor(org.springframework.data.repository.query.QueryByExampleExecutor)
JpaSpecificationExecutor (org.springframework.data.jpa.repository.JpaSpecificationExecutor)
QueryDslPredicateExecutor (org.springframework.data.querydsl.QueryDslPredicateExecutor)

两大Repository实现类:
SimpleJpaRepository(org.springframework.data.jpa.repository.support.SimpleJpaRepository)
QueryDslJpaRepository(org.springframework.data.jpa.repository.support.QueryDslJpaRepository)

JpaRepository这个接口,它又继承了PagingAndSortingRepository和CrudRepository两个接口

package org.springframework.data.repository;
import java.io.Serializable;
@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
     

   <S extends T> S save(S entity);
   <S extends T> Iterable<S> save(Iterable<S> entities);
   T findOne(ID id);
   boolean exists(ID id);
   Iterable<T> findAll();
   Iterable<T> findAll(Iterable<ID> ids);
   long count();
   void delete(ID id);
   void delete(T entity);
   void delete(Iterable<? extends T> entities);
   void deleteAll();
}

PagingAndSortingRepository:完成分页与排序功能

该接口继承了CrudRepository接口,提供了两个方法,实现了分页和排序的功能了。

package org.springframework.data.repository;

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
     
   Iterable<T> findAll(Sort sort);
   Page<T> findAll(Pageable pageable);
}

JpaRepository:

该接口继承了PagingAndSortingRepository接口。

package org.springframework.data.jpa.repository;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

/**
 * JPA specific extension of {@link org.springframework.data.repository.Repository}.
 * 
 * @author Oliver Gierke
 */
@NoRepositoryBean
public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
     

   List<T> findAll();
   List<T> findAll(Sort sort);
   List<T> findAll(Iterable<ID> ids);
   <S extends T> List<S> save(Iterable<S> entities);
   void flush();
   <S extends T> S saveAndFlush(S entity);
   void deleteInBatch(Iterable<T> entities);
   void deleteAllInBatch();
   T getOne(ID id);
}

JpaSpecification[规则,说明书的意思]Executor[执行者; 实行者]的认识

我们要使用这个接口中的方法,首先我们的接口去继承这个接口

public interface EmployeeRepository extends JpaRepository<Employee,Long>,JpaSpecificationExecutor<Employee> {
     

单个条件的查询

@Test
public void testFind() {
     
    /**
*官方解释:
     * Root root:代表了可以查询和操作的实体对象的根,
     *              可以通过它的 Path get(String attributeName); 这个方法拿到我们要操作的字段
     *              注意:只可以拿到对应的T的字段(Employee)
     * CriteriaQuery query:代表一个specific的顶层查询对象
     *              包含查询的各个部分,比如select,from,where,group by ,order by 等
     *              简单理解 就是它提供 了查询ROOT的方法(where,select,having)
     * CriteriaBuilder cb:用来构建CriteriaQuery的构建器对象(相当于条件或者说条件组合)
     *              构造好后以Predicate的形式返回
     */
/**
* 非官方理解:
 * 查询的时候就需要给一个标准(规范)
 *  -》 根据规范(这个规范我们可以先简单理解为查询的条件)进行查询
 *
 *      Root:查询哪个表(定位到表和字段-> 用于拿到表中的字段)
 *            可以查询和操作的实体的根
 *              Root接口:代表Criteria查询的根对象,Criteria查询的查询根定义了实体类型,能为将来导航获得想要的结果,它与SQL查询中的FROM子句类似
 *             Root 相当于 from Employee
 *             Root 相当于  from Product
 *      CriteriaQuery:查询哪些字段,排序是什么(主要是把多个查询的条件连系起来)
 *      CriteriaBuilder:字段之间是什么关系,如何生成一个查询条件,每一个查询条件都是什么方式
 *                      主要判断关系(和这个字段是相等,大于,小于like等)
 *      Predicate(Expression):单独每一条查询条件的详细描述 整个 where xxx=xx and yyy=yy ...
 */
    List<Employee> emps = employeeRepository.findAll(
            new Specification<Employee>() {
     
                @Override
                public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
     
                    Path path = root.get("username");//拿到要做查询的字段
                    Predicate p = cb.like(path, "%1%");//like代表做模糊查询,后面就是它的条件值
                    return p;
                }
            }
    );
    for (Employee emp : emps) {
     
        System.out.println(emp);
    }
}

多个条件查询

@Test
public void testFind02() {
     
    Specification spec = new Specification<Employee>() {
     
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
     
            //加上第一个条件: username like '%1%'
            Path path1 = root.get("username");
            Predicate p1 = cb.like(path1, "%1%");
            //加上第二个条件: email like '%2%'
            Path path2 = root.get("email");
            Predicate p2 = cb.like(path2,"%2%");
            //加上第二个条件: age < 20
            Path path3 = root.get("age");
            Predicate p3 = cb.lt(path3, 20);
           //下面是加上or的条件的方案
           //Predicate p3 = cb.or(p1,p2);

            //把两个查询条件放到query对象中去(条件使用where)
            CriteriaQuery where = query.where(p1, p2, p3);
            //返回查询条件
            return where.getRestriction();
        }
    };

    List<Employee> emps = employeeRepository.findAll(spec);
    for (Employee emp : emps) {
     
        System.out.println(emp);
    }

}

条件查询+分页排序

@Test
public void testFind03() {
     
    Specification spec = new Specification<Employee>() {
     
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
     
            //加上条件: username like '%1%'
            Path path1 = root.get("username");
            Predicate p1 = cb.and(cb.like(path1, "%1%"));
            //把两个查询条件放到query对象中去(条件使用where)
            CriteriaQuery where = query.where(p1);
            //返回查询条件
            return where.getRestriction();
        }
    };

    //排序 :第一个参数是排序的规则(DESC/ASC)  后面参数是排序的字符
    Sort sort = new Sort(Sort.Direction.DESC,"username");
    Pageable pageable = new PageRequest(0, 10,sort);

    Page<Employee> page = employeeRepository.findAll(spec, pageable);
    for (Employee emp : page) {
     
        System.out.println(emp);
    }
}

jpa-spec插件

在Maven中引入相应的包
pom.xml(咱们项目中已经引入):


<dependency>
  <groupId>com.github.wenhaogroupId>
  <artifactId>jpa-specartifactId>
  <version>3.1.1version>
  
  <exclusions>
    <exclusion>
      <groupId>*groupId>
      <artifactId>*artifactId>
    exclusion>
  exclusions>
dependency>

单个条件查询

@Test
public void testSpecFind01() {
     
    Specification<Employee> spec = Specifications.<Employee>and().like("username", "%1%").build();
    List<Employee> emps = employeeRepository.findAll(spec);
    for (Employee emp : emps) {
     
        System.out.println(emp);
    }
}

多个条件查询

@Test
public void testSpecFind02() {
     
    Specification<Employee> spec = Specifications.<Employee>and().like("username", "%1%")
            .like("email","%2%").lt("age", 20).build();
    List<Employee> emps = employeeRepository.findAll(spec);
    for (Employee emp : emps) {
     
        System.out.println(emp);
    }
}

条件+排序分页功能

//条件集成+分页
@Test
public void testSpecFind03() {
     
    Specification<Employee> spec = Specifications.<Employee>and().like("username", "%1%").build();
    //排序 :第一个参数是排序的规则(DESC/ASC)  后面参数是排序的字符
    Sort sort = new Sort(Sort.Direction.DESC,"username");
    Pageable pageable = new PageRequest(0, 10,sort);

    Page<Employee> page = employeeRepository.findAll(spec, pageable);
    for (Employee emp : page) {
     
        System.out.println(emp);
    }
}

你可能感兴趣的:(集成Spring+SpringDataJpa)