MySQL数据库中插入数据的几种方式

insert into

最简单也是最基础的插入方式,传入List集合,然后在Sql语句中遍历取出

insert into `TableName`(field1,field2,field3,...)
values  
<foreach item="item" index="index" collection="list" separator=",">
( #{item.value1}, #{item.value2}, #{item.value3})
        </foreach>

insert ignore into

插入数据时,如果存在则忽略 不存在则添加

insert  ignore  into `TableName`(field1,field2,field3,...)
values  
<foreach item="item" index="index" collection="list" separator=",">
( #{item.value1}, #{item.value2}, #{item.value3})
        </foreach>

on duplicate key update

插入数据时,如果数据存在,则更新数据,数据不存在则添加

insert into `TableName`(field1,field2,field3...)
values <foreach item="item" index="index" collection="list" separator=",">
( #{item.value1}, #{item.value2}, #{item.value3})
        </foreach>
ON DUPLICATE KEY UPDATE
field1=values(field1),
field2=values(field2),
field3=values(field3)

replace into

插入数据时,如果存在则删除重新插入

replace into `TableName`(field1,field2,field3,...)
values  
<foreach item="item" index="index" collection="list" separator=",">
( #{item.value1}, #{item.value2}, #{item.value3})
        </foreach>

你可能感兴趣的:(学习笔记,小白学Java)