1. Bulk insert
1.1 Batch insertion in the form of annotations (1)
@Repository
public interface UserMapper {
public String tableName = "user";
public String columnNoId = "ages, password";
@Insert("<script> " +
"insert into " + tableName +
"(" + columnNoId + ") " +
"values " +
"<foreach collection=\"items\" index=\"index\" item=\"item\" separator=\",\"> "
+
"(#{item.ages},#{item.password})"
+
"</foreach> " +
"</script>")
int batchSave(@Param("items") List<User> items);
}
Note: tableName,coulmNoId need to be defined by yourself
- shortcut tool
The generation of "(#{item.ages},#{item.password})" can use the following classes:
/**
* 描述:接口文档,帮助类
* @author uusao
* @create 2018-03-24 10:16
**/
public class TestOnlyByJunit4 {
/**
* 暴力反射,获取类的私有字段
*/
@Test
public void getProperties() {
System.out.println(getValues(User.class, false));
}
/**
* 多条数据查询 帮助类
*
* @param clazz
* @param containId
* @return
*/
public static String getValues(Class clazz, boolean containId) {
StringBuilder values = new StringBuilder();
values.append("\"(");
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//System.out.println(field.getName());
String propertyName = field.getName();
if (propertyName.equals("id")) {
if (!containId) { //不包含id
continue;
}
}
String value = "#{item." + propertyName + "}" + ",";
if (fields[fields.length - 1].getName().equals(propertyName)) {
value = "#{item." + propertyName + "}";
}
values.append(value);
}
values.append(")\"");
//System.out.println(values.toString());
return values.toString();
}
}
1.2 Using the annotation method (2)
/*UserDAO.java*/
public interface UserDAO {
@InsertProvider(type = UserDAOProvider.class, method = "insertAll")
void insertAll(@Param("list") List<User> users);
}
/*UserDAOProvider.java*/
public class UserDAOProvider {
public String insertAll(Map map) {
List<User> users = (List<User>) map.get("list");
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO User ");
sb.append("(id, name) ");
sb.append("VALUES ");
MessageFormat mf = new MessageFormat("(null, #'{'list[{0}].name})");
for (int i = 0; i < users.size(); i++) {
sb.append(mf.format(new Object[]{i}));
if (i < users.size() - 1) {
sb.append(",");
}
}
return sb.toString();
}
}
MyBatis will store the parameters of the List type in the insertAll method of UserDAO into a Map. The default key is "list". You can use @Param to annotate the custom name. MyBatis uses this map as a parameter when calling the method specified by @InsertProvide Pass in, use List users = (List) map.get("list"); in all codes to get the list parameter. It can be seen from the code that the generated SQL statement is roughly:
INSERT INTO User (id, name) VALUES (null, #{list[0].name}), (null, #{list[1].name})[,(null, #{list[i].name})]
1.3 Using xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.AttachmentTableMapper">
<insert id="insertByBatch" parameterType="java.util.List">
insert into attachment_table (name, logID,url)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.name,jdbcType=VARCHAR}, #{item.logid,jdbcType=INTEGER},#{item.url,jdbcType=LONGVARCHAR})
</foreach>
</insert>
</mapper>
2. Batch update Mapper writing (annotation form)
@Insert("<script> " +
"insert into " + tableName +
"(" + column+ ") " +
"values " +
"<foreach collection=\"items\" index=\"index\" item=\"item\" separator=\",\"> " +
"(#{item.id},#{item.watchCount},#{item.collectCount},#{item.activityId})" +
"</foreach> " +
"ON DUPLICATE KEY UPDATE " +
"id = VALUES(id)," +
"watch_count = VALUES(watch_count), " +
"collect_count = VALUES(collect_count), " +
"activity_id = VALUES(activity_id) " +
"</script>")
Integer batchUpdate(@Param("items") List<ActivityStatistics> items);