关于Mybatis查询结果的封装
1.结果封装为List<Object>
接口示例:
public List<Members> selectMembersListByName(String name);
配置文件示例:
<select id="selectMembersListByName" resultType="members"> select * from members where member_name like #{member_name} </select>
运行结果:
[Member [id=3, member_name=关云长, password=123456, age=54], Member [id=4, member_name=关云长, password=123456, age=54]]
返回值类型resultType还是对象类型并不是list,mybatis会自动将对象封装成list集合
2.将单条记录封装成Map<String,Object>,其中key就是表的列名,value就是对应的值
接口示例:
public Map<String, Object> selectMembersByIdReturnMap(Integer id);
配置文件示例:
其中resultType是map,因为mybatis为常用类已经起了别名(typeAliases)
<select id="selectMembersByIdReturnMap" resultType="map"> select * from members where id = #{id} </select>
运行结果:
{pass_word=123456, id=1, member_name=张三, age=25}
3.将多条记录封装成Map其中key是我们指定字段的名字,value是记录对象Map<String,Members>
接口示例:
@MapKey就是指定map中key是什么
@MapKey("id") public Map<String, Members> selectMembersByNameReturnMap(String name);
配置文件示例:
返回值类型还是对象
<select id="selectMembersByNameReturnMap" resultType="members"> select * from members where member_name like #{member_name} </select>
运行结果:
{3=Member [id=3, member_name=关云长, password=123456, age=54], 4=Member [id=4, member_name=关云长, password=123456, age=54]}