java xml type,mybatis的XML配置文件中,typeHandler、jdbcType、javaType的使用

1.前言

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

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

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

必须给属性值加引号

如果没有引号则要报错

ea478d850153ba14064b8a5b8c454bfa.png

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

不能使用引号

后台java测试方法

private SqlSession sqlSession = null;

@Beforepublic voidopenSession()

{

sqlSession=MyBatisUtil.createSqlSession();

}

@Testpublic voidaddStudent()

{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加上引号

select uuid()insert into test_student (id,age, sex, name)

values (#{id},

#{age ,jdbcType="TINYINT" ,javaType="short"},

#{sex, typeHandler="org.apache.ibatis.type.EnumTypeHandler"},

#{name} )

abada5707f78f20c0549b3c4abdecae9.png

JUinit提示不知道"TINYINT"的枚举类型

014bd56044a11abbcd6ec835ee171c36.png

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

select uuid()insert into test_student (id,age, sex, name)

values (#{id},

#{age ,jdbcType=TINYINT ,javaType=short},

#{sex, typeHandler=org.apache.ibatis.type.EnumTypeHandler},

#{name} )

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

835331f8bee6c810f0c4e6323827fda8.png

3.总结

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

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

原文出处:https://www.cnblogs.com/gocode/p/jdbcType_javaType_typeHandler-use-quotation_marks-in-mybatis_config_xml.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值