parent
14b2d5db6b
commit
8b9cf07b70
4 changed files with 4 additions and 149 deletions
@ -1,43 +0,0 @@ |
||||
package net.mingsoft.config; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType; |
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; |
||||
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; |
||||
import net.mingsoft.interceptor.DMInnerInterceptor; |
||||
import net.mingsoft.interceptor.MysqlInnerInterceptor; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import javax.sql.DataSource; |
||||
import java.sql.SQLException; |
||||
|
||||
/** |
||||
* Mybatis 拦截器配置 |
||||
*/ |
||||
@Configuration |
||||
public class MybatisInterceptorConfig { |
||||
|
||||
@Bean |
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(DataSource dataSource, @Autowired(required = false) TenantLineHandler tenantLineHandler) { |
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
||||
if(tenantLineHandler!=null){ |
||||
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler)); |
||||
} |
||||
try { |
||||
//mysql 添加转换sql
|
||||
DbType dbType = JdbcUtils.getDbType(dataSource.getConnection().getMetaData().getURL()); |
||||
if(DbType.MYSQL==dbType){ |
||||
interceptor.addInnerInterceptor(new MysqlInnerInterceptor()); |
||||
}else if(DbType.DM==dbType){ |
||||
interceptor.addInnerInterceptor( new DMInnerInterceptor()); |
||||
} |
||||
} catch (SQLException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
return interceptor; |
||||
} |
||||
} |
||||
@ -1,61 +0,0 @@ |
||||
package net.mingsoft.interceptor; |
||||
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
||||
import com.baomidou.mybatisplus.core.toolkit.TableNameParser; |
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
||||
import org.apache.ibatis.executor.Executor; |
||||
import org.apache.ibatis.executor.statement.StatementHandler; |
||||
import org.apache.ibatis.mapping.BoundSql; |
||||
import org.apache.ibatis.mapping.MappedStatement; |
||||
import org.apache.ibatis.mapping.SqlCommandType; |
||||
import org.apache.ibatis.session.ResultHandler; |
||||
import org.apache.ibatis.session.RowBounds; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.SQLException; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class DMInnerInterceptor implements InnerInterceptor { |
||||
|
||||
@Override |
||||
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { |
||||
PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); |
||||
if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; |
||||
mpBs.sql(this.changeTable(mpBs.sql())); |
||||
} |
||||
|
||||
@Override |
||||
public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { |
||||
PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); |
||||
MappedStatement ms = mpSh.mappedStatement(); |
||||
SqlCommandType sct = ms.getSqlCommandType(); |
||||
if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { |
||||
if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; |
||||
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); |
||||
mpBs.sql(this.changeTable(mpBs.sql())); |
||||
} |
||||
} |
||||
|
||||
protected String changeTable(String sql) { |
||||
TableNameParser parser = new TableNameParser(sql); |
||||
List<TableNameParser.SqlToken> names = new ArrayList<>(); |
||||
parser.accept(names::add); |
||||
StringBuilder builder = new StringBuilder(); |
||||
int last = 0; |
||||
for (TableNameParser.SqlToken name : names) { |
||||
int start = name.getStart(); |
||||
if (start != last) { |
||||
builder.append(sql, last, start); |
||||
String value = name.getValue(); |
||||
builder.append(String.format("\"%s\"",value)); |
||||
} |
||||
last = name.getEnd(); |
||||
} |
||||
if (last != sql.length()) { |
||||
builder.append(sql.substring(last)); |
||||
} |
||||
return builder.toString(); |
||||
} |
||||
} |
||||
@ -1,45 +0,0 @@ |
||||
package net.mingsoft.interceptor; |
||||
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
||||
import org.apache.ibatis.executor.Executor; |
||||
import org.apache.ibatis.executor.statement.StatementHandler; |
||||
import org.apache.ibatis.mapping.BoundSql; |
||||
import org.apache.ibatis.mapping.MappedStatement; |
||||
import org.apache.ibatis.mapping.SqlCommandType; |
||||
import org.apache.ibatis.session.ResultHandler; |
||||
import org.apache.ibatis.session.RowBounds; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.SQLException; |
||||
|
||||
/** |
||||
* Mysql 适配通用拦截器,主要处理不支持的sql语句 |
||||
*/ |
||||
public class MysqlInnerInterceptor implements InnerInterceptor { |
||||
|
||||
@Override |
||||
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { |
||||
PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); |
||||
if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; |
||||
mpBs.sql(this.changeSql(mpBs.sql())); |
||||
} |
||||
|
||||
@Override |
||||
public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { |
||||
PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); |
||||
MappedStatement ms = mpSh.mappedStatement(); |
||||
SqlCommandType sct = ms.getSqlCommandType(); |
||||
if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { |
||||
if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; |
||||
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); |
||||
mpBs.sql(this.changeSql(mpBs.sql())); |
||||
} |
||||
} |
||||
|
||||
protected String changeSql(String sql) { |
||||
//替换所有"为`
|
||||
return sql.replaceAll("\"","`"); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue