mybaits xml 文件配置形式很容易寫出來,官方也給出了例子,html
例如:http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html代碼foreach動態sql就能夠實現批量插入數據.java
mybaits註解形式批量插入數據,就須要本身拼接成sql語句.
@InsertProvider(type = UserMapperProvider.class, method = "inserAll") int insertAll(List<User> list); public static class UserMapperProvider { public String inserAll(Map<String, List<User>> map) { List<User> list = map.get("list"); StringBuilder stringBuilder = new StringBuilder(256); stringBuilder.append("insert into \"spring4_2015_user\" (\"loginName\", \"name\", \"password\", \"salt\",\"email\",\"status\",\"teamId\") values "); MessageFormat messageFormat = new MessageFormat("(#'{'list[{0}].loginName},#'{'list[{0}].name},#'{'list[{0}].password},#'{'list[{0}].salt},#'{'list[{0}].email},#'{'list[{0}].status},#'{'list[{0}].teamId})"); for (int i = 0; i < list.size(); i++) { stringBuilder.append(messageFormat.format(new Integer[]{i})); stringBuilder.append(","); } stringBuilder.setLength(stringBuilder.length() - 1); return stringBuilder.toString(); } }
上述代碼是H2數據庫sql語句.固然mysql也支持這種批量插入數據.mysql