com.dexcoder.dal.batis.BatisSqlFactory Maven / Gradle / Ivy
package com.dexcoder.dal.batis;
import java.util.HashMap;
import java.util.Map;
import com.dexcoder.commons.utils.ArrUtils;
import com.dexcoder.commons.utils.StrUtils;
import com.dexcoder.dal.BoundSql;
import com.dexcoder.dal.SqlFactory;
import com.dexcoder.dal.batis.build.Configuration;
import com.dexcoder.dal.batis.build.MappedStatement;
import com.dexcoder.dal.exceptions.JdbcAssistantException;
/**
* Created by liyd on 2015-11-24.
*/
public class BatisSqlFactory implements SqlFactory {
/** 默认参数名 */
private static final String DEFAULT_PARAMETERS_KEY = "parameters";
private Configuration configuration;
public BatisSqlFactory(Configuration configuration) {
this.configuration = configuration;
}
public BoundSql getBoundSql(String refSql, String expectParamKey, Object[] parameters) {
Map params = this.processParameters(expectParamKey, parameters);
MappedStatement mappedStatement = this.configuration.getMappedStatements().get(refSql);
if (mappedStatement == null) {
throw new JdbcAssistantException("自定义sql没有找到,refSql=" + refSql);
}
return mappedStatement.getSqlSource().getBoundSql(params);
}
/**
* 处理转换参数
*
* @param expectParamKey
* @param parameters
* @return
*/
private Map processParameters(String expectParamKey, Object[] parameters) {
if (ArrUtils.isEmpty(parameters)) {
return null;
}
String paramKey = StrUtils.isBlank(expectParamKey) ? DEFAULT_PARAMETERS_KEY : expectParamKey;
Map map = new HashMap();
if (parameters.length == 1) {
map.put(paramKey, parameters[0]);
} else {
map.put(paramKey, parameters);
}
return map;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy