| | |
| | | <dependency> |
| | | <groupId>com.baomidou</groupId> |
| | | <artifactId>mybatis-plus-boot-starter</artifactId> |
| | | <version>3.5.7</version> |
| | | <version>3.5.1</version> |
| | | </dependency> |
| | | <!--redis--> |
| | | <dependency> |
对比新文件 |
| | |
| | | package com.se.nsl.ext; |
| | | |
| | | import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| | | import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | |
| | | import java.util.List; |
| | | |
| | | @SuppressWarnings("ALL") |
| | | public class CustomizedSqlInjector extends DefaultSqlInjector { |
| | | @Override |
| | | public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) { |
| | | List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo); |
| | | methodList.add(new InsertBatchMethod()); |
| | | methodList.add(new UpdateBatchMethod()); |
| | | |
| | | return methodList; |
| | | } |
| | | } |
对比新文件 |
| | |
| | | package com.se.nsl.ext; |
| | | |
| | | import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| | | import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | import org.apache.ibatis.executor.keygen.NoKeyGenerator; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlSource; |
| | | |
| | | @SuppressWarnings("ALL") |
| | | public class InsertBatchMethod extends AbstractMethod { |
| | | @Override |
| | | public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { |
| | | final String sql = "<script>insert into %s %s values %s</script>"; |
| | | final String fieldSql = prepareFieldSql(tableInfo); |
| | | final String valueSql = prepareValuesSql(tableInfo); |
| | | final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql); |
| | | |
| | | SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass); |
| | | |
| | | return this.addInsertMappedStatement(mapperClass, modelClass, "insertBatch", sqlSource, new NoKeyGenerator(), null, null); |
| | | } |
| | | |
| | | private String prepareFieldSql(TableInfo tableInfo) { |
| | | StringBuilder fieldSql = new StringBuilder(); |
| | | // fieldSql.append(tableInfo.getKeyColumn()).append(",") |
| | | // tableInfo.getFieldList().forEach(x -> fieldSql.append(x.getColumn()).append(",")) |
| | | for (TableFieldInfo f : tableInfo.getFieldList()) { |
| | | if (StaticData.INSERT_EXCLUDE_FIELDS.contains(f.getProperty())) { |
| | | continue; |
| | | } |
| | | |
| | | fieldSql.append(f.getColumn()).append(","); |
| | | } |
| | | |
| | | fieldSql.delete(fieldSql.length() - 1, fieldSql.length()); |
| | | fieldSql.insert(0, "("); |
| | | fieldSql.append(")"); |
| | | |
| | | return fieldSql.toString(); |
| | | } |
| | | |
| | | private String prepareValuesSql(TableInfo tableInfo) { |
| | | final StringBuilder valueSql = new StringBuilder(); |
| | | valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">"); |
| | | // valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},") |
| | | // tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},")) |
| | | for (TableFieldInfo f : tableInfo.getFieldList()) { |
| | | if (StaticData.INSERT_EXCLUDE_FIELDS.contains(f.getProperty())) { |
| | | continue; |
| | | } |
| | | |
| | | valueSql.append("geom".equals(f.getProperty()) ? "${item." : "#{item.").append(f.getProperty()).append("},"); |
| | | } |
| | | |
| | | valueSql.delete(valueSql.length() - 1, valueSql.length()); |
| | | valueSql.append("</foreach>"); |
| | | |
| | | return valueSql.toString(); |
| | | } |
| | | } |
对比新文件 |
| | |
| | | package com.se.nsl.ext; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @SuppressWarnings("ALL") |
| | | public class StaticData { |
| | | public static final int I200 = 200; |
| | | |
| | | public final static List<String> INSERT_EXCLUDE_FIELDS = new ArrayList<>(Arrays.asList("id", "gid", "objectid", "updateUser", "updateTime", "shape_leng", "shape_area", "serialVersionUID", "dirName", "depName", "createUserName", "updateUserName")); |
| | | |
| | | public final static List<String> UPDATE_EXCLUDE_FIELDS = new ArrayList<>(Arrays.asList("objectid", "createUser", "createTime", "shape_leng", "shape_area", "serialVersionUID", "dirName", "depName", "createUserName", "updateUserName")); |
| | | } |
对比新文件 |
| | |
| | | package com.se.nsl.ext; |
| | | |
| | | import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| | | import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlSource; |
| | | |
| | | @SuppressWarnings("ALL") |
| | | public class UpdateBatchMethod extends AbstractMethod { |
| | | @Override |
| | | public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { |
| | | String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>"; |
| | | String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true); |
| | | //String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item."); |
| | | String setSql = getSqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item."); |
| | | String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional); |
| | | |
| | | // update %s %s where %s=#{%s} %s |
| | | // update tab set a=#{a} where gid=1 |
| | | |
| | | SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass); |
| | | |
| | | return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource); |
| | | } |
| | | |
| | | private String getSqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias, final String prefix) { |
| | | // String sqlScript = table.getAllSqlSet(logic, prefix); |
| | | String sqlScript = getSqlSet(table); |
| | | if (judgeAliasNull) { |
| | | sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true); |
| | | } |
| | | if (ew) { |
| | | sqlScript = sqlScript + "\n"; |
| | | sqlScript = sqlScript + this.convertIfEwParam("ew.sqlSet", false); |
| | | } |
| | | |
| | | sqlScript = SqlScriptUtils.convertSet(sqlScript); |
| | | |
| | | return sqlScript; |
| | | } |
| | | |
| | | private String getSqlSet(TableInfo tableInfo) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (TableFieldInfo f : tableInfo.getFieldList()) { |
| | | if (StaticData.UPDATE_EXCLUDE_FIELDS.contains(f.getProperty())) { |
| | | continue; |
| | | } |
| | | |
| | | if ("geom".equals(f.getProperty())) { |
| | | sb.append("<if test=\"item['geom'] != null\">geom=${item.geom},</if>\n"); |
| | | continue; |
| | | } |
| | | |
| | | sb.append(String.format("<if test=\"item['%s'] != null\">%s=#{item.%s},</if>\n", f.getProperty(), f.getColumn(), f.getProperty())); |
| | | } |
| | | sb.deleteCharAt(sb.length() - 1); |
| | | |
| | | return sb.toString(); |
| | | } |
| | | } |
| | |
| | | * @return 鍒犻櫎鎴愬姛鐨勮褰曟暟 |
| | | */ |
| | | public int deleteByIds(List<Integer> ids) { |
| | | return regionMapper.deleteByIds(ids); |
| | | return regionMapper.deleteBatchIds(ids); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 鍒犻櫎鎴愬姛鐨勮褰曟暟 |
| | | */ |
| | | public int deleteByIds(List<Integer> ids) { |
| | | return simuMapper.deleteByIds(ids); |
| | | return simuMapper.deleteBatchIds(ids); |
| | | } |
| | | |
| | | /** |