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"
}
]
}