MyBatis动态key和value解决方案

内容纲要

场景

在遇到单表字段超多的时候,编写与之对应的实体类就相对不切实际,所以需要动态的key和value对数据进行操作,那么在java中key->value首先联想到的就是Map对象

Java中获取Map key->value

  • map.keySet():  先获取map的key,然后根据key获取对应的value;
  • map…entrySet():  同时查询map的key和value,只需要查询一次(迭代器)
  1. map.keySet()
for(String key : map.keySet()){
   String value = map.get(key);
   System.out.println(key+"  "+value);
}
  1. 第一种 map.entrySet()
for(Entry<String, String> vo : map.entrySet()){
   vo.getKey();
   vo.getValue();
   System.out.println(vo.getKey()+"  "+vo.getValue());
  }

Mybatis中获取Key-Value值

Mybatis中动态获取使用 foreach 标签

  • 这里也分两种方法
    • map.entrySet() ,同上述一样
    • map.keys,获取map的key值,map.value 获取值
  1. map.keySet()
  int insert(@Param("param") Map<String, Object> param);

  <insert id="insert" parameterType="java.util.Map">
    INSERT INTO table(a, b)
    <foreach collection="param.entrySet()" open="VALUES(" separator="," close=")" index="key" item="item">
        #{key}, #{item}
    </foreach>
  </insert>
  1. map.keys
# java代码
  int insert(@Param("param") Map<String, Object> param);

# xml代码
 <insert id="insert" parameterType="java.util.Map">  
         insert ignore into table_name
         <foreach collection="param.keys" item="key" open="(" close=")" separator="," >  
            ${key}  
         </foreach>  
         values   
         <foreach collection="param.keys"  item="key" open="(" close=")" separator=",">  
            #{param[${key}]}  
         </foreach>  
   </insert>  

批量插入 List<Map<String,String>>

  1. 方式1

    SQL打印:
    insert into (a,b) values (1,2),(1,2),(1,2)

# java代码
int insertTableData(@Param("fieldMaps") LinkedHashMap<String, Object> fieldMaps, @Param("maps") List<Map<String, Object>> maps, @Param("tableName") String tableName);

# xml代码
   <insert id="insertTableData" >
        insert into `${tableName}`
        <foreach collection="fieldMaps.keys" item="key" open="(" close=")values" separator=",">
            ${key}
        </foreach>
         <foreach collection="maps" item="map" open="" close=" " separator=",">
            <foreach item="value" index="key" collection="map.entrySet()" open="(" separator="," close=")">
                #{value}
            </foreach>
        </foreach>
    </insert>
  1. 方式2

    SQL打印:
    insert into (a,b) values (1,2);
    insert into (a,b) values (1,2);
    insert into (a,b) values (1,2);

# java 代码
int insertTableData(@Param("maps") List<Map<String, Object>> maps, @Param("tableName") String tableName);

# xml代码
 <insert id="insertTableDataWay2">
        <foreach collection="maps" item="map" open="" close=" " separator="">
            insert into   `${tableName}`
            <foreach item="value" index="key" collection="map.entrySet()" open="(" separator="," close=")">
                ${key}
            </foreach>
            values
            <foreach item="value" index="key" collection="map.entrySet()" open="(" separator="," close=");">
                #{value}
            </foreach>
        </foreach>
    </insert>

方式2 适用场景: 当list中的每一个map的key不相等时

扩展: select 的时候 可不可动态控制select的key呢?

THE END
分享
二维码
< <上一篇
下一篇>>