¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.smartearth.poiexcel.config; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.DbType; |
| | | import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; |
| | | import com.baomidou.mybatisplus.core.MybatisConfiguration; |
| | | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; |
| | | import com.smartearth.poiexcel.extend.CustomizedSqlInjector; |
| | | import org.apache.ibatis.type.JdbcType; |
| | | import org.mybatis.spring.annotation.MapperScan; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.transaction.annotation.EnableTransactionManagement; |
| | | |
| | | /** |
| | | * Mybatis-Pluså页é
ç½® |
| | | * @author WWW |
| | | */ |
| | | @EnableTransactionManagement |
| | | @Configuration |
| | | @MapperScan("com.smartearth.poiexcel.mapper") |
| | | public class MybatisPlusConfig { |
| | | /** |
| | | * æ°çå页æä»¶,ä¸ç¼åäºç¼éµå¾ªmybatisçè§å,éè¦è®¾ç½® |
| | | * MybatisConfiguration#useDeprecatedExecutor = false |
| | | * é¿å
ç¼ååºç°é®é¢(è¯¥å±æ§ä¼å¨æ§æä»¶ç§»é¤åä¸åç§»é¤) |
| | | */ |
| | | @Bean |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL)); |
| | | return interceptor; |
| | | } |
| | | |
| | | @Bean |
| | | public ConfigurationCustomizer configurationCustomizer() { |
| | | return new ConfigurationCustomizer() { |
| | | @Override |
| | | public void customize(MybatisConfiguration configuration) { |
| | | configuration.setCacheEnabled(true); |
| | | configuration.setMapUnderscoreToCamelCase(true); |
| | | configuration.setCallSettersOnNulls(true); |
| | | configuration.setJdbcTypeForNull(JdbcType.NULL); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | @Bean |
| | | public CustomizedSqlInjector customizedSqlInjector() { |
| | | return new CustomizedSqlInjector(); |
| | | } |
| | | } |
| | |
| | | package com.smartearth.poiexcel.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | |
| | | * @author WWW |
| | | * @date 2023-10-05 |
| | | */ |
| | | @Data |
| | | @AllArgsConstructor |
| | | @TableName("smart_earth.yz_qiyexinxi") |
| | | @EqualsAndHashCode(callSuper = false) |
| | | public class EntEntity implements Serializable { |
| | | private static final long serialVersionUID = -8624235184539814990L; |
| | | |
| | | private Long id; |
| | | |
| | | private String enterprise_id; |
| | | |
| | | private String entName; |
| | | |
| | |
| | | public EntEntity() { |
| | | } |
| | | |
| | | public EntEntity(String enterprise_id, String entName, String fingerId, Integer regCapital, Integer regCapitalCNY, String legalPerson, String address, Date buildDate, String entType, String industryCategory, String businessScope, String qylabel, String creator, Double x, Double y, String enterprise_area) { |
| | | this.enterprise_id = enterprise_id; |
| | | public EntEntity(String entName, String fingerId, Integer regCapital, Integer regCapitalCNY, String legalPerson, String address, Date buildDate, String entType, String industryCategory, String businessScope, String qylabel, String creator, Double x, Double y, String enterprise_area) { |
| | | this.entName = entName; |
| | | this.fingerId = fingerId; |
| | | this.regCapital = regCapital; |
| | |
| | | |
| | | public void setId(Long id) { |
| | | this.id = id; |
| | | } |
| | | |
| | | public String getEnterprise_id() { |
| | | return enterprise_id; |
| | | } |
| | | |
| | | public void setEnterprise_id(String enterprise_id) { |
| | | this.enterprise_id = enterprise_id; |
| | | } |
| | | |
| | | public String getEntName() { |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.smartearth.poiexcel.extend; |
| | | |
| | | 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; |
| | | |
| | | /** |
| | | * èªå®ä¹æ¹æ³SQL注å
¥å¨ |
| | | * @author WWW |
| | | */ |
| | | public class CustomizedSqlInjector extends DefaultSqlInjector { |
| | | /** |
| | | * 妿åªéå¢å æ¹æ³ï¼ä¿çmybatis plusèªå¸¦æ¹æ³ï¼ |
| | | * å¯ä»¥å
è·åsuper.getMethodList()ï¼åæ·»å add |
| | | */ |
| | | @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.smartearth.poiexcel.extend; |
| | | |
| | | import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| | | import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | import com.smartearth.poiexcel.entity.StaticData; |
| | | import org.apache.ibatis.executor.keygen.NoKeyGenerator; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlSource; |
| | | |
| | | /** |
| | | * æ¹éæå
¥æ¹æ³ |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class InsertBatchMethod extends AbstractMethod { |
| | | /** |
| | | * insert into user(id, name, age) values (1, "a", 17), (2, "b", 18); |
| | | * <script> |
| | | * insert into user(id, name, age) values |
| | | * <foreach collection="list" item="item" index="index" open="(" separator="),(" close=")"> |
| | | * #{item.id}, #{item.name}, #{item.age} |
| | | * </foreach> |
| | | * </script> |
| | | */ |
| | | @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); |
| | | |
| | | // 第ä¸ä¸ªåæ°å¿
é¡»åRootMapperçèªå®ä¹æ¹æ³åä¸è´ |
| | | 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.smartearth.poiexcel.extend; |
| | | |
| | | 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 com.smartearth.poiexcel.entity.StaticData; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlSource; |
| | | |
| | | /** |
| | | * æ¹éæ´æ°æ¹æ³ |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class UpdateBatchMethod extends AbstractMethod { |
| | | /** |
| | | * update user set name = "a", age = 17 where id = 1; |
| | | * update user set name = "b", age = 18 where id = 2; |
| | | * <script> |
| | | * <foreach collection="list" item="item" separator=";"> |
| | | * update user |
| | | * <set> |
| | | * <if test="item.name != null and item.name != ''"> |
| | | * name = #{item.name,jdbcType=VARCHAR}, |
| | | * </if> |
| | | * <if test="item.age != null"> |
| | | * age = #{item.age,jdbcType=INTEGER}, |
| | | * </if> |
| | | * </set> |
| | | * where id = #{item.id,jdbcType=INTEGER} |
| | | * </foreach> |
| | | * </script> |
| | | */ |
| | | @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); |
| | | |
| | | // 第ä¸ä¸ªåæ°å¿
é¡»åRootMapperçèªå®ä¹æ¹æ³åä¸è´ |
| | | 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(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.smartearth.poiexcel.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import org.apache.ibatis.annotations.Param; |
| | | import org.apache.ibatis.annotations.Select; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * åºç¡Mapper |
| | | * @author WWW |
| | | * @param <T> æ³å |
| | | */ |
| | | public interface BasicMapper<T> extends BaseMapper<T> { |
| | | /** |
| | | * æ¨¡ç³æç´¢åæ®µ |
| | | * |
| | | * @param tab 表å |
| | | * @param field åæ®µ |
| | | * @param value å¼ |
| | | * @return ç»æå表 |
| | | */ |
| | | @Select("select ${field} from ${tab} where upper(${field}) like #{value} order by ${field} limit 10") |
| | | public List<String> selectFieldFuzzy(@Param("tab") String tab, @Param("field") String field, @Param("value") String value); |
| | | |
| | | /** |
| | | * èªå®ä¹æ¹éæå
¥ |
| | | * 妿è¦èªå¨å¡«å
ï¼@Param(xx) xxåæ°åå¿
é¡»æ¯ list/collection/array 3个çå
¶ä¸ä¹ä¸ |
| | | * |
| | | * @param list |
| | | * @return |
| | | */ |
| | | public int insertBatch(@Param("list") List<T> list); |
| | | |
| | | /** |
| | | * èªå®ä¹æ¹éæ´æ°ï¼æ¡ä»¶ä¸ºä¸»é® |
| | | * 妿è¦èªå¨å¡«å
ï¼@Param(xx) xxåæ°åå¿
é¡»æ¯ list/collection/array 3个çå
¶ä¸ä¹ä¸ |
| | | * |
| | | * @param list |
| | | * @return |
| | | */ |
| | | public int updateBatch(@Param("list") List<T> list); |
| | | } |
| | |
| | | package com.smartearth.poiexcel.mapper; |
| | | |
| | | import com.smartearth.poiexcel.entity.EntEntity; |
| | | import org.apache.ibatis.annotations.Mapper; |
| | | import org.springframework.stereotype.Repository; |
| | | |
| | |
| | | */ |
| | | @Mapper |
| | | @Repository |
| | | public interface EntMapper { |
| | | |
| | | public interface EntMapper extends BasicMapper<EntEntity> { |
| | | } |
| | |
| | | * @date 2023-10-05 |
| | | */ |
| | | @Service |
| | | public class EntService implements EntMapper { |
| | | public class EntService { |
| | | @Resource |
| | | EntMapper entMapper; |
| | | |