Commit eb6f167c by gxz

修改了一些格式的内容

parent ac23435e
...@@ -27,6 +27,7 @@ public class MongoTableInfoAdaptor { ...@@ -27,6 +27,7 @@ public class MongoTableInfoAdaptor {
} }
return result; return result;
} }
public static Map<String, String> tableInfo(String name) { public static Map<String, String> tableInfo(String name) {
Map<String, String> tableInfo = new HashMap<>(4 * 4 / 3 + 1); Map<String, String> tableInfo = new HashMap<>(4 * 4 / 3 + 1);
tableInfo.put("engine", "mongo无引擎"); tableInfo.put("engine", "mongo无引擎");
...@@ -51,7 +52,7 @@ public class MongoTableInfoAdaptor { ...@@ -51,7 +52,7 @@ public class MongoTableInfoAdaptor {
String extra = definition.isArray() ? "array" : ""; String extra = definition.isArray() ? "array" : "";
map.put("extra", extra); map.put("extra", extra);
map.put("columnComment", ""); map.put("columnComment", "");
map.put("dataType", definition.hasChild()?propertyName:type); map.put("dataType", definition.hasChild() ? propertyName : type);
map.put("columnName", propertyName); map.put("columnName", propertyName);
// mongo默认主键是_id // mongo默认主键是_id
String columnKey = propertyName.equals(mongoKey) ? "PRI" : ""; String columnKey = propertyName.equals(mongoKey) ? "PRI" : "";
...@@ -62,5 +63,4 @@ public class MongoTableInfoAdaptor { ...@@ -62,5 +63,4 @@ public class MongoTableInfoAdaptor {
} }
} }
/** /**
* Copyright (c) 2018 人人开源 All rights reserved. * Copyright (c) 2018 人人开源 All rights reserved.
* * <p>
* https://www.renren.io * https://www.renren.io
* * <p>
* 版权所有,侵权必究! * 版权所有,侵权必究!
*/ */
...@@ -37,26 +37,27 @@ public class DbConfig { ...@@ -37,26 +37,27 @@ public class DbConfig {
private MongoDBGeneratorDao mongoDBGeneratorDao; private MongoDBGeneratorDao mongoDBGeneratorDao;
private static boolean mongo = false; private static boolean mongo = false;
@Bean @Bean
@Primary @Primary
public GeneratorDao getGeneratorDao(){ public GeneratorDao getGeneratorDao() {
if("mysql".equalsIgnoreCase(database)){ if ("mysql".equalsIgnoreCase(database)) {
return mySQLGeneratorDao; return mySQLGeneratorDao;
}else if("oracle".equalsIgnoreCase(database)){ } else if ("oracle".equalsIgnoreCase(database)) {
return oracleGeneratorDao; return oracleGeneratorDao;
}else if("sqlserver".equalsIgnoreCase(database)){ } else if ("sqlserver".equalsIgnoreCase(database)) {
return sqlServerGeneratorDao; return sqlServerGeneratorDao;
}else if("postgresql".equalsIgnoreCase(database)){ } else if ("postgresql".equalsIgnoreCase(database)) {
return postgreSQLGeneratorDao; return postgreSQLGeneratorDao;
}else if("mongodb".equalsIgnoreCase(database)){ } else if ("mongodb".equalsIgnoreCase(database)) {
mongo = true; mongo = true;
return mongoDBGeneratorDao; return mongoDBGeneratorDao;
} } else {
else {
throw new RRException("不支持当前数据库:" + database); throw new RRException("不支持当前数据库:" + database);
} }
} }
public static boolean isMongo(){
public static boolean isMongo() {
return mongo; return mongo;
} }
......
...@@ -35,8 +35,8 @@ public class MongoConfig { ...@@ -35,8 +35,8 @@ public class MongoConfig {
ServerAddress serverAddress = new ServerAddress(this.host, this.port); ServerAddress serverAddress = new ServerAddress(this.host, this.port);
adds.add(serverAddress); adds.add(serverAddress);
if (this.auth) { if (this.auth) {
MongoCredential mongoCredential MongoCredential mongoCredential = MongoCredential.
= MongoCredential.createScramSha1Credential(this.username, this.source, this.password.toCharArray()); createScramSha1Credential(this.username, this.source, this.password.toCharArray());
MongoClientOptions mongoClientOptions = MongoClientOptions.builder().build(); MongoClientOptions mongoClientOptions = MongoClientOptions.builder().build();
return new MongoClient(adds, mongoCredential, mongoClientOptions); return new MongoClient(adds, mongoCredential, mongoClientOptions);
} }
......
...@@ -29,10 +29,10 @@ public class MongoDBGeneratorDao implements GeneratorDao { ...@@ -29,10 +29,10 @@ public class MongoDBGeneratorDao implements GeneratorDao {
@Override @Override
public Map<String, String> queryTable(String tableName) { public Map<String, String> queryTable(String tableName) {
Map<String,String> result = new HashMap<>(4*4/3+1); Map<String, String> result = new HashMap<>(4 * 4 / 3 + 1);
result.put("engine", ""); result.put("engine", "");
result.put("createTime", ""); result.put("createTime", "");
result.put("tableComment", "mongoDB "+tableName); result.put("tableComment", "mongoDB " + tableName);
result.put("tableName", tableName); result.put("tableName", tableName);
return result; return result;
...@@ -41,7 +41,7 @@ public class MongoDBGeneratorDao implements GeneratorDao { ...@@ -41,7 +41,7 @@ public class MongoDBGeneratorDao implements GeneratorDao {
@Override @Override
public List<Map<String, String>> queryColumns(String tableName) { public List<Map<String, String>> queryColumns(String tableName) {
MongoDefinition mongoDefinition = MongoManager.getInfo(tableName); MongoDefinition mongoDefinition = MongoManager.getInfo(tableName);
if(mongoDefinition == null){ if (mongoDefinition == null) {
System.out.println(tableName); System.out.println(tableName);
MongoScanner mongoScanner = new MongoScanner(mongoDBCollectionFactory.getCollection(tableName)); MongoScanner mongoScanner = new MongoScanner(mongoDBCollectionFactory.getCollection(tableName));
mongoDefinition = mongoScanner.getProduct(); mongoDefinition = mongoScanner.getProduct();
......
...@@ -6,6 +6,7 @@ import org.apache.commons.collections.CollectionUtils; ...@@ -6,6 +6,7 @@ import org.apache.commons.collections.CollectionUtils;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;
/** /**
* 解析表之后得到的信息实体 * 解析表之后得到的信息实体
* 换句话说这个类就是一张mongo一张表的内容 * 换句话说这个类就是一张mongo一张表的内容
...@@ -24,11 +25,11 @@ public class MongoDefinition implements Serializable { ...@@ -24,11 +25,11 @@ public class MongoDefinition implements Serializable {
private List<MongoDefinition> child; private List<MongoDefinition> child;
public List<MongoGeneratorEntity> getChildrenInfo(String tableName){ public List<MongoGeneratorEntity> getChildrenInfo(String tableName) {
List<MongoGeneratorEntity> result = new ArrayList<>(); List<MongoGeneratorEntity> result = new ArrayList<>();
MongoGeneratorEntity info = new MongoGeneratorEntity(); MongoGeneratorEntity info = new MongoGeneratorEntity();
// 表信息 // 表信息
Map<String, String> tableInfo = MongoTableInfoAdaptor.tableInfo(tableName); Map<String, String> tableInfo = MongoTableInfoAdaptor.tableInfo(tableName);
// 列名信息 // 列名信息
List<Map<String, String>> columnsInfo = new ArrayList<>(); List<Map<String, String>> columnsInfo = new ArrayList<>();
info.setColumns(columnsInfo); info.setColumns(columnsInfo);
...@@ -37,11 +38,11 @@ public class MongoDefinition implements Serializable { ...@@ -37,11 +38,11 @@ public class MongoDefinition implements Serializable {
List<MongoDefinition> child = this.getChild(); List<MongoDefinition> child = this.getChild();
for (MongoDefinition mongoDefinition : child) { for (MongoDefinition mongoDefinition : child) {
Map<String, String> columnInfo = new HashMap<>(5); Map<String, String> columnInfo = new HashMap<>(5);
columnInfo.put("columnName",mongoDefinition.getPropertyName()); columnInfo.put("columnName", mongoDefinition.getPropertyName());
columnInfo.put("dataType",Type.typeInfo(mongoDefinition.getType())); columnInfo.put("dataType", Type.typeInfo(mongoDefinition.getType()));
columnInfo.put("extra",mongoDefinition.isArray()?"array":""); columnInfo.put("extra", mongoDefinition.isArray() ? "array" : "");
columnsInfo.add(columnInfo); columnsInfo.add(columnInfo);
if(mongoDefinition.hasChild()){ if (mongoDefinition.hasChild()) {
result.addAll(mongoDefinition.getChildrenInfo(mongoDefinition.getPropertyName())); result.addAll(mongoDefinition.getChildrenInfo(mongoDefinition.getPropertyName()));
} }
} }
...@@ -59,7 +60,6 @@ public class MongoDefinition implements Serializable { ...@@ -59,7 +60,6 @@ public class MongoDefinition implements Serializable {
} }
public MongoDefinition setType(Integer type) { public MongoDefinition setType(Integer type) {
this.type = type; this.type = type;
return this; return this;
......
...@@ -6,8 +6,10 @@ import io.renren.entity.TableEntity; ...@@ -6,8 +6,10 @@ import io.renren.entity.TableEntity;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** mysql一张表只需要一个表信息和列名信息 /**
* mysql一张表只需要一个表信息和列名信息
* 但是mongo一张表可能需要多个实体类 所以单独用一个bean封装 * 但是mongo一张表可能需要多个实体类 所以单独用一个bean封装
*
* @author gxz * @author gxz
* @date 2020/5/10 0:14 * @date 2020/5/10 0:14
*/ */
...@@ -18,8 +20,7 @@ public class MongoGeneratorEntity { ...@@ -18,8 +20,7 @@ public class MongoGeneratorEntity {
private List<Map<String, String>> columns; private List<Map<String, String>> columns;
public TableEntity toTableEntity() {
public TableEntity toTableEntity(){
TableEntity tableEntity = new TableEntity(); TableEntity tableEntity = new TableEntity();
Map<String, String> tableInfo = this.tableInfo; Map<String, String> tableInfo = this.tableInfo;
tableEntity.setTableName(tableInfo.get("tableName")); tableEntity.setTableName(tableInfo.get("tableName"));
......
...@@ -17,14 +17,15 @@ public enum Type { ...@@ -17,14 +17,15 @@ public enum Type {
DOUBLE(1); DOUBLE(1);
private final int num; private final int num;
Type(int num) { Type(int num) {
this.num = num; this.num = num;
} }
public static String typeInfo(int num){ public static String typeInfo(int num) {
Type[] values = values(); Type[] values = values();
for (Type value : values) { for (Type value : values) {
if(Objects.equals(num,value.num)){ if (Objects.equals(num, value.num)) {
return value.toString(); return value.toString();
} }
} }
......
/** /**
* Copyright (c) 2018 人人开源 All rights reserved. * Copyright (c) 2018 人人开源 All rights reserved.
* * <p>
* https://www.renren.io * https://www.renren.io
* * <p>
* 版权所有,侵权必究! * 版权所有,侵权必究!
*/ */
...@@ -34,49 +34,47 @@ import java.util.zip.ZipOutputStream; ...@@ -34,49 +34,47 @@ import java.util.zip.ZipOutputStream;
*/ */
@Service @Service
public class SysGeneratorService { public class SysGeneratorService {
@Autowired @Autowired
private GeneratorDao generatorDao; private GeneratorDao generatorDao;
@Autowired @Autowired
private MongoDBCollectionFactory mongoDBCollectionFactory; 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);
}
return new PageUtils(list, total, query.getLimit(), query.getPage());
}
public Map<String, String> queryTable(String tableName) {
return generatorDao.queryTable(tableName);
}
public List<Map<String, String>> queryColumns(String tableName) { public PageUtils queryList(Query query) {
return generatorDao.queryColumns(tableName); 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);
}
return new PageUtils(list, total, query.getLimit(), query.getPage());
}
public Map<String, String> queryTable(String tableName) {
return generatorDao.queryTable(tableName);
}
public List<Map<String, String>> queryColumns(String tableName) {
return generatorDao.queryColumns(tableName);
}
public byte[] generatorCode(String[] tableNames) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for(String tableName : tableNames){
//查询表信息
Map<String, String> table = queryTable(tableName);
//查询列信息
List<Map<String, String>> columns = queryColumns(tableName);
//生成代码
GenUtils.generatorCode(table, columns, zip);
}
if(MongoManager.isMongo()){
GenUtils.generatorMongoCode(tableNames, zip);
}
public byte[] generatorCode(String[] tableNames) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for (String tableName : tableNames) {
//查询表信息
Map<String, String> table = queryTable(tableName);
//查询列信息
List<Map<String, String>> columns = queryColumns(tableName);
//生成代码
GenUtils.generatorCode(table, columns, zip);
}
if (MongoManager.isMongo()) {
GenUtils.generatorMongoCode(tableNames, zip);
}
IOUtils.closeQuietly(zip); IOUtils.closeQuietly(zip);
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
} }
...@@ -195,8 +195,8 @@ public class GenUtils { ...@@ -195,8 +195,8 @@ public class GenUtils {
for (Map<String, String> column : mongoGeneratorEntity.getColumns()) { for (Map<String, String> column : mongoGeneratorEntity.getColumns()) {
ColumnEntity columnEntity = new ColumnEntity(); ColumnEntity columnEntity = new ColumnEntity();
String columnName = column.get("columnName"); String columnName = column.get("columnName");
if(columnName.contains(".")){ if (columnName.contains(".")) {
columnName = columnName.substring(columnName.lastIndexOf(".")+1); columnName = columnName.substring(columnName.lastIndexOf(".") + 1);
} }
columnEntity.setColumnName(columnName); columnEntity.setColumnName(columnName);
columnEntity.setDataType(column.get("dataType")); columnEntity.setDataType(column.get("dataType"));
...@@ -301,7 +301,7 @@ public class GenUtils { ...@@ -301,7 +301,7 @@ public class GenUtils {
packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator; packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
} }
if (template.contains("MongoChildrenEntity.java.vm")) { if (template.contains("MongoChildrenEntity.java.vm")) {
return packagePath + "entity" + File.separator + "inner" + File.separator + currentTableName +File.separator+className + "InnerEntity.java"; return packagePath + "entity" + File.separator + "inner" + File.separator + currentTableName + File.separator + className + "InnerEntity.java";
} }
if (template.contains("Entity.java.vm") || template.contains("MongoEntity.java.vm")) { if (template.contains("Entity.java.vm") || template.contains("MongoEntity.java.vm")) {
return packagePath + "entity" + File.separator + className + "Entity.java"; return packagePath + "entity" + File.separator + className + "Entity.java";
......
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