Mybatis结果生成键值对的实例代码
作者:mrr 时间:2023-11-28 15:50:58
下面给大家介绍下mybatis结果生成键值对的实例代码,具体内容如下所示:
在实际应用中我们经常会遇到这样的情况,需要给下拉框赋值,这个时候就需要键值对了,具体使用方法如下
1,在maper.xml文件中定义结果类型(resultType)定义为hashmap,如下所示
<select id="selectSuperUnitInfo" resultType="hashmap">
SELECT unit_id ,unit_name from unit_info
</select>
2,在对应的mapper类中用List<Map<String,String>>来接受这个类型,如下所示
public List<Map<String,String>> selectSuperUnitInfo();
其实最强大的莫过于resultMap类型了,这个可以在mapper.xml文件中自定义扩展类型,然后在包中添加这个类型即可,真是强大
下面给大家补充下MyBatis返回Map键值对数据
List<Map<String, String>> getMtypeList();
<select id="getMtypeList" resultType="java.util.HashMap">
select code,`name` from jk_control_measure
</select>
[DEBUG] 2016-08-29 17:50:09 :==> Executing: select code,`name` from jk_control_measure
[DEBUG] 2016-08-29 17:50:09 :==> Parameters:
[DEBUG] 2016-08-29 17:50:10 :<== Columns: code, name
[DEBUG] 2016-08-29 17:50:10 :<== Row: one, 地面冲洗
[DEBUG] 2016-08-29 17:50:10 :<== Row: two, 边界围挡
[DEBUG] 2016-08-29 17:50:10 :<== Row: three, 垃圾覆盖
[DEBUG] 2016-08-29 17:50:10 :<== Row: four, 裸地覆盖
[DEBUG] 2016-08-29 17:50:10 :<== Row: five, 洒水降尘
[DEBUG] 2016-08-29 17:50:10 :<== Row: six, 车辆冲洗
[DEBUG] 2016-08-29 17:50:10 :<== Row: seven, 建筑渣土
[DEBUG] 2016-08-29 17:50:10 :<== Row: eight, 车辆冒装
[DEBUG] 2016-08-29 17:50:10 :<== Row: nine, 扬尘覆盖
[DEBUG] 2016-08-29 17:50:10 :<== Row: ten, 车辆撒漏
[DEBUG] 2016-08-29 17:50:10 :<== Row: eleven, 车辆黑烟
[DEBUG] 2016-08-29 17:50:10 :<== Row: twelve, 道路积尘
[{"NAME":"地面冲洗","name":"地面冲洗","code":"one","CODE":"one"},
{"NAME":"边界围挡","name":"边界围挡","code":"two","CODE":"two"},
{"NAME":"垃圾覆盖","name":"垃圾覆盖","code":"three","CODE":"three"},
{"NAME":"裸地覆盖","name":"裸地覆盖","code":"four","CODE":"four"},
{"NAME":"洒水降尘","name":"洒水降尘","code":"five","CODE":"five"},
{"NAME":"车辆冲洗","name":"车辆冲洗","code":"six","CODE":"six"},
{"NAME":"建筑渣土","name":"建筑渣土","code":"seven","CODE":"seven"},
{"NAME":"车辆冒装","name":"车辆冒装","code":"eight","CODE":"eight"},
{"NAME":"扬尘覆盖","name":"扬尘覆盖","code":"nine","CODE":"nine"},
{"NAME":"车辆撒漏","name":"车辆撒漏","code":"ten","CODE":"ten"},
{"NAME":"车辆黑烟","name":"车辆黑烟","code":"eleven","CODE":"eleven"},
{"NAME":"道路积尘","name":"道路积尘","code":"twelve","CODE":"twelve"}
]
返回的结果带有大写与小写的key
如果在sql语句中的列名使用大写会怎样呢?自己测试一下吧!
标签:mybatis,map,键值对
0
投稿
猜你喜欢
浅述int与string类型转换的两种方法
2023-09-09 08:56:28
C# 文件上传下载(Excel导入,多线程下载)功能的实现代码
2021-12-09 20:16:34
C#实现飞行棋(Winform)
2023-08-07 07:08:55
c# this关键字用法代码详解
2022-06-07 15:22:09
SpringBoot实现过滤器拦截器的耗时对比
2022-04-12 00:45:04
Spring中Bean扫描原理详情
2022-05-26 04:33:14
iOS获取AppIcon and LaunchImage's name(app图标和启动图片名字)
2022-01-11 02:39:14
SpringBoot如何动态改变日志级别
2021-08-29 02:32:17
Java多线程 Producer and Consumer设计模式
2022-08-30 07:13:24
基于C++实现的哈夫曼编码解码操作示例
2023-10-13 13:02:43
SpringBoot整合WebSocket实现聊天室流程全解
2021-07-03 23:35:21
Kotlin中ListView与RecyclerView的应用讲解
2023-01-24 01:26:44
C#编程实现动态改变配置文件信息的方法
2022-05-01 15:09:25
Java加载property文件配置过程解析
2023-10-07 07:53:03
C# 使用鼠标点击对Chart控件实现数据提示效果
2023-03-05 14:20:06
Android Studio实现简易计算器设计
2022-08-22 18:41:55
java编程小白进阶包的作用详解
2022-01-22 05:15:24
Android表格自定义控件使用详解
2023-12-23 23:35:36
Android开发Jetpack组件WorkManager用例详解
2023-05-09 03:27:07
C++二分查找算法实例
2021-09-07 20:40:05