mybatis查询数据表返回所有字段+值的map结构

java代码

package io.renren.modules.service.dao;
​
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
​
import java.util.List;
import java.util.Map;
@Mapper
public interface GetColumnDao {
​
    @Select("select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns" +
    " where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position")
    public List<Map<String,Object>> getColumn(String tablename);
}
public List<Map<String, Object>> getCp(String cpbh, Integer type) {   
        TableName tabblename = null;
        String name = "";
        CpInfoCJZ cjz = cjzService.getById(cpbh);
        tabblename = cjz.getClass().getAnnotation(TableName.class);
        List<Map<String, Object>> list = getColumnDao.getColumn(tabblename.value());
        for (Map<String, Object> map : list) {
            name = (String) map.get("columnName");
            try {
                Object o = getGetMethod(cjz, name.replace("_", ""));
                if (o == null) {
                    throw new RuntimeException("未找到对应方法" + map.get("columnName"));
                } else {
                    map.put("value", o);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    /**
     * 根据属性,获取get方法
     *
     * @param ob   对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    public static Object getGetMethod(Object ob, String name) throws Exception {
        Method[] m = ob.getClass().getMethods();
        for (int i = 0; i < m.length; i++) {
            if (("get" + name).toLowerCase().equals(m[i].getName().toLowerCase())) {
                Object o = m[i].invoke(ob);
                if (o == null) {
                    return "";
                } else {
                    return o;
                }
            }
        }
        return null;
    }
    

返回结果json

{
    "msg": "success",
    "code": 0,
    "data": [
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "产品编号",
            "columnKey": "PRI",
            "value": "test000003",
            "columnName": "cpbh"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "服务器账号",
            "columnKey": "",
            "value": "11",
            "columnName": "fwqzh"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "服务器密码",
            "columnKey": "",
            "value": "11",
            "columnName": "fwqmm"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "服务器ip",
            "columnKey": "",
            "value": "11",
            "columnName": "fwq_ip"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "平台账号",
            "columnKey": "",
            "value": "11",
            "columnName": "ptzh"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "平台密码",
            "columnKey": "",
            "value": "11",
            "columnName": "ptmm"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "平台对接",
            "columnKey": "",
            "value": "11",
            "columnName": "ptdj"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "特殊要求",
            "columnKey": "",
            "value": "11",
            "columnName": "tsyq"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "备注",
            "columnKey": "",
            "value": "11",
            "columnName": "bz"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "软件版本",
            "columnKey": "",
            "value": "11",
            "columnName": "rjbb"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "固件版本",
            "columnKey": "",
            "value": "11",
            "columnName": "gjbb"
        },
        {
            "dataType": "varchar",
            "extra": "",
            "columnComment": "ERP品号",
            "columnKey": "",
            "value": "测试",
            "columnName": "erp_code"
        }
    ]
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值