SpringBoot配置mybatis駝峰命名規(guī)則自動(dòng)轉(zhuǎn)換的實(shí)現(xiàn)
一、簡(jiǎn)述
mybatis駝峰式命名規(guī)則自動(dòng)轉(zhuǎn)換:
使用前提:數(shù)據(jù)庫(kù)表設(shè)計(jì)按照規(guī)范“字段名中各單詞使用下劃線'_'劃分”; 使用好處:省去mapper.xml文件中繁瑣編寫表字段列表與表實(shí)體類屬性的映射關(guān)系,即resultMap。示例:
<resultMap 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駝峰式命名規(guī)則自動(dòng)轉(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文件:
<?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> <!--開啟駝峰命名規(guī)則自動(dòng)轉(zhuǎn)換--> <settings> <setting name='mapUnderscoreToCamelCase' value='true' /> </settings></configuration>
注:關(guān)于xml文件,如果刪除或者注釋掉所有內(nèi)容,會(huì)報(bào)錯(cuò):'Valid XML document must hava a root tag',若忽略這個(gè)報(bào)錯(cuò)直接運(yùn)行,程序報(bào)錯(cuò):
“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ù)庫(kù)表對(duì)應(yīng)的實(shí)體類即可,但是考慮程序的安全性以及映射靈活性,通常開發(fā)中還是將resultMap結(jié)合使用。
到此這篇關(guān)于SpringBoot配置mybatis駝峰命名規(guī)則自動(dòng)轉(zhuǎn)換的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot mybatis駝峰命名轉(zhuǎn)換內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)3. XML在語(yǔ)音合成中的應(yīng)用4. 基于PHP做個(gè)圖片防盜鏈5. jscript與vbscript 操作XML元素屬性的代碼6. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)7. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解8. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字9. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)10. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)
