提交 2df01cfe 编写于 作者: Tyl666's avatar Tyl666

【Liquibase逆向工程】第一版代码提交

主要功能点:
1、支持自定义数据源来同步实体
2、支持自定义构建liquibase
上级 8938094e
......@@ -25,7 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
@SpringBootApplication(exclude = {
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure.class
})
@ComponentScan(basePackages = {"cn.ibizlab"}
@ComponentScan(basePackages = {"cn.ibizlab","liquibase.integration.spring"}
// ,excludeFilters = {
// @ComponentScan.Filter(type= org.springframework.context.annotation.FilterType.REGEX, pattern="cn.ibizlab.xxx.rest.xxx"),
// }
......
......@@ -2,9 +2,13 @@ package cn.ibizlab.core.lite.extensions.util;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.extensions.service.LiteModelService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class LiteStorage {
......@@ -86,5 +90,14 @@ public class LiteStorage {
}
}
}
public static Map parseDatasource (String json) {
JSONArray tableData = JSONArray.parseArray(json);
Map map = new HashMap();
for (int i = 0; i < tableData.size(); i++) {
JSONObject jsonObject = tableData.getJSONObject(i);
map.put(jsonObject.get("param"), jsonObject.get("value"));
}
return map;
}
}
package cn.ibizlab.core.lite.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;
import cn.ibizlab.core.lite.extensions.domain.SysModel;
import cn.ibizlab.core.lite.extensions.service.LiteCoreService;
import liquibase.integration.spring.GenerateLiquibaseChangeLog;
import cn.ibizlab.util.domain.LiquibaseProp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.core.lite.domain.DstDataSource;
......@@ -30,16 +23,16 @@ import cn.ibizlab.core.lite.filter.DstDataSourceSearchContext;
import cn.ibizlab.core.lite.service.IDstDataSourceService;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.helper.DEFieldCacheMap;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ibizlab.core.lite.mapper.DstDataSourceMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import static cn.ibizlab.core.lite.extensions.util.LiteStorage.parseDatasource;
/**
* 实体[数据源] 服务对象接口实现
......@@ -53,6 +46,11 @@ public class DstDataSourceServiceImpl extends ServiceImpl<DstDataSourceMapper, D
protected cn.ibizlab.core.lite.service.IMetaEntityService metaentityService;
protected int batchSize = 500;
@Resource
public LiteCoreService liteCoreService;
@Resource
public GenerateLiquibaseChangeLog generateLiquibaseChangeLog;
@Override
@Transactional
......@@ -131,7 +129,20 @@ public class DstDataSourceServiceImpl extends ServiceImpl<DstDataSourceMapper, D
@Override
@Transactional
public DstDataSource initDS(DstDataSource et) {
//自定义代码
DstDataSource ds = get(et.getDsId());
String datasourceMessage = ds.getDsCfg();
Map datasourceMap = parseDatasource(datasourceMessage);
LiquibaseProp lp = new LiquibaseProp();
lp.setUsername(datasourceMap.get("username") == null?"":datasourceMap.get("username").toString());
lp.setPassword(datasourceMap.get("password") == null?"":datasourceMap.get("password").toString());
lp.setUrl(datasourceMap.get("url") == null?"":datasourceMap.get("url").toString());
lp.setDefaultSchema(datasourceMap.get("defaultSchema") == null?"":datasourceMap.get("defaultSchema").toString());
// 调用liquibase逆向工程的逻辑,生成一个xml,并返回一个SysModel对象
SysModel sysModel = generateLiquibaseChangeLog.liquibaseGenerateChangeLog(lp,ds.getDsId());
liteCoreService.syncSysModel(sysModel);
return et;
}
......
package cn.ibizlab.core.util.config;
import cn.ibizlab.util.domain.LiquibaseProp;
import com.alibaba.druid.pool.DruidDataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LiquibaseGenerateConfiguration {
@Bean
public SpringLiquibase masterliquibase(LiquibaseProp masterProperties) {
return getLiquibase(masterProperties.getUsername(),masterProperties.getPassword(),masterProperties.getUrl(),masterProperties.getDefaultSchema());
}
public SpringLiquibase getLiquibase(String username, String password, String url, String defaultSchema) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setUrl(url);
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(druidDataSource);
liquibase.setChangeLog("classpath:liquibase/empty.xml");
liquibase.setContexts("development,test,production");
liquibase.setShouldRun(true);
liquibase.setDefaultSchema(defaultSchema);
return liquibase;
}
}
......@@ -88,6 +88,7 @@
<oracle.version>11.2.0.3</oracle.version>
<postgresql.version>42.2.6</postgresql.version>
<hutool.version>5.5.1</hutool.version>
</properties>
......@@ -325,12 +326,18 @@
<version>${eureka-client.version}</version>
</dependency>
<!-- Oracle驱动包 -->
<!-- &lt;!&ndash; Oracle驱动包 &ndash;&gt;
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.version}</version>
</dependency>-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- PostgreSQL驱动包 -->
<dependency>
<groupId>org.postgresql</groupId>
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册