use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.
the class XmlConfigBuilderTest method registerJavaTypeInitializingTypeHandler.
@Test
void registerJavaTypeInitializingTypeHandler() {
final String MAPPER_CONFIG = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "<!DOCTYPE configuration PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-config.dtd\">\n" + "<configuration>\n" + " <typeHandlers>\n" + " <typeHandler javaType=\"org.apache.ibatis.builder.XmlConfigBuilderTest$MyEnum\"\n" + " handler=\"org.apache.ibatis.builder.XmlConfigBuilderTest$EnumOrderTypeHandler\"/>\n" + " </typeHandlers>\n" + "</configuration>\n";
XMLConfigBuilder builder = new XMLConfigBuilder(new StringReader(MAPPER_CONFIG));
builder.parse();
TypeHandlerRegistry typeHandlerRegistry = builder.getConfiguration().getTypeHandlerRegistry();
TypeHandler<MyEnum> typeHandler = typeHandlerRegistry.getTypeHandler(MyEnum.class);
assertTrue(typeHandler instanceof EnumOrderTypeHandler);
assertArrayEquals(MyEnum.values(), ((EnumOrderTypeHandler<MyEnum>) typeHandler).constants);
}
use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.
the class AutodiscoverTest method testTypeHandler.
@Test
void testTypeHandler() {
TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
assertTrue(typeHandlerRegistry.hasTypeHandler(BigInteger.class));
}
use of org.apache.ibatis.type.TypeHandlerRegistry in project dq-easy-cloud by dq-open-cloud.
the class EcSqlParserUtil method handleSql.
/**
* 处理拦截的sql,主要包括格式化去掉空格,将传入的参数填值进去
*
* @param interceptSql
* @param mappedStatement
* @param boundSql
* @return String
*/
public static String handleSql(String interceptSql, MappedStatement mappedStatement, BoundSql boundSql) {
String sql = interceptSql.replaceAll("[\\s]+", " ").trim();
Configuration configuration = mappedStatement.getConfiguration();
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
try {
if (parameterObject != null && parameterMappings != null && !parameterMappings.isEmpty()) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
Object obj = metaObject.getValue(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
}
}
}
}
} catch (Exception e) {
throw new RuntimeException("Parse sql exception, " + sql, e);
}
return sql;
}
Aggregations