好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

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

?

1

public List<Employee> getEmpListByEmail(String email);

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

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

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

?

1

2

@MapKey ( "id" )

public Map<Integer,Employee> getEmpMapByEmail(String email);

三、查询返回单条纪录的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集合类型的数据方式的详细内容...

  阅读:43次