转达梦优化
parent
64bba380a4
commit
ca3bbe0d20
@ -1,83 +1,83 @@
|
|||||||
package com.glxp.api.config;
|
//package com.glxp.api.config;
|
||||||
|
//
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
//import cn.hutool.core.io.IORuntimeException;
|
||||||
import cn.hutool.core.io.IoUtil;
|
//import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.lang.UUID;
|
//import cn.hutool.core.lang.UUID;
|
||||||
import com.glxp.api.dao.system.DbVersionDao;
|
//import com.glxp.api.dao.system.DbVersionDao;
|
||||||
import com.glxp.api.entity.system.DbVersionEntity;
|
//import com.glxp.api.entity.system.DbVersionEntity;
|
||||||
import com.glxp.api.entity.system.SchemaData;
|
//import com.glxp.api.entity.system.SchemaData;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
//import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
//import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
//import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.core.annotation.Order;
|
//import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
import javax.annotation.Resource;
|
//import javax.annotation.Resource;
|
||||||
import java.io.InputStream;
|
//import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
//import java.util.ArrayList;
|
||||||
import java.util.Date;
|
//import java.util.Date;
|
||||||
import java.util.List;
|
//import java.util.List;
|
||||||
|
//
|
||||||
@Order(1)
|
//@Order(1)
|
||||||
@Component
|
//@Component
|
||||||
@Slf4j
|
//@Slf4j
|
||||||
public class HdSchemaExecutor implements ApplicationRunner {
|
//public class HdSchemaExecutor implements ApplicationRunner {
|
||||||
|
//
|
||||||
@Resource
|
// @Resource
|
||||||
DbVersionDao hdCommonDao;
|
// DbVersionDao hdCommonDao;
|
||||||
|
//
|
||||||
private List<SchemaData> schema = new ArrayList<>();
|
// private List<SchemaData> schema = new ArrayList<>();
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
// public void run(ApplicationArguments args) throws Exception {
|
||||||
//初始版本列表
|
// //初始版本列表
|
||||||
buildSchemas();
|
// buildSchemas();
|
||||||
//定义sql文件路径
|
// //定义sql文件路径
|
||||||
String basePath = "schemas/";
|
// String basePath = "schemas/";
|
||||||
//非版本控制,初始化脚本
|
// //非版本控制,初始化脚本
|
||||||
ClassLoader loader = this.getClass().getClassLoader();
|
// ClassLoader loader = this.getClass().getClassLoader();
|
||||||
//通过流的方式获取项目路径下的文件
|
// //通过流的方式获取项目路径下的文件
|
||||||
InputStream inputStream = loader.getResourceAsStream(basePath + "init.sql");
|
// InputStream inputStream = loader.getResourceAsStream(basePath + "init.sql");
|
||||||
//获取文件内容
|
// //获取文件内容
|
||||||
String sql = IoUtil.readUtf8(inputStream);
|
// String sql = IoUtil.readUtf8(inputStream);
|
||||||
try {
|
// try {
|
||||||
//判断版本表是否存在
|
// //判断版本表是否存在
|
||||||
int count = hdCommonDao.selectTableExist("hd_version");
|
// int count = hdCommonDao.selectTableExist("hd_version");
|
||||||
if (count == 0) {
|
// if (count == 0) {
|
||||||
hdCommonDao.updateSql(sql);
|
// hdCommonDao.updateSql(sql);
|
||||||
}
|
// }
|
||||||
for (SchemaData schemaData : schema) {
|
// for (SchemaData schemaData : schema) {
|
||||||
//查询版本记录是否存在
|
// //查询版本记录是否存在
|
||||||
count = hdCommonDao.selectVersion(schemaData.getVersion());
|
// count = hdCommonDao.selectVersion(schemaData.getVersion());
|
||||||
if (count == 0) {
|
// if (count == 0) {
|
||||||
log.info("--------------执行数据脚本,版本:" + schemaData.getVersion());
|
// log.info("--------------执行数据脚本,版本:" + schemaData.getVersion());
|
||||||
//获取对应sql脚本
|
// //获取对应sql脚本
|
||||||
inputStream = loader.getResourceAsStream(basePath + schemaData.getFileName());
|
// inputStream = loader.getResourceAsStream(basePath + schemaData.getFileName());
|
||||||
sql = IoUtil.readUtf8(inputStream);
|
// sql = IoUtil.readUtf8(inputStream);
|
||||||
hdCommonDao.updateSql(sql);
|
// hdCommonDao.updateSql(sql);
|
||||||
DbVersionEntity entity = new DbVersionEntity();
|
// DbVersionEntity entity = new DbVersionEntity();
|
||||||
entity.setId(UUID.randomUUID().toString());
|
// entity.setId(UUID.randomUUID().toString());
|
||||||
entity.setVersion(schemaData.getVersion());
|
// entity.setVersion(schemaData.getVersion());
|
||||||
entity.setCreated(new Date());
|
// entity.setCreated(new Date());
|
||||||
entity.setRemark(schemaData.getFileName());
|
// entity.setRemark(schemaData.getFileName());
|
||||||
//写入版本记录
|
// //写入版本记录
|
||||||
hdCommonDao.insertVersion(entity);
|
// hdCommonDao.insertVersion(entity);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
} catch (IORuntimeException e) {
|
// } catch (IORuntimeException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
} finally {
|
// } finally {
|
||||||
//关闭流
|
// //关闭流
|
||||||
inputStream.close();
|
// inputStream.close();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void buildSchemas() {
|
// public void buildSchemas() {
|
||||||
// schema.add(new SchemaData("v2.1", "schema_v2.1.sql"));
|
//// 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.2", "schema_v2.2.sql"));
|
||||||
schema.add(new SchemaData("v2.3", "schema_v2.3.sql"));
|
// schema.add(new SchemaData("v2.3", "schema_v2.3.sql"));
|
||||||
schema.add(new SchemaData("v2.4", "schema_v2.4.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