转达梦优化
parent
64bba380a4
commit
ca3bbe0d20
@ -1,83 +1,83 @@
|
||||
package com.glxp.api.config;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import com.glxp.api.dao.system.DbVersionDao;
|
||||
import com.glxp.api.entity.system.DbVersionEntity;
|
||||
import com.glxp.api.entity.system.SchemaData;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Order(1)
|
||||
@Component
|
||||
@Slf4j
|
||||
public class HdSchemaExecutor implements ApplicationRunner {
|
||||
|
||||
@Resource
|
||||
DbVersionDao hdCommonDao;
|
||||
|
||||
private List<SchemaData> schema = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
//初始版本列表
|
||||
buildSchemas();
|
||||
//定义sql文件路径
|
||||
String basePath = "schemas/";
|
||||
//非版本控制,初始化脚本
|
||||
ClassLoader loader = this.getClass().getClassLoader();
|
||||
//通过流的方式获取项目路径下的文件
|
||||
InputStream inputStream = loader.getResourceAsStream(basePath + "init.sql");
|
||||
//获取文件内容
|
||||
String sql = IoUtil.readUtf8(inputStream);
|
||||
try {
|
||||
//判断版本表是否存在
|
||||
int count = hdCommonDao.selectTableExist("hd_version");
|
||||
if (count == 0) {
|
||||
hdCommonDao.updateSql(sql);
|
||||
}
|
||||
for (SchemaData schemaData : schema) {
|
||||
//查询版本记录是否存在
|
||||
count = hdCommonDao.selectVersion(schemaData.getVersion());
|
||||
if (count == 0) {
|
||||
log.info("--------------执行数据脚本,版本:" + schemaData.getVersion());
|
||||
//获取对应sql脚本
|
||||
inputStream = loader.getResourceAsStream(basePath + schemaData.getFileName());
|
||||
sql = IoUtil.readUtf8(inputStream);
|
||||
hdCommonDao.updateSql(sql);
|
||||
DbVersionEntity entity = new DbVersionEntity();
|
||||
entity.setId(UUID.randomUUID().toString());
|
||||
entity.setVersion(schemaData.getVersion());
|
||||
entity.setCreated(new Date());
|
||||
entity.setRemark(schemaData.getFileName());
|
||||
//写入版本记录
|
||||
hdCommonDao.insertVersion(entity);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IORuntimeException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//关闭流
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void buildSchemas() {
|
||||
// schema.add(new SchemaData("v2.1", "schema_v2.1.sql"));
|
||||
// schema.add(new SchemaData("v2.2", "schema_v2.2.sql"));
|
||||
schema.add(new SchemaData("v2.3", "schema_v2.3.sql"));
|
||||
schema.add(new SchemaData("v2.4", "schema_v2.4.sql"));
|
||||
}
|
||||
|
||||
}
|
||||
//package com.glxp.api.config;
|
||||
//
|
||||
//import cn.hutool.core.io.IORuntimeException;
|
||||
//import cn.hutool.core.io.IoUtil;
|
||||
//import cn.hutool.core.lang.UUID;
|
||||
//import com.glxp.api.dao.system.DbVersionDao;
|
||||
//import com.glxp.api.entity.system.DbVersionEntity;
|
||||
//import com.glxp.api.entity.system.SchemaData;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.boot.ApplicationArguments;
|
||||
//import org.springframework.boot.ApplicationRunner;
|
||||
//import org.springframework.core.annotation.Order;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import javax.annotation.Resource;
|
||||
//import java.io.InputStream;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.Date;
|
||||
//import java.util.List;
|
||||
//
|
||||
//@Order(1)
|
||||
//@Component
|
||||
//@Slf4j
|
||||
//public class HdSchemaExecutor implements ApplicationRunner {
|
||||
//
|
||||
// @Resource
|
||||
// DbVersionDao hdCommonDao;
|
||||
//
|
||||
// private List<SchemaData> schema = new ArrayList<>();
|
||||
//
|
||||
// @Override
|
||||
// public void run(ApplicationArguments args) throws Exception {
|
||||
// //初始版本列表
|
||||
// buildSchemas();
|
||||
// //定义sql文件路径
|
||||
// String basePath = "schemas/";
|
||||
// //非版本控制,初始化脚本
|
||||
// ClassLoader loader = this.getClass().getClassLoader();
|
||||
// //通过流的方式获取项目路径下的文件
|
||||
// InputStream inputStream = loader.getResourceAsStream(basePath + "init.sql");
|
||||
// //获取文件内容
|
||||
// String sql = IoUtil.readUtf8(inputStream);
|
||||
// try {
|
||||
// //判断版本表是否存在
|
||||
// int count = hdCommonDao.selectTableExist("hd_version");
|
||||
// if (count == 0) {
|
||||
// hdCommonDao.updateSql(sql);
|
||||
// }
|
||||
// for (SchemaData schemaData : schema) {
|
||||
// //查询版本记录是否存在
|
||||
// count = hdCommonDao.selectVersion(schemaData.getVersion());
|
||||
// if (count == 0) {
|
||||
// log.info("--------------执行数据脚本,版本:" + schemaData.getVersion());
|
||||
// //获取对应sql脚本
|
||||
// inputStream = loader.getResourceAsStream(basePath + schemaData.getFileName());
|
||||
// sql = IoUtil.readUtf8(inputStream);
|
||||
// hdCommonDao.updateSql(sql);
|
||||
// DbVersionEntity entity = new DbVersionEntity();
|
||||
// entity.setId(UUID.randomUUID().toString());
|
||||
// entity.setVersion(schemaData.getVersion());
|
||||
// entity.setCreated(new Date());
|
||||
// entity.setRemark(schemaData.getFileName());
|
||||
// //写入版本记录
|
||||
// hdCommonDao.insertVersion(entity);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// } catch (IORuntimeException e) {
|
||||
// e.printStackTrace();
|
||||
// } finally {
|
||||
// //关闭流
|
||||
// inputStream.close();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void buildSchemas() {
|
||||
//// schema.add(new SchemaData("v2.1", "schema_v2.1.sql"));
|
||||
//// schema.add(new SchemaData("v2.2", "schema_v2.2.sql"));
|
||||
// schema.add(new SchemaData("v2.3", "schema_v2.3.sql"));
|
||||
// schema.add(new SchemaData("v2.4", "schema_v2.4.sql"));
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package com.glxp.api.handler;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.reflection.SystemMetaObject;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
@Intercepts({
|
||||
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
|
||||
})
|
||||
public class MybatisDmHandler implements Interceptor {
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
// 获取被拦截的 StatementHandler 对象
|
||||
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
|
||||
|
||||
// 获取 SQL 语句
|
||||
String sql = (String) metaObject.getValue("delegate.boundSql.sql");
|
||||
System.out.println("拦截到的 SQL: " + sql);
|
||||
String processedSql = camelToSnake(sql);
|
||||
// // 可在此对 SQL 进行处理,例如修改 SQL、添加日志等
|
||||
// String processedSql = sql.toUpperCase();
|
||||
System.out.println("处理后的 SQL: " + processedSql);
|
||||
metaObject.setValue("delegate.boundSql.sql", processedSql);
|
||||
|
||||
// 继续执行原方法
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
// 将目标对象包装成代理对象
|
||||
return Plugin.wrap(target, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
// 可在此设置拦截器的属性
|
||||
}
|
||||
public String camelToSnake(String camelCase) {
|
||||
// 使用正则表达式替换大写字母为下划线加小写字母
|
||||
return camelCase.replaceAll("(?<=[a-z])([A-Z])", "_$1").toLowerCase();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue