mybatis XML configuration file, typeHandler, jdbcType, javaType use

1 Introduction

typeHandler, jdbcType, javaType are used to process the java data types and data types of the database jdbc conversion problem, but at different positions in xml using Note the use of quotation marks.

2. The use of different positions xml

1) do attribute xml tag within the angle brackets

      Must be quoted attribute values

    <typeHandlers>
        <typeHandler handler="org.apache.ibatis.type.EnumTypeHandler"
            javaType="Sex" />
    </typeHandlers>

    If you do not have to quote an error

    2) in the xml sql statement placeholder '# {}' of

You can not use quotation marks

Background java test methods

    private SqlSession sqlSession = null;
    @Before
    public void openSession()
    {
        sqlSession = MyBatisUtil.createSqlSession();
    }
    
    @Test
    public void addStudent()
    {
        int addRows = sqlSession.insert("com.kgc.dao.StudentMapper.addStudent",
            new Student(Short.valueOf("23"), Sex.FEMALE, "张大明"));
        sqlSession.commit();
        System.out.println("新增的行数" + addRows);
    }
    

 

 

Example 1: to typeHandler, jdbcType, javaType quotes

    <insert id="addStudent" parameterType="Student"
        useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" resultType="string" order="BEFORE">
            select uuid()
        </selectKey>
        insert into test_student (id,age, sex, name)
        values (#{id},
        #{age ,jdbcType="TINYINT" ,javaType="short"},
        #{sex, typeHandler="org.apache.ibatis.type.EnumTypeHandler"},
        #{name} )
    </insert>

 

 JUinit prompt does not know "TINYINT" enumerated type

 

 

Example 2: Cancel quotes typeHandler, jdbcType, javaType attribute value

    <insert id="addStudent" parameterType="Student"
        useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" resultType="string" order="BEFORE">
            select uuid()
        </selectKey>
        insert into test_student (id,age, sex, name)
        values (#{id},
        #{age ,jdbcType=TINYINT ,javaType=short},
        #{sex, typeHandler=org.apache.ibatis.type.EnumTypeHandler},
        #{name} )
    </insert>

 Print console, increase the data successfully.

3. Summary

Angle brackets do xml tag attributes, needs to typeHandler, attribute value jdbcType, javaType the quotation marks;

 In xml sql statement placeholder '# {}' of attribute values ​​quoted can not give typeHandler, jdbcType, javaType of.

 

Guess you like

Origin www.cnblogs.com/gocode/p/jdbcType_javaType_typeHandler-use-quotation_marks-in-mybatis_config_xml.html