| | |
| | | package com.ruoyi.common.core.mybatisplus.methods; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.core.enums.SqlMethod; |
| | | import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; |
| | | import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; |
| | | import org.apache.ibatis.executor.keygen.KeyGenerator; |
| | | import org.apache.ibatis.executor.keygen.NoKeyGenerator; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlSource; |
| | | |
| | | import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | |
| | | /** |
| | | * 单sql批量插入 |
| | |
| | | final String sql = "<script>insert into %s %s values %s</script>"; |
| | | final String fieldSql = prepareFieldSql(tableInfo); |
| | | final String valueSql = prepareValuesSqlForMysqlBatch(tableInfo); |
| | | KeyGenerator keyGenerator = new NoKeyGenerator(); |
| | | SqlMethod sqlMethod = SqlMethod.INSERT_ONE; |
| | | String keyProperty = null; |
| | | String keyColumn = null; |
| | | // 表包含主键处理逻辑,如果不包含主键当普通字段处理 |
| | | if (StrUtil.isNotBlank(tableInfo.getKeyProperty())) { |
| | | if (tableInfo.getIdType() == IdType.AUTO) { |
| | | /** 自增主键 */ |
| | | keyGenerator = new Jdbc3KeyGenerator(); |
| | | keyProperty = tableInfo.getKeyProperty(); |
| | | keyColumn = tableInfo.getKeyColumn(); |
| | | } else { |
| | | if (null != tableInfo.getKeySequence()) { |
| | | keyGenerator = TableInfoHelper.genKeyGenerator(getMethod(sqlMethod), tableInfo, builderAssistant); |
| | | keyProperty = tableInfo.getKeyProperty(); |
| | | keyColumn = tableInfo.getKeyColumn(); |
| | | } |
| | | } |
| | | } |
| | | final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql); |
| | | SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass); |
| | | return this.addInsertMappedStatement(mapperClass, modelClass, "insertAll", sqlSource, new NoKeyGenerator(), null, null); |
| | | return this.addInsertMappedStatement(mapperClass, modelClass, "insertAll", sqlSource, keyGenerator, keyProperty, keyColumn); |
| | | } |
| | | |
| | | private String prepareFieldSql(TableInfo tableInfo) { |