javascript
mybatis支持驼峰自动转换sql吗_SpringBoot整合mybatis——配置mybatis驼峰命名规则自动转换...
一、簡述
mybatis駝峰式命名規(guī)則自動轉(zhuǎn)換:
使用前提:數(shù)據(jù)庫表設(shè)計按照規(guī)范“字段名中各單詞使用下劃線"_"劃分”;
使用好處:省去mapper.xml文件中繁瑣編寫表字段列表與表實體類屬性的映射關(guān)系,即resultMap。
示例:
SpringBoot整合mybatis,開啟mybatis駝峰式命名規(guī)則自動轉(zhuǎn)換,通常根據(jù)配置文件不同分為兩種方式。
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文件:
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
注:關(guān)于xml文件,如果刪除或者注釋掉所有內(nèi)容,會報錯:"Valid XML document must hava a root tag",若忽略這個報錯直接運行,程序報錯:
“Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 24; 文件提前結(jié)束。”
3、小結(jié)
開啟mybatis駝峰式命名規(guī)則轉(zhuǎn)換可以省去xml文件中resultMap編寫的麻煩,只需要為resultType指定數(shù)據(jù)庫表對應(yīng)的實體類即可,但是考慮程序的安全性以及映射靈活性,通常開發(fā)中還是將resultMap結(jié)合使用。
總結(jié)
以上是生活随笔為你收集整理的mybatis支持驼峰自动转换sql吗_SpringBoot整合mybatis——配置mybatis驼峰命名规则自动转换...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java float 存储方式_java
- 下一篇: spring更新后 外层事务查不到_再深