TableInfo { public void importPackage(StrategyConfig strategyConfig, GlobalConfig globalConfig) { boolean importSerializable = true; if (StringUtils.isNotBlank(strategyConfig.getSuperEntityClass())) { importSerializable = false; this.importPackages.add(strategyConfig.getSuperEntityClass()); } else { if (globalConfig.isActiveRecord()) { this.getImportPackages().add(Model.class.getCanonicalName()); importSerializable = false; } } if (importSerializable) { this.importPackages.add(Serializable.class.getCanonicalName()); } if (this.isConvert()) { this.importPackages.add(TableName.class.getCanonicalName()); } if (strategyConfig.getLogicDeleteFieldName() != null && this.isLogicDelete(strategyConfig.getLogicDeleteFieldName())) { this.importPackages.add(TableLogic.class.getCanonicalName()); } if (null != globalConfig.getIdType() && this.isHavePrimaryKey()) { this.importPackages.add(IdType.class.getCanonicalName()); this.importPackages.add(TableId.class.getCanonicalName()); } this.fields.forEach(field -> { if (null != field.getColumnType() && null != field.getColumnType().getPkg()) { importPackages.add(field.getColumnType().getPkg()); } if (field.isKeyFlag()) { if (field.isConvert() || field.isKeyIdentityFlag()) { importPackages.add(TableId.class.getCanonicalName()); } if (field.isKeyIdentityFlag()) { importPackages.add(IdType.class.getCanonicalName()); } } else if (field.isConvert()) { importPackages.add(com.baomidou.mybatisplus.annotation.TableField.class.getCanonicalName()); } if (null != field.getFill()) { importPackages.add(com.baomidou.mybatisplus.annotation.TableField.class.getCanonicalName()); importPackages.add(FieldFill.class.getCanonicalName()); } String versionFieldName = strategyConfig.getVersionFieldName(); if (StringUtils.isNotBlank(versionFieldName) && versionFieldName.equals(field.getName())) { this.importPackages.add(Version.class.getCanonicalName()); } }); }  @Deprecated TableInfo setConvert(boolean convert); String getEntityPath(); @Deprecated TableInfo setEntityName(String entityName); TableInfo setEntityName(StrategyConfig strategyConfig, String entityName); @Deprecated TableInfo setFields(List<TableField> fields); TableInfo addFields(List<TableField> fields); TableInfo addFields(TableField... fields); @Deprecated TableInfo setImportPackages(String pkg); TableInfo addImportPackages(String... pkgs); boolean isLogicDelete(String logicDeletePropertyName); @Deprecated TableInfo setFieldNames(String fieldNames); String getFieldNames(); @Deprecated TableInfo setCommonFields(List<TableField> commonFields); TableInfo addCommonFields(TableField... commonFields); TableInfo addCommonFields(List<TableField> commonFields); void importPackage(StrategyConfig strategyConfig, GlobalConfig globalConfig); void processTable(StrategyConfig strategyConfig, GlobalConfig globalConfig); String getFileName(String entityName, String value, Supplier<String> defaultValue);  }
@Test void importPackageTest() { TableInfo tableInfo; StrategyConfig strategyConfig; tableInfo = new TableInfo().setName("user"); tableInfo.importPackage(new StrategyConfig(), new GlobalConfig()); Assertions.assertEquals(1, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); tableInfo = new TableInfo().setName("user").setConvert(true); tableInfo.importPackage(new StrategyConfig(), new GlobalConfig()); Assertions.assertEquals(2, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableName.class.getName())); tableInfo = new TableInfo().setName("user"); strategyConfig = new StrategyConfig(); tableInfo.importPackage(strategyConfig.setSuperEntityClass("con.baomihua.demo.SuperEntity"), new GlobalConfig()); Assertions.assertEquals(1, tableInfo.getImportPackages().size()); Assertions.assertFalse(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains("con.baomihua.demo.SuperEntity")); tableInfo = new TableInfo().setName("user"); strategyConfig = new StrategyConfig(); tableInfo.importPackage(strategyConfig, new GlobalConfig().setActiveRecord(true)); Assertions.assertEquals(1, tableInfo.getImportPackages().size()); Assertions.assertFalse(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(Model.class.getName())); strategyConfig = new StrategyConfig(); tableInfo = new TableInfo().setName("user"); tableInfo.addFields(new TableField().setName("u_id").setPropertyName(strategyConfig, "uid").setColumnType(DbColumnType.LONG).setKeyFlag(true)); tableInfo.importPackage(strategyConfig, new GlobalConfig()); Assertions.assertEquals(2, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableId.class.getName())); strategyConfig = new StrategyConfig(); tableInfo = new TableInfo().setName("user"); tableInfo.addFields(new TableField().setName("u_id").setPropertyName(strategyConfig, "uid").setColumnType(DbColumnType.LONG).setKeyFlag(true).setKeyIdentityFlag(true)); tableInfo.importPackage(strategyConfig, new GlobalConfig()); Assertions.assertEquals(3, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableId.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(IdType.class.getName())); strategyConfig = new StrategyConfig().setLogicDeleteFieldName("delete_flag"); tableInfo = new TableInfo().setName("user"); tableInfo.addFields(new TableField().setName("u_id").setPropertyName(strategyConfig, "uid").setColumnType(DbColumnType.LONG).setKeyFlag(true)); tableInfo.addFields(new TableField().setName("delete_flag").setPropertyName(strategyConfig, "deleteFlag").setColumnType(DbColumnType.BOOLEAN)); tableInfo.importPackage(strategyConfig, new GlobalConfig()); Assertions.assertEquals(4, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(com.baomidou.mybatisplus.annotation.TableField.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableLogic.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableId.class.getName())); tableInfo = new TableInfo().setName("user"); tableInfo.addFields(new TableField().setName("name").setPropertyName(strategyConfig, "name").setColumnType(DbColumnType.STRING)); tableInfo.importPackage(new StrategyConfig(), new GlobalConfig().setIdType(IdType.ASSIGN_ID)); Assertions.assertEquals(1, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); tableInfo = new TableInfo().setName("user").setHavePrimaryKey(true); tableInfo.addFields(new TableField().setName("u_id").setPropertyName(strategyConfig, "uid").setColumnType(DbColumnType.LONG).setKeyFlag(true)); tableInfo.importPackage(new StrategyConfig(), new GlobalConfig().setIdType(IdType.ASSIGN_ID)); Assertions.assertEquals(3, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableId.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(IdType.class.getName())); tableInfo = new TableInfo().setName("user").setHavePrimaryKey(true); tableInfo.addFields(new TableField().setName("u_id").setPropertyName(strategyConfig, "uid").setColumnType(DbColumnType.LONG).setKeyFlag(true)); tableInfo.addFields(new TableField().setName("create_time").setPropertyName(strategyConfig, "createTime").setColumnType(DbColumnType.DATE).setFill(FieldFill.DEFAULT.name())); tableInfo.importPackage(new StrategyConfig().addTableFills(new TableFill("createTime", FieldFill.DEFAULT)), new GlobalConfig()); Assertions.assertEquals(5, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Date.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableId.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(com.baomidou.mybatisplus.annotation.TableField.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(FieldFill.class.getName())); tableInfo = new TableInfo().setName("user").setHavePrimaryKey(true); tableInfo.addFields(new TableField().setName("u_id").setPropertyName(strategyConfig, "uid").setColumnType(DbColumnType.LONG).setKeyFlag(true)); tableInfo.addFields(new TableField().setName("version").setPropertyName(strategyConfig, "version").setColumnType(DbColumnType.LONG)); tableInfo.importPackage(new StrategyConfig().setVersionFieldName("version"), new GlobalConfig()); Assertions.assertEquals(3, tableInfo.getImportPackages().size()); Assertions.assertTrue(tableInfo.getImportPackages().contains(Serializable.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(TableId.class.getName())); Assertions.assertTrue(tableInfo.getImportPackages().contains(Version.class.getName())); }