Skip to content

Commit

Permalink
此版本做一些优化调整
Browse files Browse the repository at this point in the history
  • Loading branch information
stormning committed Nov 3, 2016
1 parent 5d2e7e3 commit 59b4637
Show file tree
Hide file tree
Showing 29 changed files with 1,228 additions and 1,172 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jpa-extra</artifactId>
<version>1.2.0.RELEASE</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>spring-data-jpa dynamic query support like mybatis and much easier than mybatis.</description>
Expand Down
149 changes: 75 additions & 74 deletions src/main/java/com/slyak/spring/jpa/AbstractTemplateBasedJpaQuery.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.slyak.spring.jpa;

import org.springframework.data.jpa.repository.query.*;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.util.ReflectionUtils;
Expand All @@ -17,77 +16,79 @@
*/
public class AbstractTemplateBasedJpaQuery extends AbstractJpaQuery {

private JpaQueryMethod method;

private QueryTemplate query;

private QueryTemplate countQuery;

/**
* Creates a new {@link AbstractJpaQuery} from the given {@link JpaQueryMethod}.
*
* @param method
* @param em
*/
public AbstractTemplateBasedJpaQuery(JpaQueryMethod method, EntityManager em, QueryTemplateContext context) {
super(method, em);
this.method = method;
this.query = context.lookup(getQueryMethod().getNamedQueryName());
String cq = executeMethod("getCountQuery");
this.countQuery = cq == null ? null : context.lookup(cq);
}

@Override
protected Query doCreateQuery(Object[] values) {
ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), values);
String sortedQueryString = QueryUtils.applySorting(query.getQueryString(), accessor.getSort(), QueryUtils.detectAlias(query.getQueryString()));

Query query = createJpaQuery(sortedQueryString);

return createBinder(values).bindAndPrepare(query);
}

@Override
protected Query doCreateCountQuery(Object[] values) {
String queryString = countQuery.getQueryString();
EntityManager em = getEntityManager();
boolean isNativeQuery = executeMethod("nativeQuery");
return createBinder(values).bind(isNativeQuery ? em.createNativeQuery(queryString) : em.createQuery(queryString, Long.class));
}

public Query createJpaQuery(String queryString) {
return getEntityManager().createQuery(queryString);
}

@Override
protected ParameterBinder createBinder(Object[] values) {
return new TemplateQueryParameterBinder(getQueryMethod().getParameters(), values);
}

@SuppressWarnings("unchecked")
private <T> T executeMethod(String methodName) {
Method countQueryMethod = ReflectionUtils.findMethod(JpaQueryMethod.class, methodName);
ReflectionUtils.makeAccessible(countQueryMethod);
return (T) ReflectionUtils.invokeMethod(countQueryMethod, method);
}

private static class TemplateQueryParameterBinder extends ParameterBinder {

private Object[] values;

private JpaParameters parameters;

/**
* Creates a new {@link ParameterBinder}.
*
* @param parameters must not be {@literal null}.
* @param values must not be {@literal null}.
*/
public TemplateQueryParameterBinder(JpaParameters parameters, Object[] values) {
super(parameters, values);
this.values = values;
this.parameters = parameters;
}

}
private JpaQueryMethod method;

private QueryTemplate query;

private QueryTemplate countQuery;

/**
* Creates a new {@link AbstractJpaQuery} from the given {@link JpaQueryMethod}.
*
* @param method
* @param em
*/
public AbstractTemplateBasedJpaQuery(JpaQueryMethod method, EntityManager em, QueryTemplateContext context) {
super(method, em);
this.method = method;
this.query = context.lookup(getQueryMethod().getNamedQueryName());
String cq = executeMethod("getCountQuery");
this.countQuery = cq == null ? null : context.lookup(cq);
}

@Override
protected Query doCreateQuery(Object[] values) {
ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), values);
String sortedQueryString = QueryUtils.applySorting(query.getQueryString(), accessor.getSort(),
QueryUtils.detectAlias(query.getQueryString()));

Query query = createJpaQuery(sortedQueryString);

return createBinder(values).bindAndPrepare(query);
}

@Override
protected Query doCreateCountQuery(Object[] values) {
String queryString = countQuery.getQueryString();
EntityManager em = getEntityManager();
boolean isNativeQuery = executeMethod("nativeQuery");
return createBinder(values)
.bind(isNativeQuery ? em.createNativeQuery(queryString) : em.createQuery(queryString, Long.class));
}

public Query createJpaQuery(String queryString) {
return getEntityManager().createQuery(queryString);
}

@Override
protected ParameterBinder createBinder(Object[] values) {
return new TemplateQueryParameterBinder(getQueryMethod().getParameters(), values);
}

@SuppressWarnings("unchecked")
private <T> T executeMethod(String methodName) {
Method countQueryMethod = ReflectionUtils.findMethod(JpaQueryMethod.class, methodName);
ReflectionUtils.makeAccessible(countQueryMethod);
return (T) ReflectionUtils.invokeMethod(countQueryMethod, method);
}

private static class TemplateQueryParameterBinder extends ParameterBinder {

private Object[] values;

private JpaParameters parameters;

/**
* Creates a new {@link ParameterBinder}.
*
* @param parameters must not be {@literal null}.
* @param values must not be {@literal null}.
*/
public TemplateQueryParameterBinder(JpaParameters parameters, Object[] values) {
super(parameters, values);
this.values = values;
this.parameters = parameters;
}

}
}
Loading

0 comments on commit 59b4637

Please sign in to comment.