国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

SpringBoot配置mybatis駝峰命名規則自動轉換的實現

瀏覽:91日期:2023-04-20 17:10:17

一、簡述

mybatis駝峰式命名規則自動轉換:

使用前提:數據庫表設計按照規范“字段名中各單詞使用下劃線'_'劃分”; 使用好處:省去mapper.xml文件中繁瑣編寫表字段列表與表實體類屬性的映射關系,即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駝峰式命名規則自動轉換,通常根據配置文件不同分為兩種方式。

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駝峰命名轉換內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 蕲春县| 萨迦县| 皮山县| 商洛市| 山西省| 武胜县| 竹溪县| 绍兴县| 旌德县| 米泉市| 古蔺县| 阿瓦提县| 海淀区| 云阳县| 得荣县| 平昌县| 达州市| 玉山县| 金华市| 太原市| 南丰县| 陵川县| 台江县| 东乡| 灌云县| 天全县| 简阳市| 天等县| 普洱| 宕昌县| 河间市| 农安县| 阜宁县| 宜州市| 文登市| 满洲里市| 贺州市| 宁阳县| 水城县| 屏南县| 滦平县|