Commit 9884a790 by gxz

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

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