欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis结果生成键值对的实例代码

程序员文章站 2024-03-07 21:49:33
下面给大家介绍下mybatis结果生成键值对的实例代码,具体内容如下所示: 在实际应用中我们经常会遇到这样的情况,需要给下拉框赋值,这个时候就需要键值对了,具体使用方法如...

下面给大家介绍下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语句中的列名使用大写会怎样呢?自己测试一下吧!如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!