什么是SpringDataJpa
其实就是Spring对JPA的 封装使用,底层是通过hibernate对JPA的实现来完成的,使我们操作数据库变得更加加单方便,配置相对较多
4.0.0
cn.itsource
aisell
1.0-SNAPSHOT
UTF-8
4.2.5.RELEASE
4.3.8.Final
1.9.0.RELEASE
2.5.0
1.6.1
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-context
${org.springframework.version}
org.springframework
spring-context-support
${org.springframework.version}
org.springframework
spring-tx
${org.springframework.version}
org.springframework
spring-jdbc
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-aop
${org.springframework.version}
org.springframework
spring-test
${org.springframework.version}
test
org.springframework
spring-web
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.apache.commons
commons-io
1.3.2
commons-fileupload
commons-fileupload
1.2.2
com.fasterxml.jackson.core
jackson-core
${com.fasterxml.jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${com.fasterxml.jackson.version}
com.fasterxml.jackson.core
jackson-databind
${com.fasterxml.jackson.version}
org.hibernate
hibernate-core
${org.hibernate.version}
org.hibernate
hibernate-entitymanager
${org.hibernate.version}
org.springframework.data
spring-data-jpa
${spring-data-jpa.version}
com.github.wenhao
jpa-spec
3.1.1
*
*
commons-dbcp
commons-dbcp
1.2.2
mysql
mysql-connector-java
5.1.6
org.apache.commons
commons-lang3
3.5
junit
junit
4.12
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.slf4j
slf4j-api
${org.slf4j.version}
org.slf4j
slf4j-log4j12
${org.slf4j.version}
runtime
log4j
log4j
1.2.14
org.apache.velocity
velocity
1.6
org.apache.shiro
shiro-all
1.4.0
pom
org.apache.shiro
shiro-spring
1.4.0
cn.afterturn
easypoi-base
3.2.0
cn.afterturn
easypoi-web
3.2.0
cn.afterturn
easypoi-annotation
3.2.0
org.hibernate
hibernate-validator
5.2.4.Final
net.coobird
thumbnailator
0.4.6
quartz
quartz
1.5.2
javax.mail
mail
1.4.1
aisell
org.apache.maven.plugins
maven-compiler-plugin
1.8
org.mortbay.jetty
jetty-maven-plugin
8.1.15.v20140411
9966
foo
/
配置完之后就可以开始创建domain了,然后相应的是repository,service,controller,query等包
完成相应的CRUD,然后开始写高级查询功能
/**
* 自定义一个Repository,它是JpaRepository的功能基础上继承增强
* 在上面添加@NoRepositoryBean标注,这样Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口
* @param
* @param
*/
@NoRepositoryBean
public interface BaseRepository extends JpaRepository, JpaSpecificationExecutor {
//根据Query拿到分页对象(分页)
Page findPageByQuery(BaseQuery baseQuery);
//根据Query拿到对应的所有数据(不分页)
List findByQuery(BaseQuery baseQuery);
//根据jpql与对应的参数拿到数据
List findByJpql(String jpql,Object... values);
}
JpaSpecificationExecutor(JPA规则执行者)是JPA2.0提供的Criteria API的使用封装,可以用于动态生成Query来满足我们业务中的各种复杂场景。
Spring Data JPA为我们提供了JpaSpecificationExecutor接口,只要简单实现toPredicate方法就可以实现复杂的查询。
package org.springframework.data.jpa.repository;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
public interface JpaSpecificationExecutor {
T findOne(Specification spec);
List findAll(Specification spec);
Page findAll(Specification spec, Pageable pageable);
List findAll(Specification spec, Sort sort);
long count(Specification spec);
}
但是很难使用,所以我这里使用了一个插件
https://github.com/wenhao/jpa-spec
com.github.wenhao
jpa-spec
3.1.1
*
*
然后就可以愉快的
Specification spec = Specifications.and()
.like("username", "%1%")
。。。。一直在这儿追加高级查询就行了
.build();
可以使用插件之后我们就可以抽一个公共的查询父类出来,以后子类都继承他就行了,里面完成了分页和排序的功能,子类只用写自己的高级查询条件就行了
package cn.itsource.aisell.query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
//抽象方法类名上也要加abstract
public abstract class BaseQuery {
//注意这里使用int类型,包装类以后可能会判断是否为空,int只判断是否为0
private int currentPage = 1;
private int pageSize = 10;
//规范子类必须继承父类的查询的方法
public abstract Specification createSpec();
private Boolean orderByType = true;
private String orderByName;
public Sort createSort(){
if (StringUtils.isNotBlank(orderByName)){
//new Sort方法,要传两个参数
Sort sort = new Sort(orderByType?Sort.Direction.ASC: Sort.Direction.DESC,orderByName);
return sort;
}
return null;
}
public Boolean getOrderByType() {
return orderByType;
}
public void setOrderByType(Boolean orderByType) {
this.orderByType = orderByType;
}
public String getOrderByName() {
return orderByName;
}
public void setOrderByName(String orderByName) {
this.orderByName = orderByName;
}
public int getCurrentPage() {
return currentPage;
}
//只用给我们自己使用的获取当前页的方法,用户的是从1开始的,我们的是从0开始的
public int getJpaPage() {
return currentPage-1;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setPage(int page){
this.currentPage=page;
}
public void setRows(int rows){
this.pageSize=rows;
}
}