SpringBoot配置mybatis驼峰命名规则自动转换的实现
作者:马非白即黑 时间:2023-07-26 17:47:43
一、简述
mybatis驼峰式命名规则自动转换:
使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”;
使用好处:省去mapper.xml文件中繁琐编写表字段列表与表实体类属性的映射关系,即resultMap。
示例:
<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结合使用。
来源:https://www.cnblogs.com/gavincoder/p/10140562.html
标签:SpringBoot,mybatis,驼峰命名,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何自定义hibernate validation注解示例代码
2021-08-31 12:05:51
简单学习C#中的泛型方法使用
2022-11-06 19:37:27
Android 消息分发使用EventBus的实例详解
2022-12-23 06:28:28
DevExpress实现GridControl根据列选中一行
2021-08-16 05:23:19
Android下拉刷新官方版
2021-11-25 06:52:51
![](https://img.aspxhome.com/file/2023/6/139556_0s.gif)
Java并发线程池实例分析讲解
2022-08-05 20:25:40
![](https://img.aspxhome.com/file/2023/2/61382_0s.png)
Java GC 机制与内存分配策略详解
2022-06-12 05:36:44
![](https://img.aspxhome.com/file/2023/6/74926_0s.png)
springcloud eureka切换nacos的配置方法
2022-05-19 01:58:47
C#实现简单的点餐系统
2023-02-09 19:25:03
![](https://img.aspxhome.com/file/2023/3/102203_0s.jpg)
利用C#实现分割GIF图片
2021-06-15 16:16:41
![](https://img.aspxhome.com/file/2023/7/113807_0s.png)
Java实战之实现用户登录
2022-08-03 14:42:55
![](https://img.aspxhome.com/file/2023/0/69110_0s.png)
Mybatis-plus多数据源配置的两种方式总结
2023-07-24 05:22:48
![](https://img.aspxhome.com/file/2023/3/57613_0s.png)
C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
2022-05-17 11:46:16
![](https://img.aspxhome.com/file/2023/2/88372_0s.png)
Spring Bean生命周期之属性赋值阶段详解
2022-09-28 04:34:11
Android中微信小程序支付倒计时功能
2022-04-02 09:18:23
![](https://img.aspxhome.com/file/2023/9/91659_0s.gif)
利用 filter 机制给静态资源 url 加上时间戳,来防止js和css文件的缓存问题
2022-03-16 07:51:24
![](https://img.aspxhome.com/file/2023/3/116183_0s.png)
19款Javascript富文本网页编辑器
2021-12-08 23:48:45
![](https://img.aspxhome.com/file/2023/3/70623_0s.jpg)
C#多线程学习之Thread、ThreadPool、Task、Parallel四者区别
2023-08-27 05:32:14
![](https://img.aspxhome.com/file/2023/2/108772_0s.png)
使用C#调用百度地图并实现坐标点的设置以及读取示例
2023-01-06 02:32:57
![](https://img.aspxhome.com/file/2023/5/87015_0s.png)
Redis 订阅发布_Jedis实现方法
2023-11-10 03:38:41