前后端如何使用MyBatis使用Map作为返回值

项目说明:本项目是前后端分离项目,后端使用springBoot框架,前端使用vue.js框架

一、后端

1.controller层接口

 @ApiImplicitParams({
            @ApiImplicitParam(name = "map",
                    value = "",
                    required = true, dataType = "map", paramType = "body"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query",defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "页大小", required = true, dataType = "Integer", paramType = "query",defaultValue = "5")
    })
    @PostMapping("/findAllDeviceAndMainBoard")
    public RespPageBean findAllDeviceAndMainBoard(@RequestBody Map<String,String> map,Integer page, Integer rows) {
        try {

            return otherService.findAllDeviceAndMainBoard(map,page,rows);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

说明:

RespPageBean是封装好返回前端接收对象,接收参数也是map对象

2.server层

接口:

 /** 根据主板编号和机构编号获取所有设备和主板信息 */
    RespPageBean findAllDeviceAndMainBoard(Map<String,String> map,Integer page, Integer rows);

实现类:

 /** 根据主板编号和机构编号获取所有设备和主板信息 */
    @Override
    public RespPageBean findAllDeviceAndMainBoard(Map<String,String> map,Integer page, Integer rows) {
        try {
            PageInfo<Map<String, Object>> pageInfo = PageHelper.startPage(page,rows)
                    .doSelectPageInfo(new ISelect() {
                        @Override
                        public void doSelect() {
                            otherMapper.findAllDeviceAndMainBoard(map);
                        }
                    });
            RespPageBean respPageBean = new RespPageBean();
            respPageBean.setData(pageInfo.getList());
            respPageBean.setTotal(pageInfo.getTotal());
            return respPageBean;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }

3.Mapper层:

 /** 根据主板编号和机构编号获取所有设备和主板信息 */
List<Map<String,Object>> findAllDeviceAndMainBoard(Map<String,String> map);

4.mybatis xml

 <!-- 获取所有设备和主板信息 -->
    <select id="findAllDeviceAndMainBoard" resultType="java.util.Map" parameterType="java.util.Map">
        select d.`id` deviceId, d.`name`,d.`ip`,d.`port`,d.`status`,d.`organize` deviceOrganize,d.`motherboard` deviceHardwareSN,d.`threadId`,d.`lastDeviceJson`,d.`remarks`,
        m.*,
        (select b.`name` from tb_brand b where id = m.`brand`) brandName ,
        (select dept_name from sys_dept where dept_id = d.`organize`) orgName ,
        (select count(id) from tb_nozzle n where n.`motherboard` = m.`motherboard` and n.`organize` = m.`organize`) nozzleCount,
        (select count(id) from tb_meter me where me.`motherboard` = m.`motherboard` and me.`organize` = m.`organize`) meterCount,
        (select count(id) from tb_display dis where dis.`motherboard` = m.`motherboard` and dis.`organize` = m.`organize`) displayCount,
        (select count(id) from tb_fuelingpoint fue where fue.`hardwareSN` = m.`motherboard` and fue.`organize` = m.`organize`) fuelingPointCount
        from tb_device d left join tb_mainboard m
        on d.`motherboard` = m.`motherboard` and d.`organize` = m.`organize`

        <where>
            1 = 1
            <!-- 设备id -->
            <if test="motherboard!= null and motherboard!=''">
                and m.`motherboard` = #{motherboard}
            </if>
            <!-- 所属机构 -->
            <if test="organize != null and organize !=''">
                and m.`organize` = #{organize}
            </if>
            <!-- 厂家品牌 -->
            <if test="brand != null and brand !=''">
                and m.`brand` = #{brand}
            </if>
        </where>
        order by id asc
	</select>
注意:返回结果是resultType而不是填resultMap,parameterType是参数类型。

二、前端:

async getDeviceList(motherboard,deptId,brand){
                //先判断deptId是否为空,如果为空则设置其默认值为查看当前站id
                if (deptId == '' || deptId == null || deptId == undefined){
                    deptId = ''
                }
                let url = '/other/findAllDeviceAndMainBoard?page=' + this.page + '&rows=' + this.size;
                const params = {"motherboard":motherboard,"organize":deptId,"brand":brand};
                this.postRequest(url,params).then(res =>{
                    this.deviceList = res.data;
                    this.total = res.total;
                })
            },

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值