mybatis以map为参,动态生成表并插入数据

mybatis以map为参,动态生成表并插入数据,目前仅在PostgreSQL上做了测试
标签:  MyBatis  iBATIS

代码片段(2)[全屏查看所有代码]

1. [代码]mybatis javabean 映射文件(部分)     跳至 [1] [2] [全屏预览]

?
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 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 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);

?

你可能感兴趣的:(mybatis)