com.github.wz2cool.dynamic.mybatis.mapper.provider.DynamicQueryProvider Maven / Gradle / Ivy
package com.github.wz2cool.dynamic.mybatis.mapper.provider;
import com.github.wz2cool.dynamic.BaseFilterDescriptor;
import com.github.wz2cool.dynamic.DynamicQuery;
import com.github.wz2cool.dynamic.BaseSortDescriptor;
import com.github.wz2cool.dynamic.UpdateQuery;
import com.github.wz2cool.dynamic.mybatis.ParamExpression;
import com.github.wz2cool.dynamic.mybatis.QueryHelper;
import com.github.wz2cool.dynamic.mybatis.mapper.constant.MapperConstants;
import com.github.wz2cool.dynamic.mybatis.mapper.helper.DynamicQuerySqlHelper;
import com.github.wz2cool.dynamic.mybatis.mapper.helper.BaseEnhancedMapperTemplate;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import java.util.Map;
/**
* @author Frank
*/
public class DynamicQueryProvider extends BaseEnhancedMapperTemplate {
private static final QueryHelper QUERY_HELPER = new QueryHelper();
public DynamicQueryProvider(Class> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
@Override
protected String tableName(Class> entityClass) {
String viewExpression = QUERY_HELPER.getViewExpression(entityClass);
if (StringUtils.isNoneBlank(viewExpression)) {
return viewExpression;
} else {
return super.tableName(entityClass);
}
}
public String selectCountByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(SqlHelper.selectCount(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
public String selectMaxByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(DynamicQuerySqlHelper.getSelectMax());
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
public String selectMinByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(DynamicQuerySqlHelper.getSelectMin());
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
public String selectSumByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(DynamicQuerySqlHelper.getSelectSum());
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
public String selectAvgByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(DynamicQuerySqlHelper.getSelectAvg());
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
public String deleteByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
public String selectByDynamicQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
setResultType(ms, entityClass);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append("SELECT");
sql.append(String.format("distinct ",
MapperConstants.DYNAMIC_QUERY_PARAMS, MapperConstants.DISTINCT));
//支持查询指定列
sql.append(DynamicQuerySqlHelper.getSelectColumnsClause());
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(DynamicQuerySqlHelper.getWhereClause());
sql.append(DynamicQuerySqlHelper.getSortClause());
return sql.toString();
}
public String selectRowBoundsByDynamicQuery(MappedStatement ms) {
return selectByDynamicQuery(ms);
}
public String updateSelectiveByDynamicQuery(MappedStatement ms) {
return updateByDynamicQuery(ms, true);
}
public String updateByDynamicQuery(MappedStatement ms) {
return updateByDynamicQuery(ms, false);
}
public String updateByUpdateQuery(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getUpdateBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
sql.append(DynamicQuerySqlHelper.getSetClause());
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
private String updateByDynamicQuery(MappedStatement ms, boolean noNull) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(DynamicQuerySqlHelper.getBindFilterParams(ms.getConfiguration().isMapUnderscoreToCamelCase()));
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
sql.append(SqlHelper.updateSetColumns(entityClass, "record", noNull, isNotEmpty()));
sql.append(DynamicQuerySqlHelper.getWhereClause());
return sql.toString();
}
/// region for xml query
public static Map getDynamicQueryParamInternal(
final DynamicQuery dynamicQuery,
final boolean isMapUnderscoreToCamelCase) {
return dynamicQuery.toQueryParamMap(isMapUnderscoreToCamelCase);
}
public static Map getUpdateQueryParamInternal(
final UpdateQuery updateQuery,
final boolean isMapUnderscoreToCamelCase) {
return updateQuery.toQueryParamMap(isMapUnderscoreToCamelCase);
}
// endregion
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy