MybatisGenerator外掛開發四【批量插入】
批量插入和分頁查詢一樣,在專案中基本也是不可少的。這裡我們要給Mapper新增兩個方法:batchInsert
和batchInsertSelective
,區別是前者全欄位插入,後者只插入給定的欄位。原來的單條insertSelective
是通過物件的值是不是Null來實現的,我們也可以取插入記錄的第0條來實現batchInsertSelective
。但是,始終感覺有點不對,所以採取了另一種方式,顯式地傳遞要插入地欄位來實現。這裡就會用到了上一章節寫入到Model中的表字段枚舉了。
下面直接上程式碼。
修改Mapper介面和SqlMap檔案
public class BatchInsertPluginextends PluginAdapter{ @Override public boolean validate(List<String> list){ return true; } /** * 修改Mapper類 */ @Override public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable){ addBatchInsertMethod(interfaze, introspectedTable); return true; } private void addBatchInsertMethod(Interface interfaze, IntrospectedTable introspectedTable){ // 設定需要import的類 Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>(); importedTypes.add(FullyQualifiedJavaType.getNewListInstance()); importedTypes.add(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType())); FullyQualifiedJavaType ibsreturnType = FullyQualifiedJavaType.getIntInstance(); Method batchInsertMethod = new Method(); // 1.設定方法可見性 batchInsertMethod.setVisibility(JavaVisibility.PUBLIC); // 2.設定返回值型別 int型別 batchInsertMethod.setReturnType(ibsreturnType); // 3.設定方法名 batchInsertMethod.setName("batchInsert"); // 4.設定引數列表 FullyQualifiedJavaType paramType = FullyQualifiedJavaType.getNewListInstance(); FullyQualifiedJavaType paramListType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); paramType.addTypeArgument(paramListType); batchInsertMethod.addParameter(new Parameter(paramType, "records")); interfaze.addImportedTypes(importedTypes); interfaze.addMethod(batchInsertMethod); Method batchInsertSelectiveMethod = new Method(); // 1.設定方法可見性 batchInsertSelectiveMethod.setVisibility(JavaVisibility.PUBLIC); // 2.設定返回值型別 int型別 batchInsertSelectiveMethod.setReturnType(ibsreturnType); // 3.設定方法名 batchInsertSelectiveMethod.setName("batchInsertSelective"); // 4.設定引數列表 FullyQualifiedJavaType paramTypeSelective = FullyQualifiedJavaType.getNewListInstance(); FullyQualifiedJavaType paramListTypeSelective = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); paramTypeSelective.addTypeArgument(paramListTypeSelective); batchInsertSelectiveMethod.addParameter(new Parameter(paramTypeSelective, "records", "@Param(\"records\")")); batchInsertSelectiveMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "columns", "@Param(\"columns\")", true)); interfaze.addImportedTypes(importedTypes); interfaze.addMethod(batchInsertSelectiveMethod); } /** * 修改Mapper.xml */ @Override public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable){ addBatchInsertXml(document, introspectedTable); addBatchInsertSelectiveXml(document, introspectedTable); return true; } private void addBatchInsertXml(Document document, IntrospectedTable introspectedTable){ // <insert ... XmlElement insertBatchElement = new XmlElement("insert"); insertBatchElement.addAttribute(new Attribute("id", "batchInsert")); insertBatchElement.addAttribute(new Attribute("parameterType", "java.util.List")); XmlElement valueTrimElement = new XmlElement("trim"); valueTrimElement.addAttribute(new Attribute("prefix", " (")); valueTrimElement.addAttribute(new Attribute("suffix", ")")); valueTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); XmlElement columnTrimElement = new XmlElement("trim"); columnTrimElement.addAttribute(new Attribute("prefix", "(")); columnTrimElement.addAttribute(new Attribute("suffix", ")")); columnTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); List<IntrospectedColumn> columns = introspectedTable.getAllColumns(); for (IntrospectedColumn introspectedColumn : columns) { String columnName = introspectedColumn.getActualColumnName(); columnTrimElement.addElement(new TextElement(columnName+",")); valueTrimElement.addElement(new TextElement("#{item." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},")); } XmlElement foreachElement = new XmlElement("foreach"); foreachElement.addAttribute(new Attribute("collection", "list")); foreachElement.addAttribute(new Attribute("index", "index")); foreachElement.addAttribute(new Attribute("item", "item")); foreachElement.addAttribute(new Attribute("separator", ",")); insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())); insertBatchElement.addElement(columnTrimElement); insertBatchElement.addElement(new TextElement(" values ")); foreachElement.addElement(valueTrimElement); insertBatchElement.addElement(foreachElement); document.getRootElement().addElement(insertBatchElement); } private void addBatchInsertSelectiveXml(Document document, IntrospectedTable introspectedTable){ // <insert ... XmlElement insertBatchElement = new XmlElement("insert"); insertBatchElement.addAttribute(new Attribute("id", "batchInsertSelective")); insertBatchElement.addAttribute(new Attribute("parameterType", "map")); XmlElement foreachColumn = new XmlElement("foreach"); foreachColumn.addAttribute(new Attribute("collection", "columns")); foreachColumn.addAttribute(new Attribute("index", "index")); foreachColumn.addAttribute(new Attribute("item", "item")); foreachColumn.addAttribute(new Attribute("separator", ",")); foreachColumn.addAttribute(new Attribute("open", "(")); foreachColumn.addAttribute(new Attribute("close", ")")); foreachColumn.addElement(new TextElement("${item}")); insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())); insertBatchElement.addElement(foreachColumn); insertBatchElement.addElement(new TextElement(" values ")); XmlElement valueTrimElement = new XmlElement("trim"); valueTrimElement.addAttribute(new Attribute("prefix", " (")); valueTrimElement.addAttribute(new Attribute("suffix", ")")); valueTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); XmlElement foreachColumnForValue = new XmlElement("foreach"); foreachColumnForValue.addAttribute(new Attribute("collection", "columns")); foreachColumnForValue.addAttribute(new Attribute("index", "index")); foreachColumnForValue.addAttribute(new Attribute("item", "column")); List<IntrospectedColumn> columns = introspectedTable.getAllColumns(); for (IntrospectedColumn introspectedColumn : columns) { String columnName = introspectedColumn.getActualColumnName(); XmlElement check = new XmlElement("if"); check.addAttribute(new Attribute("test", "'" + columnName + "' == column")); check.addElement(new TextElement("#{record." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},")); foreachColumnForValue.addElement(check); } valueTrimElement.addElement(foreachColumnForValue); XmlElement foreachElement = new XmlElement("foreach"); foreachElement.addAttribute(new Attribute("collection", "records")); foreachElement.addAttribute(new Attribute("index", "index")); foreachElement.addAttribute(new Attribute("item", "record")); foreachElement.addAttribute(new Attribute("separator", ",")); foreachElement.addElement(valueTrimElement); insertBatchElement.addElement(foreachElement); document.getRootElement().addElement(insertBatchElement); } }
上述程式碼在Mapper介面和SqlMap檔案中添加了batchInsert
和batchInsertSelective
的支援。
檢視生成檔案
將外掛新增到專案中,並且執行完MybatisGenerator,我們來檢視下生成的相關檔案:
public interface AdministratorMapper{ ... int batchInsert(List<Administrator> records); int batchInsertSelective(@Param("records")List<Administrator> records, @Param("columns")String ... columns); ... }
... <insertid="batchInsert"parameterType="java.util.List"> insert into administrator <trimprefix="("suffix=")"suffixOverrides=","> id, mobile, pwd, role, deleted, create_time, update_time, </trim> values <foreachcollection="list"index="index"item="item"separator=","> <trimprefix=" ("suffix=")"suffixOverrides=","> #{item.id,jdbcType=BIGINT}, #{item.mobile,jdbcType=VARCHAR}, #{item.pwd,jdbcType=VARCHAR}, #{item.role,jdbcType=TINYINT}, #{item.deleted,jdbcType=INTEGER}, #{item.createTime,jdbcType=BIGINT}, #{item.updateTime,jdbcType=BIGINT}, </trim> </foreach> </insert> <insertid="batchInsertSelective"parameterType="map"> insert into administrator <foreachclose=")"collection="columns"index="index"item="item"open="("separator=","> ${item} </foreach> values <foreachcollection="records"index="index"item="record"separator=","> <trimprefix=" ("suffix=")"suffixOverrides=","> <foreachcollection="columns"index="index"item="column"> <iftest="'id' == column"> #{record.id,jdbcType=BIGINT}, </if> <iftest="'mobile' == column"> #{record.mobile,jdbcType=VARCHAR}, </if> <iftest="'pwd' == column"> #{record.pwd,jdbcType=VARCHAR}, </if> <iftest="'role' == column"> #{record.role,jdbcType=TINYINT}, </if> <iftest="'deleted' == column"> #{record.deleted,jdbcType=INTEGER}, </if> <iftest="'create_time' == column"> #{record.createTime,jdbcType=BIGINT}, </if> <iftest="'update_time' == column"> #{record.updateTime,jdbcType=BIGINT}, </if> </foreach> </trim> </foreach> </insert> ...
如何使用
使用方式如下:
administratorMapper.batchInsertSelective( list, Administrator.COLUMNS.ID.getColumn(), Administrator.COLUMNS.MOBILE.getColumn() );
本章節就到這裡了。