All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.zhuang.data.mybatis.util.MappedStatementUtils Maven / Gradle / Ivy
package com.zhuang.data.mybatis.util;
import com.zhuang.data.enums.DbDialect;
import com.zhuang.data.orm.enums.PlaceHolderType;
import com.zhuang.data.orm.enums.SqlType;
import com.zhuang.data.orm.mapping.ColumnMapping;
import com.zhuang.data.orm.mapping.TableMapping;
import com.zhuang.data.orm.util.SqlBuilderUtils;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.session.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
public class MappedStatementUtils {
private static Logger logger = LoggerFactory.getLogger(MappedStatementUtils.class);
public static String getMappedStatementId(Configuration configuration, DbDialect dbDialect, SqlType sqlType, Class entityType, Class> parameterType) {
return getMappedStatementId(configuration, dbDialect, sqlType, entityType, parameterType, null);
}
public static String getMappedStatementId(Configuration configuration, DbDialect dbDialect, SqlType sqlType, Class entityType, Class> parameterType, String[] propertyNames) {
SqlCommandType sqlCommandType = SqlCommandTypeUtils.getBySqlType(sqlType);
String mappedStatementId = entityType.getName() + "_" + sqlType + "_" + parameterType.getName();
if (hasPropertyNames(propertyNames)) {
mappedStatementId = mappedStatementId + "_" + String.join(".", propertyNames);
}
if (configuration.hasStatement(mappedStatementId)) return mappedStatementId;
synchronized (MappedStatementUtils.class) {
if (configuration.hasStatement(mappedStatementId)) return mappedStatementId;
TableMapping tableMapping = new TableMapping(entityType);
if (hasPropertyNames(propertyNames)) {
if (sqlCommandType == SqlCommandType.UPDATE) {
tableMapping.filterColumns(propertyNames);
} else if (sqlCommandType == SqlCommandType.SELECT) {
for (ColumnMapping columnMapping : tableMapping.getColumns()) {
if (Arrays.stream(propertyNames).anyMatch(c -> c.equals(columnMapping.getPropertyName()))) {
columnMapping.setIsId(true);
} else {
columnMapping.setIsId(false);
}
}
}
}
String sql = SqlBuilderUtils.getSql(dbDialect, tableMapping, PlaceHolderType.NumberSign, sqlType);
logger.debug("Sql:" + sql);
Class> resultType = sqlType == SqlType.SELECT_COUNT ? Integer.class : entityType;
configuration.addMappedStatement(createMappedStatement(configuration, mappedStatementId, sql, sqlCommandType, parameterType, resultType));
}
return mappedStatementId;
}
public static MappedStatement createMappedStatement(Configuration configuration, String mappedStatementId, String sql, SqlCommandType sqlCommandType, Class> parameterType, Class> resultType) {
SqlSource sqlSource = configuration.getDefaultScriptingLanuageInstance().createSqlSource(configuration, sql, parameterType);
MappedStatement mappedStatement = new MappedStatement.Builder(configuration, mappedStatementId, sqlSource, sqlCommandType)
.resultMaps(new ArrayList() {{
add(new ResultMap.Builder(configuration, "defaultResultMap", resultType, new ArrayList(0)).build());
}}).build();
return mappedStatement;
}
private static boolean hasPropertyNames(String[] propertyNames) {
return propertyNames != null && propertyNames.length > 0;
}
}