MyBatis 的每一次查询映射的返回类型都是 resultMap,查询的时候会将数据库中列数据复制到对象的相应属性上。
当我们返回类型属性是 resultType 的时候,MyBatis对自动的把对应的值赋给 resultType 所指定对象的属性。
所以当 resultType="java.util.Map" 时,不用在多表关联中配置对应的关系,SQL随意写,直接获得返回的属性值。
多表联查比较方便的 ~
1.接口的定义
List<Map<String, Object>> selectProduct(@Param("userId") long userId,
@Param("page") Integer page,@Param("limit") Integer limit);
2.XML
<select id="selectProduct" resultType="java.util.Map">
SELECT
p.rid AS rid,
p.writetime AS time,
m.money AS m_money,
l.money AS l_money,
h.money AS h_money,
FROM
t_product p
LEFT JOIN t_m_order m ON p.no= m.no
LEFT JOIN t_l_order l ON p.no= l.no
LEFT JOIN t_h_order h ON p.no= h.no
WHERE
p.user_id=#{userId}
ORDER BY
p.writetime DESC
LIMIT #{page},#{limit};
</select>
3.请求
@PostMapping("/selectProduct")
@ResponseBody
public R product(long userId,Integer page,Integer limit) {
Integer page = StringUtils.isEmpty(page) ? 1 : Integer.parseInt(page);
Integer limit = StringUtils.isEmpty(limit) ? 10 : Integer.parseInt(limit);
Integer pages = limit*(page - 1);
Integer total = null;//总页数
List<Map<String, Object>> listmap=productService.selectProductStream(userId,pages,limit);
Integer totalPageNum = (total + limit - 1) / limit;
return R.ok().put("result", listmap).put("totalPageNum", totalPageNum);
}
4.页面解析参数
mui.ajax({
...
success: function(data) {
for (var i = 0; i < data.result.length; i++) {
alert("总页数:"+data.totalPageNum);
alert("ID:"+data.result[i].rid);
alert("时间:"+data.result[i].time);
alert("M:"+data.result[i].m_money);
alert("L:"+data.result[i].l_money);
alert("H:"+data.result[i].h_money);
}
},
...
});
取值的 name 就是xml查询列表 AS 别名