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.
org.apache.ibatis.builder.annotation.MapperAnnotationBuilder Maven / Gradle / Ivy
package org.apache.ibatis.builder.annotation;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.binding.BindingException;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.JdbcType;
import java.io.IOException;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
public class MapperAnnotationBuilder {
private Configuration configuration;
private MapperBuilderAssistant assistant;
private Class type;
private HashMap sqlFragments;
public MapperAnnotationBuilder(Configuration configuration, Class type) {
String resource = type.getName().replace('.', '/') + ".java (best guess)";
this.assistant = new MapperBuilderAssistant(configuration, resource);
this.configuration = configuration;
this.type = type;
this.sqlFragments = new HashMap();
}
public void parse() {
String resource = type.toString();
if (!configuration.isResourceLoaded(resource)) {
configuration.addLoadedResource(resource);
loadXmlResource();
assistant.setCurrentNamespace(type.getName());
parseCache();
parseCacheRef();
Method[] methods = type.getMethods();
for (Method method : methods) {
parseResultsAndConstructorArgs(method);
parseStatement(method);
}
}
}
private void loadXmlResource() {
String xmlResource = type.getName().replace('.', '/') + ".xml";
Reader xmlReader = null;
try {
xmlReader = Resources.getResourceAsReader(type.getClassLoader(), xmlResource);
} catch (IOException e) {
// ignore, resource is not required
}
if (xmlReader != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(xmlReader, assistant.getConfiguration(), xmlResource, sqlFragments, type.getName());
xmlParser.parse();
}
}
private void parseCache() {
CacheNamespace cacheDomain = (CacheNamespace) type.getAnnotation(CacheNamespace.class);
if (cacheDomain != null) {
assistant.useNewCache(cacheDomain.implementation(), cacheDomain.eviction(), cacheDomain.flushInterval(), cacheDomain.size(), !cacheDomain.readWrite(), null);
}
}
private void parseCacheRef() {
CacheNamespaceRef cacheDomainRef = (CacheNamespaceRef) type.getAnnotation(CacheNamespaceRef.class);
if (cacheDomainRef != null) {
assistant.useCacheRef(cacheDomainRef.value().getName());
}
}
private void parseResultsAndConstructorArgs(Method method) {
Class returnType = getReturnType(method);
if (returnType != null) {
ConstructorArgs args = method.getAnnotation(ConstructorArgs.class);
Results results = method.getAnnotation(Results.class);
TypeDiscriminator typeDiscriminator = method.getAnnotation(TypeDiscriminator.class);
String resultMapId = generateResultMapName(method);
applyResultMap(resultMapId, returnType, argsIf(args), resultsIf(results), typeDiscriminator);
}
}
private String generateResultMapName(Method method) {
StringBuilder suffix = new StringBuilder();
for (Class c : method.getParameterTypes()) {
suffix.append("-");
suffix.append(c.getSimpleName());
}
if (suffix.length() < 1) {
suffix.append("-void");
}
return type.getName() + "." + method.getName() + suffix;
}
private void applyResultMap(String resultMapId, Class returnType, Arg[] args, Result[] results, TypeDiscriminator discriminator) {
List resultMappings = new ArrayList();
applyConstructorArgs(args, returnType, resultMappings);
applyResults(results, returnType, resultMappings);
Discriminator disc = applyDiscriminator(resultMapId, returnType, discriminator);
assistant.addResultMap(resultMapId, returnType, null, disc, resultMappings);
createDiscriminatorResultMaps(resultMapId, returnType, discriminator);
}
private void createDiscriminatorResultMaps(String resultMapId, Class resultType, TypeDiscriminator discriminator) {
if (discriminator != null) {
for (Case c : discriminator.cases()) {
String value = c.value();
Class type = c.type();
String caseResultMapId = resultMapId + "-" + value;
List resultMappings = new ArrayList();
for (Result result : c.results()) {
List flags = new ArrayList();
if (result.id()) {
flags.add(ResultFlag.ID);
}
ResultMapping resultMapping = assistant.buildResultMapping(
resultType,
result.property(),
result.column(),
result.javaType() == void.class ? null : result.javaType(),
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
hasNestedSelect(result) ? nestedSelectId(result) : null,
null,
result.typeHandler() == void.class ? null : result.typeHandler(),
flags);
resultMappings.add(resultMapping);
}
assistant.addResultMap(caseResultMapId, type, resultMapId, null, resultMappings);
}
}
}
private Discriminator applyDiscriminator(String resultMapId, Class resultType, TypeDiscriminator discriminator) {
if (discriminator != null) {
String column = discriminator.column();
Class javaType = discriminator.javaType() == void.class ? String.class : discriminator.javaType();
JdbcType jdbcType = discriminator.jdbcType() == JdbcType.UNDEFINED ? null : discriminator.jdbcType();
Class typeHandler = discriminator.typeHandler() == void.class ? null : discriminator.typeHandler();
Case[] cases = discriminator.cases();
Map discriminatorMap = new HashMap();
for (Case c : cases) {
String value = c.value();
String caseResultMapId = resultMapId + "-" + value;
discriminatorMap.put(value, caseResultMapId);
}
return assistant.buildDiscriminator(resultType, column, javaType, jdbcType, typeHandler, discriminatorMap);
}
return null;
}
private void parseStatement(Method method) {
Configuration configuration = assistant.getConfiguration();
SqlSource sqlSource = getSqlSourceFromAnnotations(method);
if (sqlSource != null) {
Options options = method.getAnnotation(Options.class);
final String mappedStatementId = method.getDeclaringClass().getName() + "." + method.getName();
boolean flushCache = false;
boolean useCache = true;
Integer fetchSize = null;
Integer timeout = null;
StatementType statementType = StatementType.PREPARED;
ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
SqlCommandType sqlCommandType = getSqlCommandType(method);
KeyGenerator keyGenerator = configuration.isUseGeneratedKeys()
&& SqlCommandType.INSERT.equals(sqlCommandType) ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
String keyProperty = "id";
if (options != null) {
flushCache = options.flushCache();
useCache = options.useCache();
fetchSize = options.fetchSize() > -1 ? options.fetchSize() : null;
timeout = options.timeout() > -1 ? options.timeout() : null;
statementType = options.statementType();
resultSetType = options.resultSetType();
keyGenerator = options.useGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
keyProperty = options.keyProperty();
}
assistant.addMappedStatement(
mappedStatementId,
sqlSource,
statementType,
sqlCommandType,
fetchSize,
timeout,
null, // ParameterMapID
getParameterType(method),
generateResultMapName(method), // ResultMapID
getReturnType(method),
resultSetType,
flushCache,
useCache,
keyGenerator,
keyProperty);
}
}
private Class getParameterType(Method method) {
Class parameterType = null;
Class[] parameterTypes = method.getParameterTypes();
for(int i=0; i resultMappings) {
if (results.length > 0) {
for (Result result : results) {
ArrayList flags = new ArrayList();
if (result.id()) flags.add(ResultFlag.ID);
ResultMapping resultMapping = assistant.buildResultMapping(
resultType,
result.property(),
result.column(),
result.javaType() == void.class ? null : result.javaType(),
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
hasNestedSelect(result) ? nestedSelectId(result) : null,
null,
result.typeHandler() == void.class ? null : result.typeHandler(),
flags);
resultMappings.add(resultMapping);
}
}
}
private String nestedSelectId(Result result) {
String nestedSelect = result.one().select();
if (nestedSelect.length() < 1) {
nestedSelect = result.many().select();
}
if (!nestedSelect.contains(".")) {
nestedSelect = type.getName() + "." + nestedSelect;
}
return nestedSelect;
}
private boolean hasNestedSelect(Result result) {
return result.one().select().length() > 0
|| result.many().select().length() > 0;
}
private void applyConstructorArgs(Arg[] args, Class resultType, List resultMappings) {
if (args.length > 0) {
for (Arg arg : args) {
ArrayList flags = new ArrayList();
flags.add(ResultFlag.CONSTRUCTOR);
if (arg.id()) flags.add(ResultFlag.ID);
ResultMapping resultMapping = assistant.buildResultMapping(
resultType,
null,
arg.column(),
arg.javaType() == void.class ? null : arg.javaType(),
arg.jdbcType() == JdbcType.UNDEFINED ? null : arg.jdbcType(),
null,
null,
arg.typeHandler() == void.class ? null : arg.typeHandler(),
flags);
resultMappings.add(resultMapping);
}
}
}
private Result[] resultsIf(Results results) {
return results == null ? new Result[0] : results.value();
}
private Arg[] argsIf(ConstructorArgs args) {
return args == null ? new Arg[0] : args.value();
}
}