美文网首页
mybaties 批量更新方法

mybaties 批量更新方法

作者: whiffet | 来源:发表于2018-04-04 16:28 被阅读0次

    mxl配置

      <update id="updateLampXY" parameterType="java.util.Map">
              UPDATE lamp
                  <trim prefix="set" suffixOverrides=",">
                    <trim prefix="x=case" suffix="end,">
                        <foreach collection="list" item="cus">
                            when gps_num=#{cus.gpsNum} then #{cus.x}
                        </foreach>
                    </trim>
                    <trim prefix="y=case" suffix="end,">
                        <foreach collection="list" item="cus">
                            when gps_num=#{cus.gpsNum} then #{cus.y}
                        </foreach>
                    </trim>
                </trim>
            where gps_num IN
            <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
                #{item.gpsNum}
            </foreach>
        </update>    
    

    mapper接口

    public interface NewLampMapper {
        public void updateLampXY(Map list);
    }
    

    测试类

      public void test(){
           ApplicationContext apx = new 
           ClassPathXmlApplicationContext("classpath:spring/applicationContext-
           dao.xml");
            NewLampMapper nl = apx.getBean(NewLampMapper.class);
            String str = "[\n" +
                    "{\"x\":115.92734814029478,\"y\":28.70862343857048,\"gpsNum\":\"01005014\"},\n" +
                    "{\"x\":115.92774788029477,\"y\":28.708877738570482,\"gpsNum\":\"01005015\"},\n" +
                    "{\"x\":115.92806122029478,\"y\":28.709041338570483,\"gpsNum\":\"01005016\"},\n" +
                    "{\"x\":115.92823863029479,\"y\":28.708661078570483,\"gpsNum\":\"01054029\"}\n" +
                    "]\n";
            List list =  JSON.parseArray(str,JsonBean.class);
            List list1 = new ArrayList();
            Map map = new HashMap();
            for (Object m : list){
                JsonBean bea = (JsonBean)m;
                list1.add(bea);
            }
            map.put("list", list1);
            nl.updateLampXY(map);       
    

    javaBean

    public class JsonBean {
        private  double x;
        private  double y;
        private  String  gpsNum;
    
        public void setX(double x) {
            this.x = x;
        }
    
        public void setY(double y) {
            this.y = y;
        }
    
        public void setGpsNum(String gpsNum) {
            this.gpsNum = gpsNum;
        }
    
        public double getX() {
    
            return x;
        }
    
        public double getY() {
            return y;
        }
    
        public String getGpsNum() {
            return gpsNum;
        }
    }
    

    正常执行的完整sql

    UPDATE lamp 
        set x=case 
        when gps_num=? then ? 
        when gps_num=? then ?
         when gps_num=? then ? 
        when gps_num=? then ?
         end,
         y=case 
        when  gps_num=? then ?
         when gps_num=? then ?
         when gps_num=? then ?
         when gps_num=? then ?
         end
        where gps_num IN ( ? , ? , ? , ? ) 
    

    相关文章

      网友评论

          本文标题:mybaties 批量更新方法

          本文链接:https://www.haomeiwen.com/subject/zxtlhftx.html