最近项目内使用MybatisPlus整合Phoenix实现对HBase进行操作,但是Phoenix的sql语法和MySQL不太一样,导致得在列上加@TableField申明列簇名称和列名称,不太友好,所以自己写了个插件拦截sql并进行修改
package org.gjw.config;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.update.Update;
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.List;
import java.util.stream.Collectors;
public class PhoenixMPPlugin extends JsqlParserSupport 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);
mpBs.sql(parserSingle(mpBs.sql(), null));
}
@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) {
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
mpBs.sql(parserMulti(mpBs.sql(), null));
}
}
@Override
protected void processSelect(Select select, int index, String sql, Object obj) {
SelectBody selectBody = select.getSelectBody();
if(selectBody instanceof PlainSelect) reformatPlainSelect((PlainSelect) selectBody);
}
@Override
protected void processInsert(Insert insert, int index, String sql, Object obj) {
System.out.println( "新增前调用,可修改sql" );
}
@Override
protected void processDelete(Delete delete, int index, String sql, Object obj) {
System.out.println( "删除前调用,可修改sql" );
}
@Override
protected void processUpdate(Update update, int index, String sql, Object obj) {
System.out.println("修改调用,可修改sql");
}
private List<SelectItem> disposeSelectColumn(List<SelectItem> selectItems){
return selectItems.stream().map( this::resetSelectItem ).collect(Collectors.toList());
}
private SelectItem resetSelectItem( SelectItem selectItem ){
if( !(selectItem instanceof SelectExpressionItem) ) return selectItem;
SelectExpressionItem item = (SelectExpressionItem)selectItem;
if( item.getExpression() instanceof Column ){
Column columnExp = (Column)item.getExpression();
return new SelectExpressionItem( reFormatSelectColumn( columnExp,item.getAlias() ) );
}
if( item.getExpression() instanceof Function){
Function function = (Function) item.getExpression();
return new SelectExpressionItem( reFormatFunction( function ) );
}
return item;
}
public void reformatPlainSelect(PlainSelect plainSelect){
List<SelectItem> selectItems = plainSelect.getSelectItems();
plainSelect.setSelectItems( disposeSelectColumn( selectItems ) );
plainSelect.setWhere( disposeSelectWhere( plainSelect.getWhere() ) );
}
private Column reFormatSelectColumn( Column columnExp,Alias alias ){
if( columnExp == null ) return columnExp;
String tableAndCFName= columnExp.getTable() == null ? "" : columnExp.getTable().toString();
String columnName= columnExp.getColumnName();
String[] tableAndCFInfo = tableAndCFName.split("\\.");
String tableName = tableAndCFInfo[0];
String cf = tableAndCFInfo[tableAndCFInfo.length - 1];
if( StrUtil.equals(tableName,cf) && StrUtil.isNotBlank(tableName) ){
if( StrUtil.equals(cf.toUpperCase(),cf) ) {
tableName = "";
}else cf = "";
}
StringBuilder finalName = new StringBuilder();
if( StrUtil.isNotBlank( tableName ) ) finalName.append( tableName ).append( "." );
if( StrUtil.isNotBlank( cf ) ) finalName.append( appendPrefixAndSuffix(cf) ).append(".");
finalName.append( appendPrefixAndSuffix(columnName) );
if( alias !=null ) finalName.append(" ").append( alias.getName() );
return new Column( finalName.toString() );
}
private Function reFormatFunction( Function function ){
List<Expression> expressions = function.getParameters().getExpressions();
expressions = expressions.stream().map(exp -> {
if (exp instanceof Column) return reFormatSelectColumn((Column) exp, null);
return exp;
}).collect(Collectors.toList());
function.getParameters().setExpressions(expressions);
return function;
}
private SubSelect reFormatSubSelect( SubSelect subSelect ){
if( subSelect.getSelectBody() instanceof PlainSelect ){
reformatPlainSelect( (PlainSelect)subSelect.getSelectBody() );
}
return subSelect;
}
public Expression disposeSelectWhere(Expression expression){
if( !(expression instanceof BinaryExpression) ) return expression;
BinaryExpression binaryExpression =(BinaryExpression)expression;
if( binaryExpression.getLeftExpression() instanceof BinaryExpression){
disposeSelectWhere( binaryExpression.getLeftExpression() );
}
if( binaryExpression.getRightExpression() instanceof BinaryExpression){
disposeSelectWhere( binaryExpression.getRightExpression() );
}
if( binaryExpression.getLeftExpression() instanceof Column ){
Column newColumn = reFormatSelectColumn((Column) binaryExpression.getLeftExpression(), null);
binaryExpression.setLeftExpression( newColumn );
}
if(binaryExpression.getLeftExpression() instanceof SubSelect){
SubSelect subSelect = (SubSelect)binaryExpression.getLeftExpression();
if( subSelect.getSelectBody() instanceof PlainSelect ){
reformatPlainSelect( (PlainSelect)subSelect.getSelectBody() );
}
}
if( binaryExpression.getRightExpression() instanceof Column ){
Column newColumn = reFormatSelectColumn((Column) binaryExpression.getLeftExpression(), null);
binaryExpression.setRightExpression( newColumn );
}
if( binaryExpression.getRightExpression() instanceof SubSelect){
SubSelect subSelect = (SubSelect)binaryExpression.getRightExpression();
reFormatSubSelect( subSelect );
}
return binaryExpression;
}
private String appendPrefixAndSuffix(String str){
final String PREFIX = "\"";
final String SUFFIX = "\"";
if( str.contains(PREFIX) ) return str;
return new StringBuilder().append(PREFIX).append(str).append(SUFFIX).toString();
}
}
使用: 编写配置类配置MybatisPlus并设置插件
@MapperScan(value = "org.gjw.mapper.phoenix",sqlSessionTemplateRef = "phoenixSqlSessionTemplate",sqlSessionFactoryRef = "phoenixSqlSessionFactory")
@Configuration
public class PhoenixConfig {
@Bean
@ConfigurationProperties("spring.datasource.phoenix")
public DataSource phoenixDataSource(){
return new HikariDataSource();
}
@Bean
public SqlSessionFactory phoenixSqlSessionFactory( @Qualifier("phoenixDataSource") @Autowired DataSource phoenixDataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource( phoenixDataSource() );
sqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:/phoenixMapper/**/*.xml"));
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor( new PhoenixMPPlugin() );
sqlSessionFactoryBean.setPlugins( interceptor );
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setMapUnderscoreToCamelCase(true);
mybatisConfiguration.setLogImpl(StdOutImpl.class);
sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate phoenixSqlSessionTemplate( @Qualifier("phoenixSqlSessionFactory") @Autowired SqlSessionFactory phoenixSqlSessionFactory){
return new SqlSessionTemplate( phoenixSqlSessionFactory );
}
@Bean
public DataSourceTransactionManager phoenixDataSourceTransactionManager(@Qualifier("phoenixDataSource") @Autowired DataSource phoenixDataSource){
return new DataSourceTransactionManager(phoenixDataSource);
}
}
|