实体类
@Data
public class User {
private long id;
private String name;
private int age;
private String email;
private String phone;
private String username;
private String password;
}
mapper
public interface UserMapper {
/**
* 批量新增用户
*
* @param userList
* @return
*/
int batchAddNewUser(@Param("userList") List<User> userList);
}
对应的xml文件
<insert id="batchAddNewUser" parameterType="java.util.List">
insert into user(name, age, email, phone, username, password) VALUES
<foreach collection="userList" item="user" separator=",">
(#{user.name}, #{user.age}, #{user.email}, #{user.phone}, #{user.username}, #{user.password})
</foreach>
</insert>
在配置文件中指定xml位置
mybatis:
mapper-locations: classpath*:mapper/*.xml
目录结构如下:
附录: ibatis使用注解批量更新
@Insert("<script>"
+ " insert into user(user_name, age, time_stamp) values"
+ "<foreach collection = 'list' item='record' separator=','>"
+ " (#{record.userName}, #{record.age}, #{record.timestamp})"
+ "</foreach>"
+ "</script>")
int saveData(@Param("list") List<User> list);