一、简述
mybatis驼峰式命名规则自动转换:
示例:
<resultMap id ="UserInfoMap" type="com.example.mybaitsxml.dao.entity.User"> <result column="name_" property="name"/> <result column="sex" property="sex"/> <result column="age" property="age"/> <result column="class_no" property="classNo"/> </resultMap>
SpringBoot整合mybatis,开启mybatis驼峰式命名规则自动转换,通常根据配置文件不同分为两种方式。
1、方式一
直接application.yml文件中配置开启
#mybatis配置 mybatis: typeAliasesPackage: com.example.mybaitsxml.dao.entity mapperLocations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: true
2、方式二
mybatis-config.xml文件中配置开启,application.yml文件指定配置文件。
application.yml文件:
#mybatis配置 mybatis: typeAliasesPackage: com.example.mybaitsxml.dao.entity mapperLocations: classpath:mapper/*.xml configLocation: classpath:/mybatis-config.xml
mybatis-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--开启驼峰命名规则自动转换--> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
注:关于xml文件,如果删除或者注释掉所有内容,会报错:"Valid XML document must hava a root tag",若忽略这个报错直接运行,程序报错:
“Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 24; 文件提前结束。”
3、小结
开启mybatis驼峰式命名规则转换可以省去xml文件中resultMap编写的麻烦,只需要为resultType指定数据库表对应的实体类即可,但是考虑程序的安全性以及映射灵活性,通常开发中还是将resultMap结合使用。
到此这篇关于SpringBoot配置mybatis驼峰命名规则自动转换的实现的文章就介绍到这了,更多相关SpringBoot mybatis驼峰命名转换内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。