Search in sources :

Example 21 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithBeforeAutoKey.

public static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<ResultMapping>()).build();
    MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config, "SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT).keyProperty("id").resultMaps(new ArrayList<ResultMap>() {

        {
            add(rm);
        }
    }).build();
    config.addMappedStatement(kms);
    MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.class)).build());
            add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
            add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
        }
    }).build()).cache(authorCache).keyGenerator(new SelectKeyGenerator(kms, true)).keyProperty("id").build();
    return ms;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) ArrayList(java.util.ArrayList) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) Section(org.apache.ibatis.domain.blog.Section) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 22 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class XmlConfigBuilderTest method registerJavaTypeInitializingTypeHandler.

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

Example 23 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class AutodiscoverTest method testTypeHandler.

@Test
public 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.Test)

Aggregations

TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)23 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)19 MappedStatement (org.apache.ibatis.mapping.MappedStatement)17 ArrayList (java.util.ArrayList)14 ResultMap (org.apache.ibatis.mapping.ResultMap)11 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)9 Section (org.apache.ibatis.domain.blog.Section)8 ParameterMap (org.apache.ibatis.mapping.ParameterMap)8 Author (org.apache.ibatis.domain.blog.Author)7 ResultMapping (org.apache.ibatis.mapping.ResultMapping)7 ResultFlag (org.apache.ibatis.mapping.ResultFlag)6 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)6 SqlSource (org.apache.ibatis.mapping.SqlSource)5 Blog (org.apache.ibatis.domain.blog.Blog)4 Date (java.util.Date)3 Comment (org.apache.ibatis.domain.blog.Comment)3 Post (org.apache.ibatis.domain.blog.Post)3 Tag (org.apache.ibatis.domain.blog.Tag)3 Configuration (org.apache.ibatis.session.Configuration)3 HashMap (java.util.HashMap)2