使用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>
相关文章推荐
- MyBatis中resultMap和resultType的使用
- mybatis 之 resultType="Map" parameterType="String"
- mybatis中resultType和resultMap使用时的区别
- mybatis简单查询,resultType及resultMap查询使用
- Mybatis中<select id="getImages" resultType="java.util.Map">查出的结果顺序
- mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".
- MyBatis Review——使用resultType和resultMap实现一对一查询
- MyBatis实战——resultMap,resultType简介,关联对象
- mybatis 之resultType="Map"
- Mybatis---查询使用resultType和resultMap的区别
- mybatis中resultMap与resultType的使用说明
- 我的试验__mybatis中resultType使用javaBean和Map的区别
- mybatis中resultType与resultMap使用经验
- mybatis ResultMap和ResultType在使用中的区别
- mybatis 使用 parameterType="Map" 传入值后 数据库中得到的却是 NULL
- mybatis中的resultType="java.util.Map",返回有重复数据
- mybatis resultType="map"
- MyBatis3 association error - The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)"
- MyBatis Review——使用resultType和resultMap实现一对一查询