@Mapper insertBatch 方法
时间: 2024-06-06 09:08:13 浏览: 85
mybatis_insertbatch_db.rar
@Mapper
public interface ExampleMapper {
@InsertProvider(type = ExampleSqlProvider.class, method = "batchInsert")
int insertBatch(List<Example> examples);
class ExampleSqlProvider {
public String batchInsert(Map<String, Object> map) {
List<Example> examples = (List<Example>) map.get("list");
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO example (id, name, age) VALUES ");
MessageFormat mf = new MessageFormat("(#'{'list[{0}].id}, #'{'list[{0}].name}, #'{'list[{0}].age})");
for (int i = 0; i < examples.size(); i++) {
sb.append(mf.format(new Object[]{i}));
if (i < examples.size() - 1) {
sb.append(",");
}
}
return sb.toString();
}
}
}
阅读全文