一、简述
mybatis驼峰式命名规则自动转换:
使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”;
使用好处:省去mapper.xml文件中繁琐编写表字段列表与表实体类属性的映射关系,即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文件:
复制代码
“Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 24; 文件提前结束。”
3、小结
开启mybatis驼峰式命名规则转换可以省去xml文件中resultMap编写的麻烦,只需要为resultType指定数据库表对应的实体类即可,但是考虑程序的安全性以及映射灵活性,通常开发中还是将resultMap结合使用。