SpringData JPA 入门

1. 概述

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

Spring Data 包含多个子项目:

  • Commons - 提供共享的基础框架,适合各个子项目使用,支持跨数据库持久化

  • 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、高级队列和高级数据类型

  • JPA - 简化创建 JPA 数据访问层和跨存储的持久层功能

  • Mapping - 基于 Grails 的提供对象映射框架,支持不同的数据库

  • Examples - 示例程序、文档和图数据库

  • Guidance - 高级文档
    其实Spring Data项目旨在提供一种通用的编码模式,统一持久化操作的封装,简化数据库操作。

2.SpringData JPA 快速入门

  • 配置 SpringData Jpa 的依赖

  4.0.0
  com.xingxue.s2sp
  s2sp
  war
  1.0-SNAPSHOT

  
    
      org.hibernate
      hibernate-core
      4.3.5.Final
    
    
    
      org.hibernate
      hibernate-entitymanager
      4.3.5.Final
    
    
    
      org.springframework
      spring-context
      4.0.4.RELEASE
    
    
      org.springframework
      spring-aop
      4.0.4.RELEASE
    
    
      org.springframework
      spring-aspects
      4.0.4.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.0.4.RELEASE
    
    
    
      org.springframework
      spring-orm
      4.0.4.RELEASE
    

    
    
      org.springframework
      spring-web
      4.0.4.RELEASE
    


    
    
      org.apache.struts
      struts2-core
      2.5.14.1
    

    
    
      org.apache.struts
      struts2-spring-plugin
      2.5.14.1
    


    
    
      org.springframework.data
      spring-data-jpa
      1.5.3.RELEASE
    



    
    
      commons-dbcp
      commons-dbcp
      1.4
    

    
      com.oracle
      ojdbc14
      10.2.0
      runtime
    

    
    
      javax.servlet
      javax.servlet-api
      3.0.1
      provided
    
    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.2.1
      provided
    
  

  

    
      

        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
        
          /
          80
        
      
    



    
      
        src/main/java
        
          **/*.xml
        
        false
      
      
        src/main/resources
        
          **/*.xml
          **/*.properties
        
        false
      
    
  





  • Spring 配置文件



    

    


    
        org.hibernate.dialect.Oracle10gDialect
        true
        true
    

    
    

    
    
    


    
    

    
    
    
    

    
    



  • 接口的定义
public interface IUserDao extends Repository {

3.Repository 接口规范

  • 接口结构图


    SpringData JPA 入门_第1张图片
    图片.png
  • Repository(父接口):
    该接口是一个标识接口(没有任何常量和方法),作用打个标记,方便SpringData Jpa 功能识别。
    Object:操作的实体
    Serializable:实体的主键类型
    SpringData 提出了约定优于编码的编程思想,所有定义了大量自定义方法的规范。
    findBy、getBy、Like、And 关键字,是帮助安装规则去查询,这样的关键子如表所示:

关键字 示例 同功能 JPQL
And findByLastnameAndFirstname where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEqual where x.firstname = 1?
Between findByStartDateBetween where x.startDate between 1? and ?2
LessThan findByAgeLessThan where x.age < ?1
LessThanEqual findByAgeLessThanEqual where x.age = ?1
GreaterThan findByAgeGreaterThan where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual where x.age >= ?1
After findByStartDateAfter where x.startDate > ?1
Before findByStartDateBefore where x.startDate < ?1
IsNull findByAgeIsNull where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull where x.age not null
Like findByFirstnameLike where x.firstname like ?1
NotLike findByFirstnameNotLike where x.firstname not like ?1
StartingWith findByFirstnameStartingWith where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc where x.age = ?1 order by x.lastname desc
Not findByLastnameNot where x.lastname <> ?1
In findByAgeIn(Collection ages) where x.age in ?1
NotIn findByAgeNotIn(Collection age) where x.age not in ?1
True findByActiveTrue() where x.active = true
False findByActiveFalse() where x.active = false
IgnoreCase findByFirstnameIgnoreCase where UPPER(x.firstame) = UPPER(?1)
  • CrudRepository
    抽取了10个最基本常用方法(find 查询、delete 删除、save 添加操作),也多写个 crud 字母,可以获得10个方法。何乐而不为!

  • PagingAndSortingRepository 带分页和排序的功能
    三个分页排序功能的类型:

    • Page:分页查询出来的封装模型
    • Pageable:分页使用的页码和大小 当前页码和页面大小
    • Sort:做排序的
      该接口不建议使用,一般在定义方法的时候,把pageable 类对象和 Sort 类型对象传入进去就行了。
  • JpaRepository
    略过,添加两个操作 JPA 的方法。

4.支持 @Query 自定义 JPQL 查询 略过...

你可能感兴趣的:(SpringData JPA 入门)