Mybatis 插入与批量插入以及多参数批量删除
说明:mybatis实现数据的批量插入及删除功能
一:批量插入
实体类:
package com.thinkgem.wlw.modules.midea.realtimedata.entity; import com.thinkgem.wlw.common.persistence.DataEntity; import java.util.Date; /** * @Author: zhouhe * @Date: 2019/6/12 10:10 */ public class Realtimedata extends DataEntity<Realtimedata> { private String code; private int device_param_id; private Double value; private Date time; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getDevice_param_id() { return device_param_id; } public void setDevice_param_id(int device_param_id) { this.device_param_id = device_param_id; } public Double getValue() { return value; } public void setValue(Double value) { this.value = value; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } }
Mapper接口:
package com.thinkgem.wlw.modules.midea.realtimedata.dao; import com.thinkgem.wlw.common.persistence.CrudDao; import com.thinkgem.wlw.common.persistence.annotation.MyBatisDao; import com.thinkgem.wlw.modules.midea.realtimedata.entity.Realtimedata; import java.util.List; /** * 实时表数据(模拟量参数) * @Author: zhouhe * @Date: 2019/6/12 10:11 */ @MyBatisDao public interface RealtimedataDao extends CrudDao<Realtimedata> { void insertByBatch(List<Realtimedata> xtd_device_data_monitor); }
Mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.thinkgem.wlw.modules.midea.realtimedata.dao.RealtimedataDao"> <insert id="insertByBatch" parameterType="java.util.List"> insert into xtd_device_data_monitor (code,device_param_id,value,time) values <foreach collection="list" item="item" index="index" separator=","> (#{item.code,jdbcType=VARCHAR}, #{item.device_param_id,jdbcType=INTEGER},#{item.value,jdbcType=DOUBLE},now()) </foreach> </insert> </mapper>
二:多参数批量删除示例
package com.vrv.linkdood.app.workreport.demomodule.mapper;import org.apache.ibatis.annotations.Param;public interface AttachmentTableMapper { void deleteByLogIdAndNames(@Param("logid") Integer logID, @Param("names") String[] names); }
<delete id="deleteByLogIdAndNames"> delete from attachment_table where logid = #{logid,jdbcType=INTEGER} AND NAME IN <foreach collection="names" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </delete>
文章来源:https://www.cnblogs.com/liaojie970/p/5577018.html