1.1、准备环境
这篇文章的批量操作都将是针对User用户表,下面给出相关的代码。
SQL建表语句
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `username` varchar(50) DEFAULT NULL COMMENT '用户名称', `password` varchar(50) DEFAULT NULL COMMENT '密码', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3010 DEFAULT CHARSET=utf8;
1.2、批量新增
<?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.mybatis.demo.mapper.UserMapper"> <!-- 批量插入 --> <insert id="batchInsert" parameterType="com.mybatis.demo.domain.User"> insert into user(id, username, password) values <foreach collection="userList" item="user" separator=","> (#{user.id}, #{user.username}, #{user.password}) </foreach> </insert> </mapper>