cn.jiangzeyin.database.CommonDatabaseConfig Maven / Gradle / Ivy
package cn.jiangzeyin.database;
import cn.jiangzeyin.common.CommonPropertiesFinal;
import cn.jiangzeyin.common.spring.init.SpringUtil;
import cn.jiangzeyin.database.config.DataSourceConfig;
import cn.jiangzeyin.system.DbLogInterface;
import cn.jiangzeyin.system.SystemDbLog;
import cn.jiangzeyin.system.log.LogType;
import cn.jiangzeyin.system.log.SystemLog;
import cn.jiangzeyin.util.ResourceUtil;
import cn.jiangzeyin.util.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
/**
* Created by jiangzeyin on 2017/8/24.
*/
@Configuration
public class CommonDatabaseConfig {
@Value("${" + CommonPropertiesFinal.DATABASE_DB_CONFIG_PATH + ":classpath:db.properties}")
private String dbConfigPath;
@Value("${beforeLoadClass:}")
private String beforeLoadClass;
@Value("${" + CommonPropertiesFinal.DATABASE_CONFIG_JSON_PATH + ":" + CommonPropertiesFinal.DATABASE_CONFIG_JSON_PATH_DEFAULT_VALUE + "}")
private String configJsonPath;
public static void init() throws Exception {
CommonDatabaseConfig commonDatabaseConfig = SpringUtil.getBean(CommonDatabaseConfig.class);
if (!StringUtil.isEmpty(commonDatabaseConfig.beforeLoadClass)) {
Class> tclass = Class.forName(commonDatabaseConfig.beforeLoadClass);
Method method = tclass.getDeclaredMethod("init");
method.invoke(null);
}
SystemDbLog.setDbLogInterface(new DbLogInterface() {
@Override
public void info(Object o) {
if (o != null)
SystemLog.LOG(LogType.SQL).info(o.toString());
}
@Override
public void error(String msg, Throwable throwable) {
SystemLog.LOG(LogType.SQL_ERROR).error(msg, throwable);
}
@Override
public void warn(Object o) {
if (o != null)
SystemLog.LOG(LogType.SQL).warn(o.toString());
}
@Override
public void warn(String s, Throwable throwable) {
SystemLog.LOG(LogType.SQL_ERROR).error(s, throwable);
}
});
try {
DataSourceConfig.init(commonDatabaseConfig.dbConfigPath);
EntityInfo.setConvertEntity(new EntityInfo.ConvertEntity() {
@Override
public String getDatabaseName(Class aClass) {
return CommonEntityInfoConfig.getDatabaseName(aClass);
}
@Override
public String getTableName(Class> class1, boolean isIndex, String index, boolean isDatabaseName) {
return CommonEntityInfoConfig.getTableName(class1, isIndex, index, isDatabaseName);
}
});
} catch (Exception e) {
SystemLog.LOG(LogType.SQL_ERROR).error("初始化数据库错误", e);
throw new Exception("启动失败", e);
}
loadEntityMapperJson(commonDatabaseConfig.configJsonPath);
}
private static void loadEntityMapperJson(String configJsonPath) throws IOException, ClassNotFoundException {
InputStream inputStream;
try {
inputStream = ResourceUtil.getResource(configJsonPath);
} catch (IOException ignored) {
if (CommonPropertiesFinal.DATABASE_CONFIG_JSON_PATH_DEFAULT_VALUE.equals(configJsonPath))
return;
throw ignored;
}
InputStreamReader isr = new InputStreamReader(inputStream);
// 读取文件内容
int length = -1;
char[] buffer = new char[1024];
StringBuilder sb = new StringBuilder();
while ((length = isr.read(buffer, 0, 1024)) != -1) {
sb.append(buffer, 0, length);
}
isr.close();
inputStream.close();
JSONObject jsonObject = JSONArray.parseObject(sb.toString());
CommonEntityInfoConfig.init(jsonObject);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy