使用mybatis foreach标签新增
dao层接口
int addOneBoard(@Param("oneBoards")List<OneBoard> oneBoards);//添加单板信息
xml
<mapper namespace="com.hbte.dao.OneBoardMapper">
<resultMap id="BaseResultMap" type="com.hbte.entity.OneBoard">
<id column="ONE_BOARD_MESSAGE_ID" jdbcType="INTEGER" property="oneBoardMessageId" />
<result column="ONE_BOARD_ID" jdbcType="VARCHAR" property="oneBoardId" />
<result column="FREQUENCY_POINT_MESSAGE" jdbcType="VARCHAR" property="frequencyPointMessage" />
<result column="COMPLETE_MACHINE_ID" jdbcType="INTEGER" property="completeMachineId" />
<result column="RADIO_FREQUENCY_PLATE_MESSAGE" jdbcType="VARCHAR" property="radioFrequencyPlateMessage" />
</resultMap>
<sql id="Base_Column_List">
ONE_BOARD_MESSAGE_ID, ONE_BOARD_ID, FREQUENCY_POINT_MESSAGE, COMPLETE_MACHINE_ID, RADIO_FREQUENCY_PLATE_MESSAGE
</sql>
<!-- 添加单板信息 -->
<insert id="addOneBoard" parameterType="com.hbte.entity.OneBoard">
INSERT INTO one_board
(ONE_BOARD_ID,FREQUENCY_POINT_MESSAGE,COMPLETE_MACHINE_ID,RADIO_FREQUENCY_PLATE_MESSAGE)VALUES
<foreach collection="oneBoards" item="item" separator=",">
(#{item.oneBoardId},#{item.frequencyPointMessage},#{item.completeMachineId},#{item.radioFrequencyPlateMessage})
</foreach>
</insert>
</mapper>
批量修改
<update id="updateBatch" parameterType="java.util.List">
update mydata_table
<trim prefix="set" suffixOverrides=",">
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status !=null and item.status != -1">
when id=#{item.id} then #{item.status}
</if>
<if test="item.status == null or item.status == -1">
when id=#{item.id} then mydata_table.status//原数据
</if>
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>