码云地址:https://gitee.com/klguang/klg-jpahtml
JPA是sun为POJO持久化制定的标准规范,用来操做实体对象,执行CRUD操做,让开发者从繁琐的JDBC和SQL代码中解脱出来。 可是JPA有如下两个缺陷:
1.臃肿的动态条件查询 java
2.众所周知,复杂查询(联接表查询)的效率低
spring-data-jpa和mybatis能够整合在一块儿使用有什么优缺点,这个问答种了解到 spring-data-jpa-extra这个库,让咱们可用更方便的写sql查询。git
klg-jpa,spring-data-jpa 最佳实践,用起来就像开了挂,更多例子klg-j2ee-dataacess-demo 演示github
1. find by attribute是一种较灵活的方式,须要用到jpa生成的metamodel,在快速开发的利器 spring
BaseRepository api:sql
//-------------find by attribute------------------------
public Page<T> findPage(Pageable pageable,AttrExpression...exprs);
public List<T> findList(Sort sort,AttrExpression...exprs);
public List<T> findList(AttrExpression...exprs);
public T getOne(AttrExpression...exprs);
public long count(AttrExpression...exprs);
find by attribute 适合不定条件单表查询,默认是不忽略空值的(null或者""),若是要忽略空值,请用AExpr属性表达式构造器的igEmpty()方法。api
Pageable pageable = new PageRequest(0, 10, new Sort(Direction.DESC, "userId"));
Page<User> userPage = userDAO.findPage(pageable,
AExpr.eq(User_.account, "").igEmpty(),
AExpr.contain(User_.userName, "fd"));
含有开始和结束时间 动态查询的处理mybatis
@ResponseBody
@RequestMapping("/findpage")
public EasyUIPage findpage(
@RequestParam int page,
@RequestParam int rows,
@RequestParam(required=false) Long employeeid,
@RequestParam(required=false) @DateTimeFormat(pattern="yyyy-MM-dd") Date startDate,
@RequestParam(required=false) @DateTimeFormat(pattern="yyyy-MM-dd") Date endDate){
Pageable pageable=new PageRequest(page<1?0:page-1, rows, new Sort(Direction.DESC,"numId"));
_EmployeeName employeeName=employeeid==null?null:MyReflectUtil.createWithSet(_EmployeeName.class, "id", employeeid);
Page<DrugOut> pageData=drugOutService.findPage(pageable,
AExpr.eq(DrugOut_.employeeName, employeeName).igEmpty(),
AExpr.gte(DrugOut_.saledate, startDate).igEmpty(),
AExpr.lte(DrugOut_.saledate, endDate).igEmpty());
return new EasyUIPage(pageData);
}
注意:
1).复杂属性的表达式处理:先判断复杂属性的实体的id是否为空
2).动态查询,参数(required=false)、查询的属性表达式列表(igEmpty)架构
2. 经过解析Repository中的方法名建立查询,是spring-data-jpa的一大特点。符合经典的java三层架构:表现层(UI),业务逻辑层(BLL),数据访问层(DAL) app
这是spring官网的例子:经过EmailAddress和Lastname来查找用户
public interface UserRepository extends Repository<User, Long> {
List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
}
详见spring-data-jpa#Query Creation
3. 页面参数封装到实体,而后运用qbe(query by example)。qbe会自动忽略null值
BaseRepository api:
//--------------qbe(query by example)--------------------
public Page<T> findPage(T example,Pageable pageable);
public List<T> findList(T example,Sort sort);
public List<T> findList(T example);
public T getOne(T example);
public long count(T example);
qbe复杂查询只支持String的模糊查询,大于、小于、between and均不支持。
ExampleMatcher例字,嵌套属性模糊匹配
//User有属性logrole
ExampleMatcher matcher=ExampleMatcher.matching()
.withMatcher("logrole.logRoleName", GenericPropertyMatcher.of(StringMatcher.CONTAINING).ignoreCase());
List<User> users=userDAO.findAll(Example.of(user,matcher));
详见spirng-data-jpa#query-by-example
复杂查询,不管是用java代码进行sql拼接仍是臃肿的Criteria动态查询都是不推荐的。由于这么作一方面是不够优雅,更重要的是难以维护。Mybatis的流行给了咱们不少启发,复杂查询用sql配置文件,是一种灵活且方便维护的方式。
如下两种方式是我推荐你们使用的:
<!-- 指定 BaseRepositoryFactoryBean -->
<jpa:repositories base-package="demo,com.slyak.spring.jpa"
factory-class="klg.common.dataaccess.BaseRepositoryFactoryBean"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" repository-impl-postfix="Impl" />
<!-- 配置 freemarkerSqlTemplates解析相关-->
<bean id="freemarkerSqlTemplates" class="com.slyak.spring.jpa.FreemarkerSqlTemplates">
<property name="suffix" value=".sftl" />
<property name="templateLocation" value="classpath*:/sqls"/>
</bean>
若有疑问下载klg-j2ee-dataacess-demo 演示
本项目依赖spring-data-jpa-extra-2.1.2.RELEASE,但我作了一些改动,而后打成jar包
项目地址:https://github.com/klguang/spring-data-jpa-extra/tree/2.1.2.RELEASE