Search in sources :

Example 31 with TypeHandlerRegistry

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);
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) StringReader(java.io.StringReader) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) Test(org.junit.jupiter.api.Test)

Example 32 with TypeHandlerRegistry

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));
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) BigInteger(java.math.BigInteger) Test(org.junit.jupiter.api.Test)

Example 33 with TypeHandlerRegistry

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;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) Configuration(org.apache.ibatis.session.Configuration) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Aggregations

TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)33 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)22 ArrayList (java.util.ArrayList)18 MappedStatement (org.apache.ibatis.mapping.MappedStatement)16 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)15 ResultMap (org.apache.ibatis.mapping.ResultMap)12 ResultMapping (org.apache.ibatis.mapping.ResultMapping)10 Configuration (org.apache.ibatis.session.Configuration)9 Section (org.apache.ibatis.domain.blog.Section)8 ParameterMap (org.apache.ibatis.mapping.ParameterMap)8 Author (org.apache.ibatis.domain.blog.Author)7 MetaObject (org.apache.ibatis.reflection.MetaObject)7 ResultFlag (org.apache.ibatis.mapping.ResultFlag)6 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)6 SqlSource (org.apache.ibatis.mapping.SqlSource)5 HashMap (java.util.HashMap)4 Blog (org.apache.ibatis.domain.blog.Blog)4 Test (org.junit.jupiter.api.Test)4 Date (java.util.Date)3 Comment (org.apache.ibatis.domain.blog.Comment)3