Spring Data一、 学习安排Spring-data、spring-boot、spring-cloud Spring-data是一个spring提供的数据访问层框架。封装若干中数据服务访问能力。如:spring-data-jpa、spring-data-jdbc、spring-data-redis等。 Spring-data-jpa: 是通过JPA标准规范,底层使用Hibernate框架做为实现,开发的关系型数据库访问模块。 Spring-data-jdbc: 就是底层使用Spring-jdbc实现的关系型数据库访问模块。可以使用Mybatis技术作为底层实现的替代产品。 Spring-data-redis:底层使用jedis实现的访问redis数据服务的模块。 上述三个描述的模块是Spring-data中详细讲解的内容。其他的模块非常多,不可能全部讲解。主要作为介绍和配置简单描述。 二、 Spring Data 简介官网: projects.spring.io/spring-data Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies. Spring Data是一个保持底层数据存储特性的,基于Spring编程模型的,数据访问技术。 Spring Data可以简单快速的访问关系型数据库、非关系型数据库、map-reduce框架和基于云的数据服务。且Spring Data的开发有这些数据服务厂商和开发人员参与。 总而言之,这是一个数据访问层框架,可以高效的访问现今绝大多数数据存储服务,可以统一数据访问层技术,让开发和维护更加方便。 三、 Spring Data JPAJava persistence api在spring-data工程中的具体应用。 Spring-data-jpa底层是使用Hibernate实现的。 Spring-data-jpa1.8.x+底层依赖的是Hibernate5.x版本的框架。 课程中使用的是spring-data-jpa1.11.x版本。 底层使用Hibernate实现是因为Hibernate本身就是JPA标准规范的实现框架。Hibernate又是经历了长时间商业项目验证的稳定的数据访问层框架。所以spring-data-jpa选择的是Hibernate作为底层实现。 1 Spring-DATA-JPA框架提供的接口的继承树 Spring-Data-JPA提供了若干的Repository接口,提供很多的关系型数据库访问能力。只要对Repository接口有足够的掌握,不需要写SQL就可以使用数据库的访问操作。 1.1 类图

image.png 1.2 Repository标记接口。可以继承此接口,根据指定的规则,实现数据查询。 1.2.1 方法名称规则查询这种查询也是有局限性的。只能做单表查询,且查询结果一定是实体类型对象或实体类型对象的集合。毕竟是一个InvocationHandler实现的模板代码,难以完成绝对的定制化开发。只能实现最基础的通用的功能。互联网商业项目中,有半数以上的查询是单表查询。 命名规则:findBy(关键字)+属性名称(属性名称的首字母大写)+查询条件(首字母大写) 关键字 | 方法命名 | sql where字句 |
---|
And | findByNameAndPwd | where name= ? and pwd =? | Or | findByNameOrSex | where name= ? or sex=? | Is,Equal | findById,findByIdEquals | where id= ? | Between | findByIdBetween | where id between ? and ? | LessThan | findByIdLessThan | where id < ? | LessThanEqual | findByIdLessThanEquals | where id <= ? | GreaterThan | findByIdGreaterThan | where id > ? | GreaterThanEqual | findByIdGreaterThanEquals | where id > = ? | After | findByIdAfter | where id > ? | Before | findByIdBefore | where id < ? | IsNull | findByNameIsNull | where name is null | isNotNull,NotNull | findByNameNotNull | where name is not null | Like | findByNameLike | where name like ? | NotLike | findByNameNotLike | where name not like ? | StartingWith | findByNameStartingWith | where name like '?%' | EndingWith | findByNameEndingWith | where name like '%?' | Containing | findByNameContaining | where name like '%?%' | OrderBy | findByIdOrderByXDesc | where id=? order by x desc | Not | findByNameNot | where name <> ? | In | findByIdIn(Collection<?> c) | where id in (?) | NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) | TRUE | findByAaaTue | where aaa = true | FALSE | findByAaaFalse | where aaa = false | IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
1.2.2 @Query注解查询使用@Query注解描述方法,可以绕过方法命名规则,通过编写JPQL或SQL来实现数据访问。 JPQL:java persistence 通过Hibernate的HQL演变过来的。他和HQL语法及其相似。JPQL语法查询,可以实现投影查询,可以实现表连接查询,毕竟是通过一种查询语法规则实现的查询逻辑。From Users、 select username from Users、from Users u,Roles r where u.xxx = r.xxx。JPQL语法查询的时候,在JPQL(1.11.14)中,传递参数的时候,使用变量命名的方式,通过’:变量名’来定义语法中的变量,方法的参数表中,通过@Param注解,描述方法参数表中的参数和JPQL中变量的对应关系,注解的属性value代表JPQL中变量的名称。 nativeQuery: 通过SQL语法实现数据查询。效率更高的执行逻辑。因为spring-data-jpa不需要实现JPQL->SQL的语法转换。少了JPQL字符串的解析,SQL字符串的拼接等过程,效率相对更高。 语法如下:JPQL或SQL中的变量占位符顺序对应方法参数表中的参数顺序。 public interface IA{ @Query(value="JPQL") List xxx(); @Query(value="SQL", nativeQuery=true) List yyy();
}
1.2.3 @Query注解实现数据写操作@Query注解是用于描述数据访问语法的,所以可以执行CRUD任意操作。但是JPQL只能执行查询和更新操作,SQL可以执行CRUD任意操作。语法如下:JPQL或SQL中的变量占位符顺序对应方法参数表中的参数顺序。 public interface IA{ @Query(value="JPQL") @Modifying
List xxx(); @Query(value="SQL", nativeQuery=true) @Modifying
List yyy();
}
1.3 CrudRepository 定义了CRUD操作的接口,是Repository的子接口。接口中没有定义独立的更新方法,调用新增数据方法(save)的时候,底层会根据方法参数决定是新增数据还是更新数据。因为底层是Hibernate,Hibernate中有方法saveOrUpdate,而spring-data-jpa中提供的save方法底层使用的就是Hibernate中的saveOrUpdate方法。判断依据,就是数据是否存在,就是数据对象的id是否存在。 1.4 PagingAndSortingRepository 定义了分页和排序查询的接口,是CrudRepository的子接口。 1.5 JpaRepository PagingAndSortingRepository的子接口,提供了对缓存的处理方法。 1.6 JpaSpecificationExecutor 提供了条件查询和分页处理的接口,是一个相对独立的接口。这个接口的使用必须配合Repository接口树中的任意接口。 2 自定义RepositorySpring-data-jpa做开发的时候,一般不会去定义DAO接口的实现类。因为spring-data-jpa会提供一个动态代理对象,动态代理对象的类型是SimpleJpaRepository。 2.1 自定义Repository接口定义一个Repository接口,不需要继承任何接口,定义需要的自定义方法。 2.2 定义Dao接口定义一个Dao接口,这个接口是服务代码调用的。Dao接口继承spring-data-jpa提供的Repository接口和自定义的Repository接口。 2.3 定义Dao接口实现提供一个接口实现类,这个实现类只需要实现自定义Repository接口,但是其类名必须和Dao接口相关,命名规则为"Dao接口名Impl",如:Dao接口为UserDao,实现类命名为UserDaoImpl,实现类实现接口Repository。 符合上述要求的代码spring-data-jpa会自动为Dao创建一个动态代理对象,这个动态代理对象会提供spring-data-jpa定义的Repository相关代码实现,并使用自定义的接口实现类提供自定义Repository接口的方法实现。 类图如下:

