zoukankan      html  css  js  c++  java
  • mybatis的XML配置文件中,typeHandler、jdbcType、javaType的使用

    1.前言

    typeHandler、jdbcType、javaType都是用来处理java数据类型和jdbc数据库数据类型的转换问题,但在xml的不同位置使用需要注意引号使用问题。

    2.在xml的不同位置的使用方式

    1)在xml的尖括号标签内做属性

          必须给属性值加引号

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

        如果没有引号则要报错

        2) 在xml的sql语句占位'#{   }'内

    不能使用引号

    后台java测试方法

        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);
        }
        

    示例1:给typeHandler、jdbcType、javaType加上引号

        <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提示不知道"TINYINT"的枚举类型

    示例2:取消typeHandler、jdbcType、javaType属性值的引号

        <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>

     控制台打印,增加数据成功。

    3.总结

    在xml的尖括号标签内做属性,需要给typeHandler、jdbcType、javaType的属性值加引号;

     在xml的sql语句占位'#{   }'内 ,不能给typeHandler、jdbcType、javaType的属性值加引号。

  • 相关阅读:
    Matlab随笔之三维图形绘制
    Matlab随笔之模拟退火算法
    Matlab随笔之矩阵入门知识
    Matlab随笔之求解线性方程
    Matlab随笔之分段线性函数化为线性规划
    Matlab随笔之指派问题的整数规划
    Matlab随笔之线性规划
    Android单位转换 (px、dp、sp之间的转换工具类)
    Android禁止输入表情符号
    设计模式之策略模式
  • 原文地址:https://www.cnblogs.com/gocode/p/jdbcType_javaType_typeHandler-use-quotation_marks-in-mybatis_config_xml.html
Copyright © 2011-2022 走看看