springMVC使用map接收前端传递过来的值 | 您所在的位置:网站首页 › 前端传map数据格式 › springMVC使用map接收前端传递过来的值 |
SpringMvc 后台使用map接收前端传递过来的数据
@GetMapping(value = "/getList")
public Result getList(@RequestParam HashMap map) {
logger.info(map);
return Result.formatRet(dymJSONService.getList(map));
}
根据前端传递过来的map构建查询语句
public void setSelectSqlByMap(Map map) {
String sql = "select * from table ";
this.page = map.get("page") == null ? 0 : Integer.valueOf(map.get("page"));
map.remove("page");
this.pageSize = map.get("pageSize") == null ? 10 : Integer.valueOf(map.get("pageSize"));
map.remove("pageSize");
if (map.size() > 0) {
sql = " where ";
for (Object key : map.keySet()) {
String k = (String) key;
String val = map.get(key);
if ("".equals(val)) {
continue;
}
if (!key.equals("page") && !key.equals("pageSize")) {
if (!sql.equals(" where ")) {
sql += "and";
}
sql += " `" + k + "`=\"" + val + "\"";
}
}
}
if (" where ".equals(sql)) {
sql = "";
}
String limit = " limit " + this.page + "," + this.pageSize;
sql += limit;
this.sql += sql;
}
数据样例: select * from writes_mychannel_transaction构建查询数量语句: public String getCountSql() { this.countSql = this.sql.replace("*", "count(1)"); int limit = this.countSql.indexOf("limit"); if (limit != -1) { this.countSql = this.countSql.substring(0, limit); } return countSql; }结果示例: select count(1) from writes_mychannel_transaction扩展: 使用map生成建表语句: public void setCreateSql(String table, Map cols) { this.table = table; this.createSql = "create table `" + table + "` (id int(20) AUTO_INCREMENT, ref_id varchar(200)"; for (String col : cols.keySet()) { Object value = cols.get(col); if (value instanceof String) { this.createSql += ",`" + col + "` text"; } else if (value instanceof Integer) { this.createSql += ",`" + col + "` bigint(24)"; } else if (value instanceof Date) { this.createSql += ",`" + col + "` date"; } else if (value instanceof Float) { this.createSql += ",`" + col + "` float(10,6)"; } else if (value instanceof Double) { this.createSql += ",`" + col + "` double(20,12)"; } else { this.createSql += ",`" + col + "` text"; } } this.createSql += ", primary key (id)"; this.createSql += ")"; }根据map生成插入语句: public void setInsertSql(String table, Map vals) { if ("".equals(table)) { throw new RuntimeException("表名不能为空"); } if (null == vals || vals.isEmpty()) { throw new RuntimeException("字段列表为空"); } this.table = table; this.insertSql = "insert into "+ table +" ("; String insert = ""; String values = ""; for (String key : vals.keySet()) { Object val = vals.get(key); insert += ",`" + key + "`"; if (val instanceof String) { values += ",\'" + val + "\'"; } else if (val instanceof Number) { values += "," + val; } else if (val instanceof Date) { values += ",\'" + val + "\'"; } else { values += ",\'" + JSON.toJSONString(val) + "\'"; } } this.insertSql += insert.substring(1) + ") values (" + values.substring(1) + ")"; }mybatis执行sql: ${createSql} ${insertSql} ${sql} ${countSql}整体需求,根据fabric区块写集内容自动创建表,插入数据,动态查询。 |
CopyRight 2018-2019 实验室设备网 版权所有 |