1、mybatis返回单个map存单条数据
mapper接口:
Map<String,Object> selectCount(String count);
select count(id) as value , name from xxx where xx = #{xx}
但这样数据库只能返回一条数据,如果数据多就会报错 except one …
遇到这样的情况,一般会写为
List<Map<String,Object>> selectCount(String count);
返回多个Map存储在List内。有时候这样的数据格式取值较为不便,所以需要单个Map存入多条消息。
2、mybatis返回单个Map存入多条信息。利用MapKey注解完成
可以新建一个do类来接收返回的值。 TestDo
public class TestDo{
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@MapKey("name")
Map<String,TestDo> selectCount(String count);
<resultMap id="params" type="TestDo">
<id property="name" column="NAME" />
<id property="value" column="VALUE" />
</resultMap>
<select id="selectCount" resultMap="params" parameterType="String">
select t.info as name, count(id) as value from Test t where val= #{count} group by pro
</select>