springMVC使用map接收前端传递过来的值

4 篇文章 0 订阅
3 篇文章 1 订阅
SpringMvc 后台使用map接收前端传递过来的数据
@GetMapping(value = "/getList")
public Result getList(@RequestParam HashMap<String, String> map) {

    logger.info(map);

    return Result.formatRet(dymJSONService.getList(map));
}
根据前端传递过来的map构建查询语句
public void setSelectSqlByMap(Map<String, String> 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<String, Object> 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<String, Object> 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:

<insert id="executeCreateTable" parameterType="com.wnzx.blockchain.manage.entity.DymJSONSql">
    ${createSql}
</insert>

<insert id="insertSql" parameterType="com.wnzx.blockchain.manage.entity.DymJSONSql">
    ${insertSql}
</insert>

<select id="executeQuerySql" parameterType="com.wnzx.blockchain.manage.entity.DymJSONSql" resultType="map">
    ${sql}
</select>

<select id="getAllCountBySql" parameterType="com.wnzx.blockchain.manage.entity.DymJSONSql" resultType="long">
    ${countSql}
</select>

整体需求,根据fabric区块写集内容自动创建表,插入数据,动态查询。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你的问题。以下是使用SpringBoot框架配置Spring和SpringMVC框架如何接收前端传递过来和对应的数据的转发显示的步骤。 1. 首先,你需要在你的pom.xml文件中添加SpringBoot框架的依赖。这可以通过以下代码来实现: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 然后,你需要创建一个Controller类来接受前端传递过来并处理它们。以下是一个简单的示例控制器类: ``` @RestController public class MyController { @GetMapping("/hello") public String hello(@RequestParam("name") String name) { return "Hello, " + name + "!"; } } ``` 在上面的示例中,我们使用了@GetMapping注解来指定我们的控制器应该处理哪个URL请求。我们还使用@RequestParam注解来指定我们要接收参数名称。 3. 接下来,你需要创建一个HTML页面来向你的控制器发送请求。以下是一个示例页面: ``` <html> <head> <title>Hello World!</title> </head> <body> <form method="get" action="/hello"> <label for="name">Enter your name:</label> <input type="text" name="name" /> <input type="submit" value="Submit" /> </form> </body> </html> ``` 在上面的示例中,我们使用了一个简单的表单来向我们的控制器发送请求。我们指定了请求的方法和URL,并为我们要接收参数指定了名称。 4. 最后,你需要运行你的SpringBoot应用程序并访问你的HTML页面。当你提交表单时,控制器将接收到请求并处理它。在上面的示例中,控制器将返回一个包含用户名称的简单欢迎消息。 希望这可以帮助你理解如何使用SpringBoot框架配置Spring和SpringMVC框架来接收前端传递过来并将它们转发显示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值