总结:
1、合并sql
INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) VALUES ('0', 'userid_0', 'content_0', 0), ('1', 'userid_1', 'content_1', 1);
2、使用事务
3、插入时主键有序
4、用mybatis-plus的 insertBatch
mybatis实现一次插入多条记录
<!--id是自定义的方法名,parameterType是List-->
<insert id="insertChatHistoryList" useGeneratedKeys="true" parameterType="java.util.List">
<selectKey resultType="long" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
insert into n_chat_history (id,from_name,to_name,message_type, content)
values
<!--item就是List里每一项的对象名,要用","分割每一条数据,最后要";"结尾-->
<foreach collection="list" item="item" index="index" separator="," close=";">
(#{item.id,jdbcType=BIGINT}, #{item.fromName,jdbcType=VARCHAR}, #{item.toName,jdbcType=VARCHAR}, #{item.content,jdbcType=VARCHAR},
#{item.sendType,jdbcType=INTEGER})
</foreach>
</insert>
sql实现一次查询多条记录
insert into Custumer(cust_id,
cust_cintact,
cust_name,
cust_email,
cust_address,
cust_country)select cust_id,
cust_cintact,
cust_name,
cust_email,
cust_address,
cust_country
from CustNew;