mybatis實現批量插入
阿新 • • 發佈:2018-12-21
entity層資訊:
public class DeviceInfo { private Long id; private String sbmc; private String sbip; private Integer ywlldk; private Integer gllldk; private String sblxmc; private String sbggmc; private Long dydmid; private String sbbz; private Long sblxid; private Long sbggsyz; private Long gxsj; private Short valid; //set,get }
dao層資訊:
public interface DeviceInfoMapper {
//批量插入
public int insertDeviceInfoBatch(List<DeviceInfo> deviceInfoList);
}
service層資訊:
public interface DeviceInfoService { //批量插入 public int insertDeviceInfoBatch(List<DeviceInfo> deviceInfoList); } @Service public class DeviceInfoServiceImpl implements DeviceInfoService { @Override public int insertDeviceInfoBatch(List<DeviceInfo> deviceInfoList) { return dao.insertDeviceInfoBatch(deviceInfoList); } }
mapper.xml檔案配置:
<insert id="insertDeviceInfoBatch" parameterType="java.util.List"> INSERT INTO device_info(SBMC, SBIP, YWLLDK, GLLLDK, SBLXMC, SBGGMC, DYDMID, SBBZ, SBLXID, SBGGSYZ, GXSJ, VALID) VALUES <foreach collection="list" item="item" separator=","> (#{item.sbmc,jdbcType=VARCHAR}, #{item.sbip,jdbcType=VARCHAR}, #{item.ywlldk,jdbcType=INTEGER}, #{item.gllldk,jdbcType=INTEGER}, #{item.sblxmc,jdbcType=VARCHAR}, #{item.sbggmc,jdbcType=VARCHAR}, #{item.dydmid,jdbcType=BIGINT}, #{item.sbbz,jdbcType=VARCHAR}, #{item.sblxid,jdbcType=BIGINT}, #{item.sbggsyz,jdbcType=BIGINT}, #{item.gxsj,jdbcType=BIGINT}, #{item.valid,jdbcType=SMALLINT}) </foreach> </insert>
若插入的物件沒有id,它就會自動生成;今天調了半天,出現的問題就是foreach裡面“#{item.sbmc,jdbcType=VARCHAR}”寫成了“#{sbmc,jdbcType=VARCHAR}”,導致遍歷的時候沒有找到list裡面的abmc的屬性,應該加個item來修飾,相當於java裡面的foreach標籤。