欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis插入数据是参数map且map中values为对象的处理

程序员文章站 2022-04-12 22:53:20
...
xml配置:
    <insert id="insertWinePrice" parameterType="java.util.Map">
        insert into wineprice (id,price,time) VALUES
        <foreach collection="params.keys" item="key" open="("  separator=",">
            ${key}
        </foreach>
        ,
        <foreach collection="params.values" item="v" close=")"  separator=",">
            #{v.m},#{v.p}
        </foreach>
    </insert>

接口代码:
public void insertWinePrice(@Param("params") Map<Integer,JdWinePo> map);

测试代码:
        Reader reader = Resources.getResourceAsReader("config/conf.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = factory.openSession();
        JdMapper jm = sqlSession.getMapper(JdMapper.class);
        Map<Integer,JdWinePo> map = new HashMap<Integer, JdWinePo>();
        JdWinePo j = new JdWinePo();
        j.setId("3232");
        j.setM("erwewr");
        j.setOp("fsaeet");
        j.setP("4343");
        map.put(212112,j);
        jm.insertWinePrice(map);
        sqlSession.commit();
        sqlSession.close();


这里我的理解。xml配置生成sql语句不能生成批量sql语句,所有的批量sql,只能在业务逻辑代码处进行处理。