clickhouse模块增加mybatis-plus基本赠删改接口方法实现
parent
e8e2846c42
commit
921d0084ac
|
|
@ -0,0 +1,35 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.enums.SqlMethodDiv;
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.methods.DeleteClickHouse;
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.methods.UpdateByIdClickHouse;
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.methods.UpdateClickHouse;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册方法
|
||||||
|
*
|
||||||
|
* @author coder
|
||||||
|
*/
|
||||||
|
public class ClickHouseSqlInjector extends DefaultSqlInjector {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
|
||||||
|
/**
|
||||||
|
* 这里很重要,先要通过父类方法,获取到原有的集合,不然会自带的通用方法会失效的
|
||||||
|
*/
|
||||||
|
List<AbstractMethod> methodList = super.getMethodList(mapperClass,tableInfo);
|
||||||
|
/***
|
||||||
|
* 添加自定义方法类
|
||||||
|
*/
|
||||||
|
methodList.add(new UpdateByIdClickHouse(SqlMethodDiv.UPDATE_BY_ID.getMethod()));
|
||||||
|
methodList.add(new UpdateClickHouse(SqlMethodDiv.UPDATE.getMethod()));
|
||||||
|
methodList.add(new DeleteClickHouse(SqlMethodDiv.DELETE_BY_ID.getMethod()));
|
||||||
|
return methodList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector.config;//package com.w3.framework.clickhouse.config;
|
||||||
|
//
|
||||||
|
//import lombok.Data;
|
||||||
|
//import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * jdbc基本配置参数
|
||||||
|
// */
|
||||||
|
//@ConfigurationProperties(prefix = "spring.datasource.click")
|
||||||
|
//@Data
|
||||||
|
//public class CkProperties {
|
||||||
|
//
|
||||||
|
// public String address;
|
||||||
|
//
|
||||||
|
// public String username;
|
||||||
|
//
|
||||||
|
// public String password;
|
||||||
|
//
|
||||||
|
// public String dbName;
|
||||||
|
//
|
||||||
|
// public int socketTimeout;
|
||||||
|
//
|
||||||
|
// private String driverClassName;
|
||||||
|
//
|
||||||
|
// private String url;
|
||||||
|
//
|
||||||
|
// private int initialSize;
|
||||||
|
//
|
||||||
|
// private int maxActive;
|
||||||
|
//
|
||||||
|
// private int minIdle;
|
||||||
|
//
|
||||||
|
// private int maxWait;
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector.config;//package com.w3.framework.clickhouse.config;
|
||||||
|
//
|
||||||
|
//import lombok.Data;
|
||||||
|
//import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
//import org.springframework.context.annotation.Bean;
|
||||||
|
//import ru.yandex.clickhouse.ClickHouseDataSource;
|
||||||
|
//import ru.yandex.clickhouse.settings.ClickHouseProperties;
|
||||||
|
//
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * @desc ClickHouse基本配置参数
|
||||||
|
// * SQL方式执行
|
||||||
|
// */
|
||||||
|
//@AutoConfiguration
|
||||||
|
//@EnableConfigurationProperties({CkProperties.class})
|
||||||
|
//@Data
|
||||||
|
//public class ClickHouseConfig {
|
||||||
|
//
|
||||||
|
//// private static ClickHouseDataSource clickHouseDataSource;
|
||||||
|
//
|
||||||
|
// @Bean
|
||||||
|
// public ClickHouseDataSource clickHouseDataSource(CkProperties ckProperties) {
|
||||||
|
// ClickHouseProperties properties = new ClickHouseProperties();
|
||||||
|
// properties.setUser(ckProperties.username);
|
||||||
|
// properties.setPassword(ckProperties.getPassword());
|
||||||
|
// properties.setDatabase(ckProperties.dbName);
|
||||||
|
// properties.setSocketTimeout(ckProperties.socketTimeout);
|
||||||
|
//// clickHouseDataSource = new ClickHouseDataSource(clickhouseProperties.address, properties);
|
||||||
|
//// return clickHouseDataSource;
|
||||||
|
// return new ClickHouseDataSource(ckProperties.address, properties);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//// public static List<Map<String, String>> exeSql(String sql) {
|
||||||
|
//// System.out.println("cliockhouse 执行sql:" + sql);
|
||||||
|
//// Connection connection = null;
|
||||||
|
//// try {
|
||||||
|
//// connection = clickHouseDataSource.getConnection();
|
||||||
|
//// assert connection != null;
|
||||||
|
//// Statement statement = connection.createStatement();
|
||||||
|
//// ResultSet results = statement.executeQuery(sql);
|
||||||
|
//// ResultSetMetaData rsmd = results.getMetaData();
|
||||||
|
//// List<Map<String, String>> list = new ArrayList<>();
|
||||||
|
//// while (results.next()) {
|
||||||
|
//// Map<String, String> row = new HashMap<>();
|
||||||
|
//// for (int i = 1; i <= rsmd.getColumnCount(); i++) {
|
||||||
|
//// row.put(rsmd.getColumnName(i), results.getString(rsmd.getColumnName(i)));
|
||||||
|
//// }
|
||||||
|
//// list.add(row);
|
||||||
|
//// }
|
||||||
|
//// return list;
|
||||||
|
//// } catch (SQLException e) {
|
||||||
|
//// e.printStackTrace();
|
||||||
|
//// }
|
||||||
|
//// return null;
|
||||||
|
//// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector.enums;
|
||||||
|
|
||||||
|
public enum SqlMethodDiv {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除
|
||||||
|
*/
|
||||||
|
DELETE_BY_ID("deleteByIdClickHouse", "根据ID 删除一条数据", "<script>\nALTER TABLE %s DELETE WHERE %s=#{%s}\n</script>"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 逻辑删除
|
||||||
|
*/
|
||||||
|
LOGIC_DELETE_BY_ID("deleteByIdClickHouse", "根据ID 逻辑删除一条数据", "<script>\nALTER TABLE %s UPDATE %s where %s=#{%s} %s\n</script>"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改 条件主键
|
||||||
|
*/
|
||||||
|
UPDATE_BY_ID("updateByIdClickHouse", "根据ID 选择修改数据", "<script>\nALTER TABLE %s UPDATE %s where %s=#{%s} %s\n</script>"),
|
||||||
|
/**
|
||||||
|
* 修改 条件主键
|
||||||
|
*/
|
||||||
|
UPDATE("updateClickHouse", "根据 whereEntity 条件,更新记录", "<script>\nALTER TABLE %s UPDATE %s %s %s\n</script>");
|
||||||
|
|
||||||
|
private final String method;
|
||||||
|
private final String desc;
|
||||||
|
private final String sql;
|
||||||
|
|
||||||
|
SqlMethodDiv(String method, String desc, String sql) {
|
||||||
|
this.method = method;
|
||||||
|
this.desc = desc;
|
||||||
|
this.sql = sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDesc() {
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSql() {
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector.methods;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.enums.SqlMethodDiv;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author coder
|
||||||
|
* @date 2023-08-27
|
||||||
|
* @desc 通过MybatisPlus源码 扩展修改
|
||||||
|
*/
|
||||||
|
public class DeleteClickHouse extends AbstractMethod {
|
||||||
|
|
||||||
|
public DeleteClickHouse(String methodName) {
|
||||||
|
super(methodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
String sql;
|
||||||
|
SqlMethodDiv sqlMethod = SqlMethodDiv.LOGIC_DELETE_BY_ID;
|
||||||
|
if (tableInfo.isWithLogicDelete()) {
|
||||||
|
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
|
||||||
|
tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
|
||||||
|
tableInfo.getLogicDeleteSql(true, true));
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
|
||||||
|
return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
|
||||||
|
} else {
|
||||||
|
sqlMethod = SqlMethodDiv.DELETE_BY_ID;
|
||||||
|
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
|
||||||
|
tableInfo.getKeyProperty());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
|
||||||
|
return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL 更新 set 语句
|
||||||
|
*
|
||||||
|
* @param logic 是否逻辑删除注入器
|
||||||
|
* @param ew 是否存在 UpdateWrapper 条件
|
||||||
|
* @param table 表信息
|
||||||
|
* @param alias 别名
|
||||||
|
* @param prefix 前缀
|
||||||
|
* @return sql
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String sqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias,
|
||||||
|
final String prefix) {
|
||||||
|
String sqlScript = table.getAllSqlSet(logic, prefix);
|
||||||
|
if (judgeAliasNull) {
|
||||||
|
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true);
|
||||||
|
}
|
||||||
|
if (ew) {
|
||||||
|
sqlScript += NEWLINE;
|
||||||
|
sqlScript += SqlScriptUtils.convertIf(SqlScriptUtils.unSafeParam(U_WRAPPER_SQL_SET),
|
||||||
|
String.format("%s != null and %s != null", WRAPPER, U_WRAPPER_SQL_SET), false);
|
||||||
|
}
|
||||||
|
sqlScript = convertSet(sqlScript);
|
||||||
|
return sqlScript;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去掉sest 和 suffixOverrides=","代表去掉第一个逗号
|
||||||
|
*
|
||||||
|
* @param sqlScript
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String convertSet(final String sqlScript) {
|
||||||
|
return "<trim prefix=\"\" suffixOverrides=\",\"> " + sqlScript + "\n" + "</trim>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector.methods;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.enums.SqlMethodDiv;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||||
|
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author coder
|
||||||
|
* @date 2023-08-27
|
||||||
|
* @desc 通过MybatisPlus源码 扩展修改
|
||||||
|
*/
|
||||||
|
public class UpdateByIdClickHouse extends AbstractMethod {
|
||||||
|
|
||||||
|
public UpdateByIdClickHouse(String methodName) {
|
||||||
|
super(methodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethodDiv sqlMethod = SqlMethodDiv.UPDATE_BY_ID;
|
||||||
|
|
||||||
|
final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);
|
||||||
|
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
|
||||||
|
this.sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, ENTITY, ENTITY_DOT),
|
||||||
|
tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional);
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addInsertMappedStatement(mapperClass, modelClass,
|
||||||
|
sqlMethod.getMethod(), sqlSource, new NoKeyGenerator(), null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL 更新 set 语句
|
||||||
|
*
|
||||||
|
* @param logic 是否逻辑删除注入器
|
||||||
|
* @param ew 是否存在 UpdateWrapper 条件
|
||||||
|
* @param table 表信息
|
||||||
|
* @param alias 别名
|
||||||
|
* @param prefix 前缀
|
||||||
|
* @return sql
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String sqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias,
|
||||||
|
final String prefix) {
|
||||||
|
String sqlScript = table.getAllSqlSet(logic, prefix);
|
||||||
|
if (judgeAliasNull) {
|
||||||
|
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true);
|
||||||
|
}
|
||||||
|
if (ew) {
|
||||||
|
sqlScript += NEWLINE;
|
||||||
|
sqlScript += SqlScriptUtils.convertIf(SqlScriptUtils.unSafeParam(U_WRAPPER_SQL_SET),
|
||||||
|
String.format("%s != null and %s != null", WRAPPER, U_WRAPPER_SQL_SET), false);
|
||||||
|
}
|
||||||
|
sqlScript = convertSet(sqlScript);
|
||||||
|
return sqlScript;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去掉sest 和 suffixOverrides=","代表去掉第一个逗号
|
||||||
|
*
|
||||||
|
* @param sqlScript
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String convertSet(final String sqlScript) {
|
||||||
|
return "<trim prefix=\"\" suffixOverrides=\",\"> " + sqlScript + "\n" + "</trim>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package cn.iocoder.yudao.framework.clickhouse.injector.methods;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.clickhouse.injector.enums.SqlMethodDiv;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||||
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author coder
|
||||||
|
* @date 2023-08-27
|
||||||
|
* @desc 通过MybatisPlus源码 扩展修改
|
||||||
|
*/
|
||||||
|
public class UpdateClickHouse extends AbstractMethod {
|
||||||
|
|
||||||
|
public UpdateClickHouse(String methodName) {
|
||||||
|
super(methodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
|
SqlMethodDiv sqlMethod = SqlMethodDiv.UPDATE;
|
||||||
|
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
|
||||||
|
sqlSet(true, true, tableInfo, true, ENTITY, ENTITY_DOT),
|
||||||
|
sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
|
return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL 更新 set 语句
|
||||||
|
*
|
||||||
|
* @param logic 是否逻辑删除注入器
|
||||||
|
* @param ew 是否存在 UpdateWrapper 条件
|
||||||
|
* @param table 表信息
|
||||||
|
* @param alias 别名
|
||||||
|
* @param prefix 前缀
|
||||||
|
* @return sql
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String sqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias,
|
||||||
|
final String prefix) {
|
||||||
|
String sqlScript = table.getAllSqlSet(logic, prefix);
|
||||||
|
if (judgeAliasNull) {
|
||||||
|
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true);
|
||||||
|
}
|
||||||
|
if (ew) {
|
||||||
|
sqlScript += NEWLINE;
|
||||||
|
sqlScript += SqlScriptUtils.convertIf(SqlScriptUtils.unSafeParam(U_WRAPPER_SQL_SET),
|
||||||
|
String.format("%s != null and %s != null", WRAPPER, U_WRAPPER_SQL_SET), false);
|
||||||
|
}
|
||||||
|
sqlScript = convertSet(sqlScript);
|
||||||
|
return sqlScript;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去掉sest 和 suffixOverrides=","代表去掉第一个逗号
|
||||||
|
*
|
||||||
|
* @param sqlScript
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String convertSet(final String sqlScript) {
|
||||||
|
return "<trim prefix=\"\" suffixOverrides=\",\"> " + sqlScript + "\n" + "</trim>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
## com.w3.framework.clickhouse.config.ClickHouseConfig
|
||||||
Loading…
Reference in New Issue