Spring Data

来源:互联网 发布:李玖哲 解脱 知乎 编辑:程序博客网 时间:2024/06/09 06:16

大部分内容来自:https://segmentfault.com/a/1190000009211986#articleHeader9

Spring Data是什么

Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库、Map-Reduce 框架、云数据服务等等;另外也包含对关系数据库的访问支持。

这里写图片描述

使用spring data可以简单的通过配置就达到切换数据库的目的,给我们一种通用的编码模式统一接口

Spring Data JPA

开发环境搭建

Spring Data JPA快速起步

开发环境搭建Spring Data JPA HelloWorld开发

代码演示:

1、创建DB配置文件

jdbc.url = jdbc:mysql:///springdatajdbc.username = rootjdbc.password = rootjdbc.driverClass = com.mysql.jdbc.Driver

2、创建配置文件类

package com.myimooc.springdata.jpa.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * Created by ZC on 2017/4/24. */@PropertySource(value="classpath:db.properties")@Componentpublic class PropertiesConfig {    @Value("${jdbc.driverClass}")    private String jdbcDriverClass;    @Value("${jdbc.url}")    private String jdbcUrl;    @Value("${jdbc.username}")    private String jdbcUser;    @Value("${jdbc.password}")    private String jdbcPassword;    @Override    public String toString() {        return "Properties{" +                "jdbcDriverClass='" + jdbcDriverClass + '\'' +                ", jdbcUrl='" + jdbcUrl + '\'' +                ", jdbcUser='" + jdbcUser + '\'' +                ", jdbcPassword='" + jdbcPassword + '\'' +                '}';    }    public String getJdbcDriverClass() {        return jdbcDriverClass;    }    public String getJdbcUrl() {        return jdbcUrl;    }    public String getJdbcUser() {        return jdbcUser;    }    public String getJdbcPassword() {        return jdbcPassword;    }}

3、配置TransactionManager、EntityManagerFactory和Spring自动扫描注入

package com.myimooc.springdata.jpa.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.jdbc.datasource.DriverManagerDataSource;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.Database;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import java.util.Properties;/** * Spring配置类 * Created by ZC on 2017/4/24. */// 声明为配置类@Configuration// 启用事务管理@EnableTransactionManagement// 启用自动扫描继承 JpaRepository 接口的类。// 注意,此注解需要配置 entityManagerFactory 和 transactionManager// 方式一:定义获取Bean方法名为 entityManagerFactory 和 transactionManager// 方式二:配置 @EnableJpaRepositories注解的 entityManagerFactoryRef 属性 为自定义获取Bean的方法名。@EnableJpaRepositories(basePackages = "com.myimooc.springdata.jpa")// 启用自动扫描 @Component 注解的Bean@ComponentScan(basePackages = "com.myimooc.springdata.jpa")public class SpringConfig{    @Autowired    private PropertiesConfig propertiesConfig;    /**     * 配置数据源     * @return     */    @Bean    public DriverManagerDataSource dataSource(){        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();        driverManagerDataSource.setDriverClassName(propertiesConfig.getJdbcDriverClass());        driverManagerDataSource.setUrl(propertiesConfig.getJdbcUrl());        driverManagerDataSource.setUsername(propertiesConfig.getJdbcUser());        driverManagerDataSource.setPassword(propertiesConfig.getJdbcPassword());        return driverManagerDataSource;    }    /**     * 配置事务管理器 JpaTransactionManager     * @return     */    @Bean(name="transactionManager")    public PlatformTransactionManager transactionManager(){        JpaTransactionManager transactionManager = new JpaTransactionManager();        transactionManager.setDataSource(this.dataSource());        transactionManager.setEntityManagerFactory(this.entityManagerFactory().getObject());        return transactionManager;//        return new DataSourceTransactionManager(this.dataSource());//        return new JpaTransactionManager(this.entityManagerFactory().getObject());    }    /**     * 配置JPA的 EntityManagerFactory     * @return     */    @Bean    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();        entityManagerFactory.setDataSource(dataSource());        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();        jpaVendorAdapter.setGenerateDdl(true);        jpaVendorAdapter.setDatabase(Database.MYSQL);        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);        entityManagerFactory.setPackagesToScan("com.myimooc.springdata.jpa");        Properties jpaProperties = new Properties();//        jpaProperties.setProperty("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");        jpaProperties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect");        jpaProperties.setProperty("hibernate.show_sql","true");        jpaProperties.setProperty("hibernate.format_sql","true");        jpaProperties.setProperty("hibernate.hbm2ddl.auto","update");        entityManagerFactory.setJpaProperties(jpaProperties);        return entityManagerFactory;    }}

