Commit 9884a790 by gxz

解决了使用非Mongo 仍然加载相应依赖的问题

parent eb6f167c
...@@ -13,6 +13,7 @@ import io.renren.utils.RRException; ...@@ -13,6 +13,7 @@ import io.renren.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
...@@ -33,13 +34,12 @@ public class DbConfig { ...@@ -33,13 +34,12 @@ public class DbConfig {
private SQLServerGeneratorDao sqlServerGeneratorDao; private SQLServerGeneratorDao sqlServerGeneratorDao;
@Autowired @Autowired
private PostgreSQLGeneratorDao postgreSQLGeneratorDao; private PostgreSQLGeneratorDao postgreSQLGeneratorDao;
@Autowired
private MongoDBGeneratorDao mongoDBGeneratorDao;
private static boolean mongo = false; private static boolean mongo = false;
@Bean @Bean
@Primary @Primary
@Conditional(MongoNullCondition.class)
public GeneratorDao getGeneratorDao() { public GeneratorDao getGeneratorDao() {
if ("mysql".equalsIgnoreCase(database)) { if ("mysql".equalsIgnoreCase(database)) {
return mySQLGeneratorDao; return mySQLGeneratorDao;
...@@ -49,14 +49,19 @@ public class DbConfig { ...@@ -49,14 +49,19 @@ public class DbConfig {
return sqlServerGeneratorDao; return sqlServerGeneratorDao;
} else if ("postgresql".equalsIgnoreCase(database)) { } else if ("postgresql".equalsIgnoreCase(database)) {
return postgreSQLGeneratorDao; return postgreSQLGeneratorDao;
} else if ("mongodb".equalsIgnoreCase(database)) {
mongo = true;
return mongoDBGeneratorDao;
} else { } else {
throw new RRException("不支持当前数据库:" + database); throw new RRException("不支持当前数据库:" + database);
} }
} }
@Bean
@Primary
@Conditional(MongoCondition.class)
public GeneratorDao getMongoDBDao(MongoDBGeneratorDao mongoDBGeneratorDao) {
mongo = true;
return mongoDBGeneratorDao;
}
public static boolean isMongo() { public static boolean isMongo() {
return mongo; return mongo;
} }
......
...@@ -12,7 +12,7 @@ public class MongoCondition implements Condition { ...@@ -12,7 +12,7 @@ public class MongoCondition implements Condition {
@Override @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String mongodb = context.getEnvironment().getProperty("mongodb.host"); String database = context.getEnvironment().getProperty("renren.database");
return StringUtils.isNotEmpty(mongodb); return "mongodb".equalsIgnoreCase(database);
} }
} }
...@@ -5,6 +5,9 @@ import com.mongodb.MongoClientOptions; ...@@ -5,6 +5,9 @@ import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential; import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import io.renren.factory.MongoDBCollectionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
...@@ -16,9 +19,9 @@ import java.util.List; ...@@ -16,9 +19,9 @@ import java.util.List;
/** /**
* @author: gxz gongxuanzhang@foxmail.com * @author: gxz gongxuanzhang@foxmail.com
**/ **/
@Conditional(MongoCondition.class)
@Component @Component
@ConfigurationProperties(prefix = "mongodb") @ConfigurationProperties(prefix = "mongodb")
public class MongoConfig { public class MongoConfig {
private String host; private String host;
private int port; private int port;
...@@ -30,6 +33,7 @@ public class MongoConfig { ...@@ -30,6 +33,7 @@ public class MongoConfig {
@Bean @Bean
@Conditional(MongoCondition.class)
private MongoClient getMongoClient() { private MongoClient getMongoClient() {
List<ServerAddress> adds = new ArrayList<>(); List<ServerAddress> adds = new ArrayList<>();
ServerAddress serverAddress = new ServerAddress(this.host, this.port); ServerAddress serverAddress = new ServerAddress(this.host, this.port);
...@@ -44,11 +48,13 @@ public class MongoConfig { ...@@ -44,11 +48,13 @@ public class MongoConfig {
} }
@Bean @Bean
@Conditional(MongoCondition.class)
public MongoDatabase getDataBase() { public MongoDatabase getDataBase() {
return getMongoClient().getDatabase(dataBase); return getMongoClient().getDatabase(dataBase);
} }
public MongoConfig setHost(String host) { public MongoConfig setHost(String host) {
this.host = host; this.host = host;
return this; return this;
......
package io.renren.config;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @author: gxz gongxuanzhang@foxmail.com
**/
public class MongoNullCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String database = context.getEnvironment().getProperty("renren.database");
return !"mongodb".equalsIgnoreCase(database);
}
}
package io.renren.dao; package io.renren.dao;
import io.renren.adaptor.MongoTableInfoAdaptor; import io.renren.adaptor.MongoTableInfoAdaptor;
import io.renren.config.MongoCondition;
import io.renren.config.MongoManager; import io.renren.config.MongoManager;
import io.renren.entity.mongo.MongoDefinition; import io.renren.entity.mongo.MongoDefinition;
import io.renren.factory.MongoDBCollectionFactory; import io.renren.factory.MongoDBCollectionFactory;
import io.renren.utils.MongoScanner; import io.renren.utils.MongoScanner;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.HashMap; import java.util.HashMap;
...@@ -16,14 +18,15 @@ import java.util.Map; ...@@ -16,14 +18,15 @@ import java.util.Map;
* @author: gxz gongxuanzhang@foxmail.com * @author: gxz gongxuanzhang@foxmail.com
**/ **/
@Repository @Repository
@Conditional(MongoCondition.class)
public class MongoDBGeneratorDao implements GeneratorDao { public class MongoDBGeneratorDao implements GeneratorDao {
@Autowired
private MongoDBCollectionFactory mongoDBCollectionFactory; private MongoDBCollectionFactory mongoDBCollectionFactory;
@Override @Override
public List<Map<String, Object>> queryList(Map<String, Object> map) { public List<Map<String, Object>> queryList(Map<String, Object> map) {
List<String> collectionNames = mongoDBCollectionFactory.getCollectionNames(map); List<String> collectionNames = MongoDBCollectionFactory.getCollectionNames(map);
return (List) MongoTableInfoAdaptor.tableInfo(collectionNames); return (List) MongoTableInfoAdaptor.tableInfo(collectionNames);
} }
......
...@@ -3,10 +3,13 @@ package io.renren.factory; ...@@ -3,10 +3,13 @@ package io.renren.factory;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable; import com.mongodb.client.MongoIterable;
import io.renren.config.MongoCondition;
import org.bson.Document; import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -17,16 +20,23 @@ import java.util.stream.Collectors; ...@@ -17,16 +20,23 @@ import java.util.stream.Collectors;
**/ **/
@Component @Component
@Conditional(MongoCondition.class)
public class MongoDBCollectionFactory { public class MongoDBCollectionFactory {
private final String TABLE_NAME_KEY = "tableName"; private static final String TABLE_NAME_KEY = "tableName";
private final String LIMIT_KEY = "limit"; private static final String LIMIT_KEY = "limit";
private final String OFFSET_KEY = "offset"; private static final String OFFSET_KEY = "offset";
private static MongoDatabase mongoDatabase;
// 此处是为了兼容mongo相关内容和关系型数据库的静态耦合所导致的问题
@Autowired @Autowired
private MongoDatabase mongoDatabase; private MongoDatabase database;
@PostConstruct
public void initMongoDatabase(){
mongoDatabase = database;
}
/*** /***
* 通过表名获得查询对象 * 通过表名获得查询对象
...@@ -47,7 +57,7 @@ public class MongoDBCollectionFactory { ...@@ -47,7 +57,7 @@ public class MongoDBCollectionFactory {
* @param map 这是查询条件 和关系型数据库一致 * @param map 这是查询条件 和关系型数据库一致
* @return 集合名称 * @return 集合名称
**/ **/
public List<String> getCollectionNames(Map<String, Object> map) { public static List<String> getCollectionNames(Map<String, Object> map) {
int limit = Integer.valueOf(map.get(LIMIT_KEY).toString()); int limit = Integer.valueOf(map.get(LIMIT_KEY).toString());
int skip = Integer.valueOf(map.get(OFFSET_KEY).toString()); int skip = Integer.valueOf(map.get(OFFSET_KEY).toString());
List<String> names; List<String> names;
...@@ -65,7 +75,7 @@ public class MongoDBCollectionFactory { ...@@ -65,7 +75,7 @@ public class MongoDBCollectionFactory {
* @param map 这是查询条件 和关系型数据库一致 * @param map 这是查询条件 和关系型数据库一致
* @return int * @return int
**/ **/
public int getCollectionTotal(Map<String, Object> map) { public static int getCollectionTotal(Map<String, Object> map) {
if (map.containsKey(TABLE_NAME_KEY)) { if (map.containsKey(TABLE_NAME_KEY)) {
return getCollectionNames(map.get(TABLE_NAME_KEY).toString()).size(); return getCollectionNames(map.get(TABLE_NAME_KEY).toString()).size();
} }
...@@ -74,7 +84,7 @@ public class MongoDBCollectionFactory { ...@@ -74,7 +84,7 @@ public class MongoDBCollectionFactory {
} }
private List<String> getCollectionNames() { private static List<String> getCollectionNames() {
MongoIterable<String> names = mongoDatabase.listCollectionNames(); MongoIterable<String> names = mongoDatabase.listCollectionNames();
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
for (String name : names) { for (String name : names) {
...@@ -83,7 +93,7 @@ public class MongoDBCollectionFactory { ...@@ -83,7 +93,7 @@ public class MongoDBCollectionFactory {
return result; return result;
} }
private List<String> getCollectionNames(String likeName) { private static List<String> getCollectionNames(String likeName) {
return getCollectionNames() return getCollectionNames()
.stream() .stream()
.filter((name) -> name.contains(likeName)).collect(Collectors.toList()); .filter((name) -> name.contains(likeName)).collect(Collectors.toList());
......
...@@ -36,15 +36,14 @@ import java.util.zip.ZipOutputStream; ...@@ -36,15 +36,14 @@ import java.util.zip.ZipOutputStream;
public class SysGeneratorService { public class SysGeneratorService {
@Autowired @Autowired
private GeneratorDao generatorDao; private GeneratorDao generatorDao;
@Autowired
private MongoDBCollectionFactory mongoDBCollectionFactory;
public PageUtils queryList(Query query) { public PageUtils queryList(Query query) {
Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit()); Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
List<Map<String, Object>> list = generatorDao.queryList(query); List<Map<String, Object>> list = generatorDao.queryList(query);
int total = (int) page.getTotal(); int total = (int) page.getTotal();
if (generatorDao instanceof MongoDBGeneratorDao) { if (generatorDao instanceof MongoDBGeneratorDao) {
total = mongoDBCollectionFactory.getCollectionTotal(query); total = MongoDBCollectionFactory.getCollectionTotal(query);
} }
return new PageUtils(list, total, query.getLimit(), query.getPage()); return new PageUtils(list, total, query.getLimit(), query.getPage());
} }
......
...@@ -34,14 +34,14 @@ spring: ...@@ -34,14 +34,14 @@ spring:
resources: resources:
static-locations: classpath:/static/,classpath:/views/ static-locations: classpath:/static/,classpath:/views/
mongodb: #mongodb:
host: localhost # host: localhost
port: 27017 # port: 27017
auth: false #是否使用密码验证 # auth: false #是否使用密码验证
username: tincery # username: tincery
password: Txy123.com # password: Txy123.com
source: tincery_pro # source: tincery_pro
database: tincery_pro # database: test
mybatis-plus: mybatis-plus:
mapperLocations: classpath:mapper/**/*.xml mapperLocations: classpath:mapper/**/*.xml
...@@ -55,5 +55,5 @@ pagehelper: ...@@ -55,5 +55,5 @@ pagehelper:
#指定数据库,可选值有【mysql、oracle、sqlserver、postgresql、mongodb】 #指定数据库,可选值有【mysql、oracle、sqlserver、postgresql、mongodb】
renren: renren:
database: mongodb database: mysql
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment