Maven项目-集成SSJ(Spring+SpringMVC+SpringDataJpa)

Maven项目-集成SSJ(Spring+SpringMVC+SpringDataJpa)

  • 一.什么是SpringDataJpa
  • 二.项目导包
  • 三 集成SpringDataJpa
      • 3.1.1 准备db.propeties
      • 3.2.2 准备applicationContext.xml
  • 3.2 集成SpringDataJpa
      • 3.2.1 准备domain(父类)
      • 3.2.2 准备Repository接口
      • 3.2.3 扫描repository
  • 四.完成的CRUD功能
    • 4.1 基本的CRUD
    • 4.2 分页和排序
      • 4.2.1 排序
      • 4.2.2 分页
    • 4.2.3 分页+排序
    • 4.3 名称规则(根据条件进行查询)
    • 4.4 Query注解查询
  • 五.JpaSpecificationExecutor
    • 5.1 最简单的查询
    • 5.2 多个条件查询
    • 5.3 查询+分页+排序
  • 六.jpa-spec
    • 6.1 简单查询
    • 6.2 多条件查询
    • 6.3 多条件+分页
  • 七.Query抽取
    • 7.1 BaseQuery
    • 7.2 EmployeeQuery

一.什么是SpringDataJpa

  • 它是Spring的一个子框架
  • 集成Jpa,让咱们操作数据库变得更加的简单

二.项目导包

  • 将下面的中的代码拷贝到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">

  <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>aisellfinalName>
    <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>

三 集成SpringDataJpa

3.1.1 准备db.propeties

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

3.2.2 准备applicationContext.xml


<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
">
    
    <context:component-scan base-package="cn.momoqc.aisell.service" />
    
    
    <context:property-placeholder location="classpath:db.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}" />
    bean>
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="packagesToScan" value="cn.momoqc.aisell.domain" />
        
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                
                <property name="generateDdl" value="false" />
                
                <property name="showSql" value="true" />
            bean>
        property>
    bean>
    
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    bean>
    
    <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

<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
">
   ...

    
    <jpa:repositories base-package="cn.momoqc.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 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变得更加简单的
  • 使用的时候需要导包(咱们已经导入)
  • 文档

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));
}

你可能感兴趣的:(JAVA)