4、编写实体类:Employee

package com.myimooc.springdata.jpa.domain;import javax.persistence.*;/** * 雇员:先开发实体类,然后自动生成实体表 * Created by ZC on 2017/4/24. */@Entity@Table(name = "test_employee")public class Employee {    @Id    @GeneratedValue    private Integer id;    @Column(length = 20)    private String name;    private Integer age;    @Override    public String toString() {        return "Employee{" +                "id=" + id +                ", name='" + name + '\'' +                ", age=" + age +                '}';    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

起步程序开发

代码演示:

1、编写EmployeeRepository接口

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.domain.Employee;import org.springframework.data.jpa.repository.Modifying;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.Repository;import org.springframework.data.repository.query.Param;import org.springframework.transaction.annotation.Transactional;import java.util.List;/** * 使用 Repository 接口 * Created by ZC on 2017/4/25. */// 方式二:使用 @RepositoryDefinition 注解//  @RepositoryDefinition(domainClass = Employee.class,idClass = Integer.class)public interface EmployeeRepository extends Repository<Employee,Integer> {//方式一:继承 Repository 接口    /**     * 获取雇员对象通过名称     * @param name     * @return     */    Employee findByName(String name);}

2、编写单元测试类:EmployeeRepositoryTest

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.config.SpringConfig;import com.myimooc.springdata.jpa.domain.Employee;import com.myimooc.springdata.jpa.repository.EmployeeRepository;import com.myimooc.springdata.jpa.service.EmployeeService;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import java.util.ArrayList;import java.util.List;/** * EmployeeRepository单元测试类 * Created by ZC on 2017/4/24. */public class EmployeeRepositoryTest {    private ApplicationContext ctx = null;    private EmployeeRepository employeeRepository = null;    @Before    public void init(){        ctx = new AnnotationConfigApplicationContext(SpringConfig.class);        employeeRepository = ctx.getBean(EmployeeRepository.class);    }    @After    public void destroy(){        ctx = null;    }    @Test    public void entityManageFactoryTest(){        LocalContainerEntityManagerFactoryBean entityManagerFactory = (LocalContainerEntityManagerFactoryBean)ctx.getBean(LocalContainerEntityManagerFactoryBean.class);        Assert.assertNotNull(entityManagerFactory);    }    @Test    public void findByNameTest(){        System.out.println(employeeRepository);        Employee employee = employeeRepository.findByName("cc");        if( null == employee){            System.out.println("查询数据为空");        }else{            System.out.println(employee.toString());        }    }}

Repository

Repository:Spring Data核心类RepositoryDefinition:使用该注解进行配置Repository Query Specification:查询时,方法命名不能乱写Query Annotation:使用该注解,可以实现原生SQL查询Update/Delete/Transaction:更新、删除操作,支持事务

Repository Hierarchy

CrudRepository:内置了新增、更新、删除、查询方法PagingAndSortingRespository:分页和排序JpaRepositoryJpaSpecificationExcutor

Spring Data JPA进阶

关于Repository接口

Repository接口详解

Repository接口是Spring Data的核心接口,不提供任何方法public interface Repository<T, ID extends Serializable>{}@RepositoryDefinition注解的使用

Repository类的定义:

1)Repository是一个空接口,标记接口。没有包含方法声明的接口2)如果我们定义的接口EmployeeRepository extends Repository,会被Spring管理。    如果我们自己的接口没有extends Repository,运行时会报错,没有这个Bean。

Repository子接口详解

Repository子接口详解

CrudRepository:继承Repository,实现了CRUD相关的方法PagingAndSortingRepository:继承CrudRepository,实现了分页排序相关的方法JpaRepository:继承PagingAndSortingRepositor,实现JPA规范相关的方法

查询方法定义规则和使用

Repository中查询方法定义规则和使用

了解Spring Data中查询方法名称的定义规则使用Spring Data完成复杂查询方法名称的命名

查询方法定义规则

这里写图片描述
这里写图片描述

代码演示:

