mybatis plus order by 不支持convert函数

最近业务上有个需求,要按照企业名称中文进行排序显示。项目使用的是mybatisplus + mysql

从网上看到的排序解决方法是使用mysql 的 convert函数:

select * from 客户表 where *** order by convert(`企业名称` using GBK);

为什么要使用convert函数那?因为一般使用的数据编码是utf-8,mysql中utf-8的默认排序规则是utf8_general_ci ,它的排序顺序并不是按照拼音来排序的。

这样就需要吧utf-8的字符转译成gbk编码,就是convert函数的作用

但是!!!

mybatis plus 中如果使用了上述的convert函数来进行排序的话,运行时会报错:

 

现阶段解决方法:

使用 cast(xxx as char character set gbk) 函数即可,亲测有效!!

那么cast和convert函数有什么区别那?

答,基本没有区别,唯一的区别就是cast是标准函数(ANSI标准语法),而convert函数不是。仅此而已

mysql官方文档如下:

To convert strings between character sets, you can also use CONVERT(exprtype) syntax (without USING), or CAST(expr AS type), which is equivalent:

CONVERT(string, CHAR[(N)] CHARACTER SET charset_name)
CAST(string AS CHAR[(N)] CHARACTER SET charset_name)

 

问题原因:

这个详见 mybatis-plus issue :https://gitee.com/baomidou/mybatis-plus/issues/I449PW 

mybatis是使用 jsqlparse库来进行解析sql的,这个问题就出在jsqlparse库上

详见:jsqlparse issue : https://github.com/JSQLParser/JSqlParser/issues/1767

jsqlparse 5.0 解决了这个问题,

详见:https://github.com/JSQLParser/JSqlParser/pull/1778

因为现在mybatis使用的jsqlparse还是4.6版本的,所以这个问题暂时无解。不知道mybatis什么时候会升级jsqlparse库版本

 

posted @ 2023-11-22 16:19  刀客八号  阅读(575)  评论(0编辑  收藏  举报