from—https://www.oschina.net/code/snippet_42170_36396

1. [代码]mybatis javabean 映射文件(部分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<insert id="creatTableAndInsert" parameterType="java.util.Map" statementType="STATEMENT">
        create table if not exists ${tablename}
        <foreach collection="keys" item="k" index="index" open="(" separator="," close=");">
            ${k} varchar(30) not null
        </foreach>
        insert into ${tablename}
        <foreach collection="keys" item="k" index="index" open="(" separator="," close=")">
            ${k}
        </foreach>
        values
        <foreach collection="keys" item="k" index="index" open="(" separator="," close=")">
            '${params[k]}'
        </foreach>
    </insert>

2. [代码]mybatis中传入的map结构    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Map<String, Object> params = new HashMap<>();
            params.put("a", "safd");
            params.put("b", "dsjaf");
            params.put("c", "123456");
            params.put("d", "风景的撒娇房顶上经费等快速拉进房间空间上");
            
            String[] keys = new String[params.size()];
            Set<String> sset = params.keySet();
            int i = 0;
            for (String os : sset) {
                keys[i++] = os;
            }
            Map map = new HashMap<>();
            map.put("tablename", "pay_o_tb");
            map.put("keys", keys);
            map.put("params", params);
            mapper.creatTableAndInsert(map);