1、在EmployeeRepository接口编写以下代码

    // 使用JPA规范查询    // where name like ?% and age < ?    List<Employee> findByNameStartingWithAndAgeLessThan(String name,Integer age);    // where name like %? and age < ?    List<Employee> findByNameEndingWithAndAgeLessThan(String name,Integer age);    // where name in (?,?...) or age < ?    List<Employee> findByNameInOrAgeLessThan(List<String> name,Integer age);    // where name in (?,?...) and age < ?    List<Employee> findByNameInAndAgeLessThan(List<String> name,Integer age);

2、在EmployeeRepositoryTest单元测试类进行测试

1)方法名比较长:约定大于配置2)对于一些复杂的查询,是很难实现。

使用@Query注解来解决。

Query注解使用

Query注解使用

Respository方法中使用,不需要遵循查询方法命令规则只需要将@Query定义在Respository中的方法之上即可命名参数及索引参数的使用本地查询

代码演示:

1、在EmployeeRepository接口编写以下代码

// 使用@Query注解查询    /**     * 自定义查询SQL     * */    @Query("select o from Employee o where id=(select max(id) from Employee t1)")    Employee getEmployeeByMaxId();    /**     * 使用占位符进行参数绑定     * */    @Query("select o from Employee o where o.name=?1 and o.age=?2")    List<Employee> listEmployeeByNameAndAge(String name, Integer age);    /**     * 使用命名参数进行参数绑定     * */    @Query("select o from Employee o where o.name=:name and o.age=:age")    List<Employee> listEmployeeByNameAndAge2(@Param("name") String name, @Param("age")Integer age);    /**     * 自定义查询SQL,like,占位符进行参数绑定     * */    @Query("select o from Employee o where o.name like %?1%")    List<Employee> listEmployeeByLikeName(String name);    /**     * 自定义查询SQL,like,命名参数进行参数绑定     * */    @Query("select o from Employee o where o.name like %:name%")    List<Employee> listEmployeeByLikeName2(@Param("name") String name);    /**     * 使用原生态SQL查询     * @return     */    @Query(nativeQuery = true,value = "select count(1) from employee")    long getCount();

2、在EmployeeRepositoryTest单元测试类进行测试

//  使用 @Query 注解查询    @Test    public void getEmployeeByMaxIdTest(){        Employee employee = employeeRepository.getEmployeeByMaxId();        if( null != employee ){                System.out.println(employee.toString());        }else{            System.out.println("查询数据为空");        }    }    @Test    public void listEmployeeByNameAndAgeTest(){        List<Employee> employees = employeeRepository.listEmployeeByNameAndAge("zhangsan",20);        if( null != employees && employees.size() > 0){            for (Employee employee : employees) {                System.out.println(employee.toString());            }        }else{            System.out.println("查询数据为空");        }    }    @Test    public void listEmployeeByNameAndAge2Test(){        List<Employee> employees = employeeRepository.listEmployeeByNameAndAge2("zhangsan",20);        if( null != employees && employees.size() > 0){            for (Employee employee : employees) {                System.out.println(employee.toString());            }        }else{            System.out.println("查询数据为空");        }    }    @Test    public void listEmployeeByLikeNameTest(){        List<Employee> employees = employeeRepository.listEmployeeByLikeName("test1");        if( null != employees && employees.size() > 0){            for (Employee employee : employees) {                System.out.println(employee.toString());            }        }else{            System.out.println("查询数据为空");        }    }    @Test    public void listEmployeeByLikeName2Test(){        List<Employee> employees = employeeRepository.listEmployeeByLikeName2("test");        if( null != employees && employees.size() > 0){            for (Employee employee : employees) {                System.out.println(employee.toString());            }        }else{            System.out.println("查询数据为空");        }    }    @Test    public void getCountTest(){        long count = employeeRepository.getCount();        System.out.println(count);    }

更新操作整合事务使用

更新及删除操作整合事务的使用

@Modifying注解使用@Modifying结合@Query注解执行更新操作@Transaction在Spring Data中的使用

事务在Spring data中的使用:

1)事务一般是在Service层2@Query@Modifying@Transaction的综合使用

代码演示:

1、基于javaconfig在SpringConfig类进行事务配置

