您的位置:首页 > 其它

使用mybatis的resultType="Map"可省去new返回值对象

2017-07-10 14:31 447 查看
@RestController  注解相当于@Controller+@ResponseBody ,用此注解返回无视图jsp


springmvc形参(@RequestBody HashMap hashMap),即请求过来的json就封装到了hashMap里,这样可以省去QueryVo,直接调用service接口(传入hashMap.get("id"),hashMap.get("name"))

但通常还是将请求过来的json通过@RequestBody注解转为实体类(或者用hashmap<String,String> hashmap来接收),然后调用service层(此时传入对象或hashmap)



在mapper映射文件里  

parameterType="Map"  请求参数就是你serviceImpl参数里传入的map

resultType="Map"      返回值map的键就是表中字段的别名,值就是是你查到的值,如果是多个,mapper接口就是list包map,即 List<Map<String, Object>>,如此可省去new返回值对象,很简单

Controller里的接口这么写:

@RequestMapping(value = "/getChannelBySiteKey", method = RequestMethod.POST)
public Msg getChannelBySiteKey(@RequestBody InspectionRecords inspectionRecords) {

List<Map<String, Object>> mapList = patrolRecordsService.getChannelBySiteKey(inspectionRecords);
if (mapList != null) {
return Msg.success().add(mapList);
}
return Msg.success().add("");
}

ServiceImpl这么写的:
public List<Map<String, Object>> getChannelBySiteKey(InspectionRecords inspectionRecords) {

Map<String,String> map=new HashMap<String,String>();
Assert.notNull(inspectionRecords.getSiteKey()," [Assertion siteKey] - this argument is required; it must not be null");
map.put("siteKey", inspectionRecords.getSiteKey());
List<Map<String, Object>> maps=patrolRecordsMapper.getChannelBySiteKey(map);
return maps;
}

mapper接口这么写:

public List<Map<String,Object>> getChannelBySiteKey(Map<String,String> map) ;

<select id="getChannelBySiteKey" resultType="Map" parameterType="Map" >
SELECT
dai.Channel AS ChannelId,
IFNULL(cai.CameraAliasid,'') AS ChannelName,
cai.isCloud AS isCloud
FROM
camera_all_info cai,
device_all_info dai
WHERE
cai.SiteId = dai.SiteId
AND dai.SiteId = #{siteKey}
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: