mybatis-plus关键字解析出错

206 阅读1分钟

mybatis-plus关键字解析出错

mybatis-plus 引入MybatisPlusInterceptor租户插件,sql解析出错

image.png

配置插件

在mybatis中配置插件,参考官网配置

@Configuration
@MapperScan("com.*.**.domain")
public class MybatisPlusConfig {
    @Autowired(required = false) //可以关闭多租户的功能,所以这里只能不强制注入
    private TenantProperties tenantProperties;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(TenantProperties properties) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties));
        // 添加到 interceptor 中
        interceptor.addInnerInterceptor(inner);
        // 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
//        MyBatisUtils.addInterceptor(interceptor, inner, 0);
        return interceptor;
    }
}

问题复现

执行发现有个sql出错了,没有引入插件是没问题的。

SELECT  hope_wscd,hope_wsnm,county_adcd,ws_area,status,w_shp_url,r_shp_url,area_url,moditime,del_flag,geo_polygon_area  FROM ia_wata_hope 
WHERE (BINARY hope_wscd = 'FJWGD551D3DA500000') 

异常:

net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: 
"BINARY" "BINARY" at line 3, column 9. Was expecting one of: "!" "(" "NOT"

**JSqlParser**找到了解决方法

image.png

总结

关键字要注意处理,插件解析的时候会出问题 BINARY hope_wscd = 'FJWGD551D3DA500000' ==> CAST(hope_wscd AS BINARY) ='FJWGD551D3DA500000'

==>  Preparing: SELECT hope_wscd, hope_wsnm, county_adcd, ws_area, status, w_shp_url, r_shp_url, area_url, moditime, del_flag, geo_polygon_area FROM ia_wata_hope WHERE (CAST(hope_wscd AS BINARY) = ?) AND ia_wata_hope.tenant_id = 1
==> Parameters: WFH10G012E100000(String)
<==      Total: 0