package com.myimooc.springdata.jpa.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.jdbc.datasource.DriverManagerDataSource;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.Database;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import java.util.Properties;/** * Spring配置类 * Created by ZC on 2017/4/24. */// 声明为配置类@Configuration// 启用事务管理@EnableTransactionManagement// 启用自动扫描继承 JpaRepository 接口的类。// 注意,此注解需要配置 entityManagerFactory 和 transactionManager// 方式一:定义获取Bean方法名为 entityManagerFactory 和 transactionManager// 方式二:配置 @EnableJpaRepositories注解的 entityManagerFactoryRef 属性 为自定义获取Bean的方法名。@EnableJpaRepositories(basePackages = "com.myimooc.springdata.jpa")// 启用自动扫描 @Component 注解的Bean@ComponentScan(basePackages = "com.myimooc.springdata.jpa")public class SpringConfig{    @Autowired    private PropertiesConfig propertiesConfig;    /**     * 配置数据源     * @return     */    @Bean    public DriverManagerDataSource dataSource(){        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();        driverManagerDataSource.setDriverClassName(propertiesConfig.getJdbcDriverClass());        driverManagerDataSource.setUrl(propertiesConfig.getJdbcUrl());        driverManagerDataSource.setUsername(propertiesConfig.getJdbcUser());        driverManagerDataSource.setPassword(propertiesConfig.getJdbcPassword());        return driverManagerDataSource;    }    /**     * 配置事务管理器 JpaTransactionManager     * @return     */    @Bean(name="transactionManager")    public PlatformTransactionManager transactionManager(){        JpaTransactionManager transactionManager = new JpaTransactionManager();        transactionManager.setDataSource(this.dataSource());        transactionManager.setEntityManagerFactory(this.entityManagerFactory().getObject());        return transactionManager;//        return new DataSourceTransactionManager(this.dataSource());//        return new JpaTransactionManager(this.entityManagerFactory().getObject());    }    /**     * 配置JPA的 EntityManagerFactory     * @return     */    @Bean    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();        entityManagerFactory.setDataSource(dataSource());        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();        jpaVendorAdapter.setGenerateDdl(true);        jpaVendorAdapter.setDatabase(Database.MYSQL);        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);        entityManagerFactory.setPackagesToScan("com.myimooc.springdata.jpa");        Properties jpaProperties = new Properties();//        jpaProperties.setProperty("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");        jpaProperties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect");        jpaProperties.setProperty("hibernate.show_sql","true");        jpaProperties.setProperty("hibernate.format_sql","true");        jpaProperties.setProperty("hibernate.hbm2ddl.auto","update");        entityManagerFactory.setJpaProperties(jpaProperties);        return entityManagerFactory;    }}

2、在EmployeeRepository接口编写以下代码

    // 更新数据    @Transactional    @Modifying    @Query("update Employee o set o.age = :age where o.id = :id")    void updateAgeById(@Param("id")Integer id,@Param("age")Integer age);

3、定义Service层,实际开发中,需要定义接口,这里为了演示方便,直接使用类。

package com.myimooc.springdata.jpa.service;import com.myimooc.springdata.jpa.domain.Employee;import com.myimooc.springdata.jpa.repository.EmployeeCrudRepository;import com.myimooc.springdata.jpa.repository.EmployeeRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.List;/** * Created by ZC on 2017/4/25. */@Servicepublic class EmployeeService {    @Autowired    private EmployeeRepository employeeRepository;    @Autowired    private EmployeeCrudRepository employeeCrudRepository;    @Transactional    public void updateAgeById(Integer id, Integer age){        this.employeeRepository.updateAgeById(id,age);    };    @Transactional    public void save(List<Employee> employees){        this.employeeCrudRepository.save(employees);    }}

4、编写EmployeeService单元测试类

