Commit eb6f167c by gxz

修改了一些格式的内容

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