实体类
@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);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/153511.html