image.png 3 Spring-data-jpa正向工程建表在默认情况下(spring-data-jpa 1.11.x),spring-data-jpa使用正向工程创建的数据库表格,是有默认配置的,其建表语句如下: create table tb_users (
userid integer not null auto_increment,
userage integer,
username varchar(255),
primary key (userid)
) engine=MyISAM
Spring-data-jpa默认情况下,创建的MySQL数据库表格是MyISAM引擎的表格。这种表格,适合查询,不支持事务,不支持外键约束。 但是,因为其不支持事务,所有有脏数据出现的可能。 可以在配置LocalContainerEntityManagerFactoryBean的时候,增加配置信息,约束建表的引擎。 <!-- Spring整合JPA 配置EntityManagerFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- hibernate相关的属性的注入 -->
<!-- 配置数据库类型 -->
<property name="database" value="MYSQL"/>
<!-- 正向工程 自动创建表 -->
<property name="generateDdl" value="true"/>
<!-- 显示执行的SQL -->
<property name="showSql" value="true"/>
<!-- 提供数据库特性配置。和数据库的独特功能紧密相关。这个配置是使用Hibernate提供的Dialect来配置的。 -->
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>
</property>
<!-- 扫描实体的包 -->
<property name="packagesToScan">
<list>
<value>com.bjsxt.pojo</value>
</list>
</property>
</bean>
修改配置后,建表语句如下:create table tb_users (
userid integer not null auto_increment,
userage integer,
username varchar(255),
primary key (userid)
) engine=InnoDB
是否需要配置数据库特性,由具体业务决定。一般来说,商业项目中,不可能使用正向工程建表。也就是说,配置应该是<property name="generateDdl" value="false"/> 4 关联操作关联操作在spring-data-jpa中主要通过JPA注解来实现。如:@OneToOne、@OneToMany、@ManyToOne、@ManyToMany、@JoinColumn、@JoinTable等。 详见代码。 四、 Spring Data Redisspring-data框架中的每个子模块其版本未必一致,毕竟对应不同数据服务的访问层框架,更新时间和周期是不同的。在本案例中,使用的spring-data-redis版本为1.8.14。 spring-data-redis框架的执行需要jackson组件的辅助,建议导入jackson版本为2.7+(对应当前环境中的spring-data-redis版本)。
|