mybatis返回多个map结果
如果返回一条结果,xml直接这样写:
<select id="searchncomedateByInvestID" resultMap="java.util.HashMap">
? ? select
? ? t1.invest_id ? ? ? ? ? ? ? ? ? ? ?,
? ? cast(t1.modify_time AS DATE) modify_time
? ? from t_c_wh_redeeminfo t1
? ? where 1=1
? ? and t1.invest_id =#{investId}
</select>
dao中:
Map<String,Object> searchncomedateByInvestID(investId);
如果返回多条结果,xml这样写:
<resultMap id="getAllSetDaysResult" ? type="HashMap">
? ? ? ? <result property="investid" column="invest_id" jdbcType="VARCHAR" />
? ? ? ? <result property="modifytime" column="modify_time" jdbcType="DATE"/>
</resultMap>
<select id="searchncomedateByInvestID" parameterType="java.util.List" resultMap="getAllSetDaysResult">
? ? select
? ? t1.invest_id ? ? ? ? ? ? ? ? ? ? ?,
? ? cast(t1.modify_time AS DATE) modify_time
? ? from t_c_wh_redeeminfo t1
? ? where 1=1
? ? and t1.invest_id in
? ? <foreach collection="list" item="investId" index="index" ?open="(" close=")" separator=",">
? ? ? ? #{investId}
? ? </foreach>
</select>
dao中:
List<Map<String, Object>> searchncomedateByInvestID(List<String> preinvestList);
mybatis返回map类型的注意事项及小技巧
项目中为了避免定义java实体Bean,Mapper中往往会返回map类型。而返回map类型有几种定义方式:
1.resultType="java.util.Map"
接口中返回类型为Map<String,Object>;
例如:
<select id="getRoleInfo" resultType="java.util.Map">
? ? ? ? select sr.role_level as roleLevel,
? ? ? ? ? ? ? ?sr.id as roleId,
? ? ? ? ? ? ? ?sr.name as roleName,
? ? ? ? ? ? ? ?sr.code as roleCode
? ? ? ? from fqc_sys_role sr
? ? ? ? left join fqc_sys_user_role sur on sr.id=sur.role_id
? ? ? ? left join fqc_sys_user su on sur.user_id=su.id
? ? ? ? where su.id=#{userId}
? ? </select>
Map<String,Object> getRoleInfo(@Param("userId")Integer id);
这种情况Map中的value返回值类型是由mysql数据库字段类型 jdbcType与javaType的对应关系决定具体是什么类型,其中role_level 数据库为int,对应的javaType就是Integer;name,code字段为varchar,对应的javaType就是String。
在调用接口获得返回值后还需要转换为对应类型。
2.定义一个resultMap标签,
在resultMap的type属性指定type="java.util.Map";
例如:
<resultMap id="roleResultMap" type="java.util.Map">
?? ?<result property="roleLevel" column="role_level" javaType="java.lang.String"/>
?? ?<result property="roleId" column="id" javaType="java.lang.String"/>
?? ?<result property="roleName" column="name" javaType="java.lang.String"/>
?? ?<result property="roleCode" column="code" javaType="java.lang.String"/>
</resultMap>
<select id="getRoleInfo" resultMap="roleResultMap">
? ? ? ? select sr.role_level,
? ? ? ? ? ? ? ?sr.id,
? ? ? ? ? ? ? ?sr.name,
? ? ? ? ? ? ? ?sr.code
? ? ? ? from fqc_sys_role sr
? ? ? ? ? ? ? ? ?left join fqc_sys_user_role sur on sr.id=sur.role_id
? ? ? ? ? ? ? ? ?left join fqc_sys_user su on sur.user_id=su.id
? ? ? ? where su.id=#{userId}
</select>
Map<String,String> getRoleInfo(@Param("userId")Integer id);
这种情况Map中的value返回值类型在resultMap中通过javaType指定了,在调用接口获得返回值后可以直接使用。
3.返回的Map对象
第一列数据作为key,第二列数据作为value,跟列名就没关系了。
需要用到ResultHandler,ResultHandler主要作用是用来做数据转换的,这里不详细展开。
定义一个MapResultHandler
public class MapResultHandler<K,V> implements ResultHandler<Map<K,V>> {
? ? private final Map<K,V> mappedResults = new HashMap<>();
?
? ? @Override
? ? public void handleResult(ResultContext context) {
? ? ? ? Map map = (Map) context.getResultObject();
? ? ? ? mappedResults.put((K)map.get("key"), (V)map.get("value"));
? ? }
?
? ? public Map<K,V> getMappedResults() {
? ? ? ? return mappedResults;
? ? }
}
mapper中定义的方法设置返回值为void
public interface AllMedicalRecordStatisticsMapper extends BaseMapper<AllMedicalRecordStatistics> {
?
? ? void queryAllAverageScoreList(@Param("params") List<String> params, MapResultHandler<String,Double> mapResultHandler);
?
? ? void queryFirstRateRecordList(@Param("params") List<String> params, MapResultHandler<String, Double> mapResultHandler);
}
service调用时new一个自定义的MapResultHandler
public Map<String, Double> queryAllAverageScoreList(List<String> params) {
?? ?MapResultHandler<String, Double> resultHandler = new MapResultHandler<>();
?? ?allMedicalRecordStatisticsMapper.queryAllAverageScoreList(params,resultHandler);
?? ?return resultHandler.getMappedResults();
}
xml中定义一个对应的resultMap
<resultMap id="mapResultScore" type="java.util.HashMap">
? ? ? ? <result property="key" column="statis_date" jdbcType="VARCHAR"/>
? ? ? ? <result property="value" column="averageScore" jdbcType="DOUBLE" javaType="java.lang.Double"/>
? ? </resultMap>?
?
<select id="queryAllAverageScoreList" resultMap="mapResultScore">
? ? ? ? <foreach collection="params" item="item" index="index" ?separator="UNION ALL" >
? ? ? ? ? ? select DATE_FORMAT(statis_date,'%Y-%m') as statis_date,
? ? ? ? ? ? ? ? ? ?ROUND(IFNULL(record_score/NULLIF(record_count,0),0),2) as averageScore
? ? ? ? ? ? from all_medical_record_statistics
? ? ? ? ? ? where DATE_FORMAT(statis_date,'%Y-%m') = #{item}
? ? ? ? </foreach>
</select>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。