1.返回一条结果
接口定义
/**
* 根据ID查找用户,返回Map对象。
* @param id
* @return
*/
Map<String, Object> selectUserByIdForMap(Long id);
Mapper
<select id="selectUserByIdForMap" resultType="map">
select
id, user_name name, user_password password, user_phone phone, user_email email, user_info info, user_img img, create_time createTime
from
`db_user`
where
id = #{id}
</select>
测试类
@Test
public void testSelectUserByIdForMap() {
Map<String, Object> userMap = mapper.selectUserByIdForMap(2L);
String key = null;
Object value = null;
for (Map.Entry<String, Object> field : userMap.entrySet()) {
key = field.getKey();
value = field.getValue();
System.out.println(key + " : " + value);
}
}
测试结果
DEBUG [main] - Cache Hit Ratio [com.mybatis.mapper.UserMapper]: 0.0
DEBUG [main] - ==> Preparing: select id, user_name name, user_password password, user_phone phone, user_email email, user_info info, user_img img, create_time createTime from `db_user` where id = ?
DEBUG [main] - ==> Parameters: 2(Long)
TRACE [main] - <== Columns: id, name, password, phone, email, info, img, createTime
TRACE [main] - <== Row: 2, batch0, a0, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
DEBUG [main] - <== Total: 1
password : a0
img : [B@5c90e579
phone : 222
createTime : 2018-08-03 11:17:52.0
name : batch0
id : 2
email : 123@123.com
info : 222
2.返回多条结果
接口定义
/**
* 查找所有用户,返回Map。
* @return
*/
@MapKey("id")
Map<Long, User> selectAllUserForMap();
Mapper
<select id="selectAllUserForMap" resultType="User">
select
id, user_name name, user_password password, user_phone phone, user_email email, user_info info, user_img img, create_time createTime
from
`db_user`
</select>
测试类
@Test
public void testSelectAllUserForMap() {
Map<Long, User> userMap = mapper.selectAllUserForMap();
Long key = null;
User user = null;
for (Map.Entry<Long, User> map : userMap.entrySet()) {
key = map.getKey();
user = map.getValue();
System.out.println(key + " : " + user);
}
}
测试结果
这里为了方便显示只查询了前10条数据。
DEBUG [main] - Cache Hit Ratio [com.mybatis.mapper.UserMapper]: 0.0
DEBUG [main] - ==> Preparing: select id, user_name name, user_password password, user_phone phone, user_email email, user_info info, user_img img, create_time createTime from `db_user` limit 10
DEBUG [main] - ==> Parameters:
TRACE [main] - <== Columns: id, name, password, phone, email, info, img, createTime
TRACE [main] - <== Row: 1, hotch1, 111, 111, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-07-31 15:34:51
TRACE [main] - <== Row: 2, batch0, a0, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 3, batch1, a1, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 4, batch2, a2, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 5, batch3, a3, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 6, batch4, a4, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 7, batch5, a5, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 8, batch6, a6, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 9, batch7, a7, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
TRACE [main] - <== Row: 10, batch8, a8, 222, 123@123.com, <<BLOB>>, <<BLOB>>, 2018-08-03 11:17:52
DEBUG [main] - <== Total: 10
1 : User(id=1, name=hotch1, password=111, phone=111, email=123@123.com, info=111, img=[97, 118, 100], createTime=Tue Jul 31 15:34:51 CST 2018)
2 : User(id=2, name=batch0, password=a0, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
3 : User(id=3, name=batch1, password=a1, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
4 : User(id=4, name=batch2, password=a2, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
5 : User(id=5, name=batch3, password=a3, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
6 : User(id=6, name=batch4, password=a4, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
7 : User(id=7, name=batch5, password=a5, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
8 : User(id=8, name=batch6, password=a6, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
9 : User(id=9, name=batch7, password=a7, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
10 : User(id=10, name=batch8, password=a8, phone=222, email=123@123.com, info=222, img=[97, 118, 100], createTime=Fri Aug 03 11:17:52 CST 2018)
从上面的查询结果可以看出返回的 Map 的 key
值是返回对象的 id
,value
的值是对象本身。那么MyBatis是怎么知道 Map 的 key
是对象的 ID 值呢?这就要从定义接口方法时添加的注释说起了,@MapKey("id")
注释可以指定返回 Map 的 key
值是那个属性值,所以如果把注释中的 id
改为 name
的话 Map 的 key
就会是对象的 name
属性值了。