gpt4 book ai didi

Mybatis查找返回Map,List集合类型的数据方式

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 28 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Mybatis查找返回Map,List集合类型的数据方式由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

Mybatis查找返回Map,List集合类型的数据

1、查找返回Bean对象的List集合

基本与返回Bean对象没什么区别,resultType依然为Bean对象的全类名,只是接口中的方法类型要进行修改 。

?
1
public List<Employee> getEmpListByEmail(String email);

2、查询返回Bean对象的Map集合

同样,resultType依然为Bean对象的全类名,只是接口中的方法类型要进行修改,添加注解.

@MapKey("Bean对象属性名"):指定Map的键为Bean对象的哪个属性,一般设置为主键,因为Map不能存重复的键.

?
1
2
@MapKey ( "id" )
public Map<Integer,Employee> getEmpMapByEmail(String email);

3、查询返回单条纪录的Map集合

即当前记录以键为列名,值为列属性存入map(查询到的记录一定要只有一条,否则报错) 。

注意,resultType需要设置为map,接口中方法类型需要修改,Map的键默认为列名.

?
1
public Map<String, Object> getEmpByEmail(String email);

mybatis 查询返回List集合、map集合、List<Map>集合

返回map类型

1. xml中 。

?
1
2
< select id = "selectUser"  resultType = "java.util.HashMap"
</ select >

2.Dao接口中 。

?
1
Map<String,Object> selectUser();

这种方式SQL只能返回一行记录或者没有返回,如果返回多行记录,则程序报错.

返回List<String>类型

3. xml中 。

?
1
2
< select id = "selectUser"  resultType = "java.lang.String"
</ select >

2.Dao接口中 。

?
1
List<String> selectUser();

这种方式可以返回多行记录,但是每行记录只有指定的一列数据.

返回List<Map>类型

1.xml中 。

?
1
2
< select id = "selectUser"  resultType = "java.util.HashMap"
</ select >

2.Dao接口中 。

?
1
List<Map<String,Object>> selectUser ();

这种方式可以返回指定的多行多列的记录.

返回List<指定对象>类型

xml中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< resultMap id = "baseResult" type = "com.XXX.BscntrUnitInfoResult(对应对象)" >
   < result column = "unit_id" property = "unitId" jdbcType = "INTEGER" (字段映射关系)/>
   < result column = "unit_name" property = "unitName"
    jdbcType = "VARCHAR" />
   < result column = "unit_type" property = "unitType"
    jdbcType = "INTEGER" />
   < result column = "super_unit_id" property = "superUnitId"
    jdbcType = "INTEGER" />
   < result column = "gis_start_x" property = "gisStartX"
    jdbcType = "FLOAT" />
   < result column = "ext_top" property = "extTop" jdbcType = "DOUBLE" />
</ resultMap >
< select id = "getBscntrUnitInfoListByName" resultMap = "baseResult" >
  
</ select >

Dao接口中:

?
1
public List<BscntrUnitInfoResult> getBscntrUnitInfoListByName();

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.

原文链接:https://blog.csdn.net/qq_38487155/article/details/82854803 。

最后此篇关于Mybatis查找返回Map,List集合类型的数据方式的文章就讲到这里了,如果你想了解更多关于Mybatis查找返回Map,List集合类型的数据方式的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com