springcloud下hibernate本地化方言配置,通过application.yml进行配置,通过自定义一个方言类配置到application.yml中,以mysql中convert为例,convert能够将汉字以首字符方式进行排序
ORDER BY convert(a.userName using 'gbk') DESC
如果是ssh看这里https://blog.csdn.net/myfmyfmyfmyf/article/details/45503919
1、创建一个方言类,继承org.hibernate.dialect.MySQL5Dialect
在com.muyunfei.hibernateDialect包下创建一个LocalMysqlDialect .java类,编写无参构造,继承父类的构造super(),增加registerFunction注册一个函数
package com.muyunfei.hibernateDialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.type.StringType;
public class LocalMysqlDialect extends org.hibernate.dialect.MySQL5Dialect {
public LocalMysqlDialect(){
super();
registerFunction("convert", new SQLFunctionTemplate(StringType.INSTANCE, "convert(?1 using ?2)"));
}
}
2、更改配置文件
将自定义的方言类,配置到spring.jpa.properties.hibernate.dialect下
spring:
jpa:
show-sql: false
properties:
hibernate:
jdbc:
batch_size: 100
order_inserts: true
order_updates: true
dialect: com.muyunfei.hibernateDialect.LocalMysqlDialect
3、使用
将convert作为正常函数使用即可
hql.append(" ORDER BY convert(a.userName , gbk ) DESC ");
注意:convert函数有点特殊,在方言里我们需要定义"convert(?1 using ?2)"),但是使用时需要convert(a.userName , gbk )
扫描二维码关注公众号,回复:
11629898 查看本文章