MyBatis: 批量插入
原创
©著作权归作者所有:来自51CTO博客作者wx62e0d796b5814的原创作品,请联系作者获取转载授权,否则将追究法律责任
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.fkit.domain.User;public interface UserMapper {
@Insert("<script>" +
"insert into tb_user(username,loginname,password,phone,address) values "
+"<foreach collection='users' separator=',' item='usr'> "
+ "(#{usr.username},#{usr.loginname},#{usr.password},#{usr.phone},#{usr.address}) "
+ "</foreach> "
+"</script>")
public void batchInsertUser(@Param("users") List<User> users);
}
UserMapper um=session.getMapper(UserMapper.class);
List<User> users=new LinkedList<>();
users.add(new User("dos","dfkdf","fdkdf","dfkdf","fdkfd"));
users.add(new User("dosss","dffskdf","fdfdkdf","dfdffkdf","fdfdkfd"));
um.batchInsertUser(users);
session.commit();