package com.myimooc.springdata.jpa.service;import com.myimooc.springdata.jpa.config.SpringConfig;import com.myimooc.springdata.jpa.repository.EmployeeRepository;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.transaction.PlatformTransactionManager;/** * EmployeeService单元测试类 * Created by ZC on 2017/4/25. */public class EmployeeServiceTest {    private ApplicationContext ctx = null;    private EmployeeService employeeService = null;    @Before    public void init(){        ctx = new AnnotationConfigApplicationContext(SpringConfig.class);        employeeService = ctx.getBean(EmployeeService.class);    }    @After    public void destroy(){        ctx = null;    }    @Test    public void transactionManagerTest(){        PlatformTransactionManager transactionManager = (PlatformTransactionManager)ctx.getBean(PlatformTransactionManager.class);        Assert.assertNotNull(transactionManager);    }    // 更新操作    @Test    public void updateAgeByIdTest(){        employeeService.updateAgeById(1,55);    }}

Spring Data JPA高级

CrudRepository接口使用详解

CrudRepository接口使用详解

save(entity):保存一个实体save(entities):保存多个实体findOne(id):找到一个对象exists(id):根据ID判断对象是否存在findAll():找到所有实体对象delete(id):根据ID删除实体对象delete(entity):根据实体对象删除实体对象delete(entities):删除多个实体对象deleteAll():删除所有实体对象

代码演示:

1、编写EmployeeCrudRepository接口

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.domain.Employee;import org.springframework.data.repository.CrudRepository;/** * 使用 CrudRepository 接口 * Created by ZC on 2017/4/26. */public interface EmployeeCrudRepository extends CrudRepository<Employee,Integer>{}

2、编写EmployeeCrudRepositoryTest单元测试类

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.config.SpringConfig;import com.myimooc.springdata.jpa.domain.Employee;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.ArrayList;import java.util.List;/** * EmployeeRepository单元测试类 * Created by ZC on 2017/4/24. */public class EmployeeCrudRepositoryTest {    private ApplicationContext ctx = null;    private EmployeeCrudRepository employeeCrudRepository = null;    @Before    public void init(){        ctx = new AnnotationConfigApplicationContext(SpringConfig.class);        employeeCrudRepository = ctx.getBean(EmployeeCrudRepository.class);    }    @After    public void destroy(){        ctx = null;    }    @Test    public void saveTest(){        List<Employee> employees = new ArrayList<Employee>();        Employee employee = null;        for(int i=0;i<100;i++){            employee = new Employee();            employee.setName("test"+i);            employee.setAge(100 - i);            employees.add(employee);        }        employeeCrudRepository.save(employees);    }}

PagingAndSortingRespository接口使用详解

PagingAndSortingRespository接口使用详解

该接口包含分页和排序的功能带排序的查询:findAll(Sort sort)带排序的分页查询:findAll(Pageable pageable)

代码演示:

1、编写EmployeePagingAndSortingRepository接口

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.domain.Employee;import org.springframework.data.repository.PagingAndSortingRepository;/** * 使用 PagingAndSortingRepository 实现分页和排序功能 * Created by ZC on 2017/4/26. */public interface EmployeePagingAndSortingRepository extends PagingAndSortingRepository<Employee,Integer> {}

2、编写EmployeePagingAndSortingRepositoryTest单元测试类

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.config.SpringConfig;import com.myimooc.springdata.jpa.domain.Employee;import com.myimooc.springdata.jpa.repository.EmployeePagingAndSortingRepository;import com.myimooc.springdata.jpa.service.EmployeeService;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.data.repository.PagingAndSortingRepository;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import java.util.ArrayList;import java.util.List;/** * PagingAndSortingRepository 单元测试类 * Created by ZC on 2017/4/26. */public class EmployeePagingAndSortingRepositoryTest {    private ApplicationContext ctx = null;    private EmployeePagingAndSortingRepository employeePagingAndSortingRepository = null;    @Before    public void init(){        ctx = new AnnotationConfigApplicationContext(SpringConfig.class);        employeePagingAndSortingRepository = ctx.getBean(EmployeePagingAndSortingRepository.class);    }    @After    public void destroy(){        ctx = null;    }    /**     * 分页功能测试     */    @Test    public void pageTest(){        // page: index是从0开始的,不是从1开始的        Pageable pageable = new PageRequest(0,9);        Page<Employee> employeePage = employeePagingAndSortingRepository.findAll(pageable);        System.out.println("查询的总页数:"+employeePage.getTotalPages());        System.out.println("查询的总记录数:"+employeePage.getTotalElements());        System.out.println("查询的当前第几页:"+(employeePage.getNumber() + 1));        System.out.println("查询的当前页面的集合:"+employeePage.getContent());        System.out.println("查询的当前页面的记录数:"+employeePage.getNumberOfElements());    }    /**     * 分页和排序功能测试     */    @Test    public void pageAndSort(){        Sort.Order order = new Sort.Order(Sort.Direction.ASC,"id");        Sort sort = new Sort(order);        // page: index是从0开始的,不是从1开始的        Pageable pageable = new PageRequest(0,5,sort);        Page<Employee> employeePage = employeePagingAndSortingRepository.findAll(pageable);        System.out.println("查询的总页数:"+employeePage.getTotalPages());        System.out.println("查询的总记录数:"+employeePage.getTotalElements());        System.out.println("查询的当前第几页:"+(employeePage.getNumber() + 1));        System.out.println("查询的当前页面的集合:"+employeePage.getContent());        System.out.println("查询的当前页面的记录数:"+employeePage.getNumberOfElements());    }}

JpaRepository接口使用详解

JpaRepository接口使用详解

finaAll:查询所有记录findAll(Sort sort):查询所有记录并排序save(entities):保存多个实体对象fiush:deleteInBatch(entities):一个批次里面删除那些实体

代码演示:

1、编写EmployeeJpaRepository接口

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.domain.Employee;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.PagingAndSortingRepository;/** * 使用 JpaRepository 接口 * Created by ZC on 2017/4/26. */public interface EmployeeJpaRepository extends JpaRepository<Employee,Integer> {}

2、编写EmployeeJpaRepositoryTest单元测试类

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.config.SpringConfig;import com.myimooc.springdata.jpa.domain.Employee;import com.myimooc.springdata.jpa.repository.EmployeeJpaRepository;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.PagingAndSortingRepository;/** * EmployeeJpaRepository 单元测试类 * Created by ZC on 2017/4/26. */public class EmployeeJpaRepositoryTest {    private ApplicationContext ctx = null;    private EmployeeJpaRepository employeeJpaRepository = null;    @Before    public void init(){        ctx = new AnnotationConfigApplicationContext(SpringConfig.class);        employeeJpaRepository = ctx.getBean(EmployeeJpaRepository.class);    }    @After    public void destroy(){        ctx = null;    }    @Test    public void findTest(){        Employee employee = employeeJpaRepository.findOne(99);        System.out.println("employee"+employee.toString());        System.out.println("employee(10)"+employeeJpaRepository.exists(10));        System.out.println("employee(102)"+employeeJpaRepository.exists(102));    }}

JpaSpecificationExecutor接口使用详解

JpaSpecificationExecutor接口使用详解

Specification封装了JPA Criteria查询条件

代码演示:

1、编写EmployeeJpaSpecificationExecutor接口

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.domain.Employee;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor;/** * 使用 JpaSpecificationExecutor 接口 * Created by ZC on 2017/4/26. */public interface EmployeeJpaSpecificationExecutor extends JpaRepository<Employee,Integer> ,        JpaSpecificationExecutor<Employee>{}

2、编写EmployeeJpaSpecificationExecutorTest单元测试类

package com.myimooc.springdata.jpa.repository;import com.myimooc.springdata.jpa.config.SpringConfig;import com.myimooc.springdata.jpa.domain.Employee;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.data.jpa.domain.Specification;import javax.persistence.criteria.*;/** * EmployeeJpaSpecificationExecutor 单元测试类 * Created by ZC on 2017/4/26. */public class EmployeeJpaSpecificationExecutorTest {    private ApplicationContext ctx = null;    private EmployeeJpaSpecificationExecutor employeeJpaSpecificationExecutor = null;    @Before    public void init(){        ctx = new AnnotationConfigApplicationContext(SpringConfig.class);        employeeJpaSpecificationExecutor = ctx.getBean(EmployeeJpaSpecificationExecutor.class);    }    @After    public void destroy(){        ctx = null;    }    /**     * 1、分页     * 2、排序     * 3、查询条件:age > 50     */    @Test    public void queryTest(){        Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");        Sort sort = new Sort(order);        // page: index是从0开始的,不是从1开始的        Pageable pageable = new PageRequest(0,5,sort);        /**         * root : 就是我们要查询的类型 (Employee)         * query : 添加查询条件         * cb : 构建 Predicate         */        Specification<Employee> specification = new Specification<Employee>() {            // 查询条件            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {                // root (employee (age))                Path path = root.get("age");                return cb.gt(path,50);            }        };        Page<Employee> employeePage = employeeJpaSpecificationExecutor.findAll(specification,pageable);        System.out.println("查询的总页数:"+employeePage.getTotalPages());        System.out.println("查询的总记录数:"+employeePage.getTotalElements());        System.out.println("查询的当前第几页:"+(employeePage.getNumber() + 1));        System.out.println("查询的当前页面的集合:"+employeePage.getContent());        System.out.println("查询的当前页面的记录数:"+employeePage.getNumberOfElements());    }}
0 0
原创粉丝点击