在使用criteria api进行查询时 criteriaBuilder只提供了一个部分标准的sql函数,但当我们需要使用oracle特有的行转列函数wm_concat或

mysql特有的行转列函数group_concat时,就只能自己封装了,

criteriaBuilder提供了一个封装函数的方法:

    /**
* Create an expression for the execution of a database
* function.
* @param name function name
* @param type expected result type
* @param args function arguments
* @return expression
*/
<T> Expression<T> function(String name, Class<T> type,
Expression<?>... args);

如我们封装wm_concat函数,代码如下:

Expression<String> wmConcat = cb.function("wm_concat",
String.class, root.get("ID"));

生成的sql 如:select  wm_concat(id)........

如果我们想生成 select wm_concat(distinct(id))这个的形式,来去id进行去重distinct。可以用下面的代码,对function进行嵌套:

        Expression<String> distinct = cb.function("distinct",
String.class, root.get("ID")); Expression<String> wmConcat = cb.function("wm_concat",
String.class, distinct);

当然这还没有完,生成这样的函数后,其实jpa是不认可wm_concat函数的,(distinct函数,lower函数等是认可的),这是为什么呢,因为jpa认可的oracle函数都会在OracleDialect定义。

如我使用的OracleDialect版本是Oracle12cDialect,它本身没有注册自定义函数,但是它继承的类Oracle8iDialect注册了很多函数,如下:

protected void registerFunctions() {
registerFunction( "abs", new StandardSQLFunction("abs") );
registerFunction( "sign", new StandardSQLFunction("sign", StandardBasicTypes.INTEGER) ); registerFunction( "acos", new StandardSQLFunction("acos", StandardBasicTypes.DOUBLE) );
registerFunction( "asin", new StandardSQLFunction("asin", StandardBasicTypes.DOUBLE) );
registerFunction( "atan", new StandardSQLFunction("atan", StandardBasicTypes.DOUBLE) );
registerFunction( "bitand", new StandardSQLFunction("bitand") );
registerFunction( "cos", new StandardSQLFunction("cos", StandardBasicTypes.DOUBLE) );
registerFunction( "cosh", new StandardSQLFunction("cosh", StandardBasicTypes.DOUBLE) );
registerFunction( "exp", new StandardSQLFunction("exp", StandardBasicTypes.DOUBLE) );
registerFunction( "ln", new StandardSQLFunction("ln", StandardBasicTypes.DOUBLE) );
registerFunction( "sin", new StandardSQLFunction("sin", StandardBasicTypes.DOUBLE) );
registerFunction( "sinh", new StandardSQLFunction("sinh", StandardBasicTypes.DOUBLE) );
registerFunction( "stddev", new StandardSQLFunction("stddev", StandardBasicTypes.DOUBLE) );
registerFunction( "sqrt", new StandardSQLFunction("sqrt", StandardBasicTypes.DOUBLE) );
registerFunction( "tan", new StandardSQLFunction("tan", StandardBasicTypes.DOUBLE) );
registerFunction( "tanh", new StandardSQLFunction("tanh", StandardBasicTypes.DOUBLE) );
registerFunction( "variance", new StandardSQLFunction("variance", StandardBasicTypes.DOUBLE) ); registerFunction( "round", new StandardSQLFunction("round") );
registerFunction( "trunc", new StandardSQLFunction("trunc") );
registerFunction( "ceil", new StandardSQLFunction("ceil") );
registerFunction( "floor", new StandardSQLFunction("floor") ); registerFunction( "chr", new StandardSQLFunction("chr", StandardBasicTypes.CHARACTER) );
registerFunction( "initcap", new StandardSQLFunction("initcap") );
registerFunction( "lower", new StandardSQLFunction("lower") );
registerFunction( "ltrim", new StandardSQLFunction("ltrim") );
registerFunction( "rtrim", new StandardSQLFunction("rtrim") );
registerFunction( "soundex", new StandardSQLFunction("soundex") );
registerFunction( "upper", new StandardSQLFunction("upper") );
registerFunction( "ascii", new StandardSQLFunction("ascii", StandardBasicTypes.INTEGER) ); registerFunction( "to_char", new StandardSQLFunction("to_char", StandardBasicTypes.STRING) );
registerFunction( "to_date", new StandardSQLFunction("to_date", StandardBasicTypes.TIMESTAMP) ); registerFunction( "current_date", new NoArgSQLFunction("current_date", StandardBasicTypes.DATE, false) );
registerFunction( "current_time", new NoArgSQLFunction("current_timestamp", StandardBasicTypes.TIME, false) );
registerFunction( "current_timestamp", new NoArgSQLFunction("current_timestamp", StandardBasicTypes.TIMESTAMP, false) ); registerFunction( "last_day", new StandardSQLFunction("last_day", StandardBasicTypes.DATE) );
registerFunction( "sysdate", new NoArgSQLFunction("sysdate", StandardBasicTypes.DATE, false) );
registerFunction( "systimestamp", new NoArgSQLFunction("systimestamp", StandardBasicTypes.TIMESTAMP, false) );
registerFunction( "uid", new NoArgSQLFunction("uid", StandardBasicTypes.INTEGER, false) );
registerFunction( "user", new NoArgSQLFunction("user", StandardBasicTypes.STRING, false) ); registerFunction( "rowid", new NoArgSQLFunction("rowid", StandardBasicTypes.LONG, false) );
registerFunction( "rownum", new NoArgSQLFunction("rownum", StandardBasicTypes.LONG, false) ); // Multi-param string dialect functions...
registerFunction( "concat", new VarArgsSQLFunction(StandardBasicTypes.STRING, "", "||", "") );
registerFunction( "instr", new StandardSQLFunction("instr", StandardBasicTypes.INTEGER) );
registerFunction( "instrb", new StandardSQLFunction("instrb", StandardBasicTypes.INTEGER) );
registerFunction( "lpad", new StandardSQLFunction("lpad", StandardBasicTypes.STRING) );
registerFunction( "replace", new StandardSQLFunction("replace", StandardBasicTypes.STRING) );
registerFunction( "rpad", new StandardSQLFunction("rpad", StandardBasicTypes.STRING) );
registerFunction( "substr", new StandardSQLFunction("substr", StandardBasicTypes.STRING) );
registerFunction( "substrb", new StandardSQLFunction("substrb", StandardBasicTypes.STRING) );
registerFunction( "translate", new StandardSQLFunction("translate", StandardBasicTypes.STRING) ); registerFunction( "substring", new StandardSQLFunction( "substr", StandardBasicTypes.STRING ) );
registerFunction( "locate", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "instr(?2,?1)" ) );
registerFunction( "bit_length", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "vsize(?1)*8" ) );
registerFunction( "coalesce", new NvlFunction() ); // Multi-param numeric dialect functions...
registerFunction( "atan2", new StandardSQLFunction("atan2", StandardBasicTypes.FLOAT) );
registerFunction( "log", new StandardSQLFunction("log", StandardBasicTypes.INTEGER) );
registerFunction( "mod", new StandardSQLFunction("mod", StandardBasicTypes.INTEGER) );
registerFunction( "nvl", new StandardSQLFunction("nvl") );
registerFunction( "nvl2", new StandardSQLFunction("nvl2") );
registerFunction( "power", new StandardSQLFunction("power", StandardBasicTypes.FLOAT) ); // Multi-param date dialect functions...
registerFunction( "add_months", new StandardSQLFunction("add_months", StandardBasicTypes.DATE) );
registerFunction( "months_between", new StandardSQLFunction("months_between", StandardBasicTypes.FLOAT) );
registerFunction( "next_day", new StandardSQLFunction("next_day", StandardBasicTypes.DATE) ); registerFunction( "str", new StandardSQLFunction("to_char", StandardBasicTypes.STRING) );
}

通过调试代码,可以看到oracledialect共注册了80个函数。

同理,我们也可继承Oracle12cDialect,添加自己的函数:

public class MyOracleDialect extends Oracle12cDialect {

    /**
* 添加oracler内置函数和自定义函数
*/
@Override
protected void registerFunctions() {
super.registerFunctions();
registerFunction("wm_concat", new StandardSQLFunction("wm_concat", StandardBasicTypes.STRING));
} /**
* 解决启动时数字溢出的错误
* @return
*/
@Override
public String getQuerySequencesString() {
return "select * from user_sequences";
}
}

配置完成后,启动项目即可。

Jpa 重写方言dialect 使用oracle / mysql 数据库自定义函数的更多相关文章

  1. Hibernate根据方言dialect动态连接多数据库

    Hibernate根据方言dialect动态连接多数据库 由于最近需要在web项目中动态链接不同地址的数据库,且要链接的子数据库的信息在总数据库(即web项目的主数据库)的表中,所以动手写了一个类. ...

  2. 【转】MySql数据库--mysql_real_escape_string()函数

    MySql数据库--mysql_real_escape_string()函数 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to ...

  3. MySQL数据库聚合函数

    +++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据库聚合函数时间:2019年2月25日内容:MySQL数据库聚合函数重点:MySQL数据库聚合函 ...

  4. mysql数据库-定义函数-存储过程写法

    ------------- mysql  定义自定义函数写法 DELIMITER $$ USE `iwmsdb`$$ DROP FUNCTION IF EXISTS `F_WM_DBNAME`$$ C ...

  5. navicat与phpmyadmin做mysql的自定义函数和事件

    自定义函数和事件是mysql一个很方便的功能,navicat在5.1以上版本就支持了自定义函数和事件,phpmyadmim不清楚. 用这个是由于一些简单的事情,没有必要去做一个服务器计划使用 接下来我 ...

  6. Mysql - 存储过程/自定义函数

    在数据库操作中, 尤其是碰到一些复杂一些的系统, 不可避免的, 会用到函数/自定义函数, 或者存储过程. 实际项目中, 自定义函数和存储过程是越少越好, 因为这个东西多了, 也是一个非常难以维护的地方 ...

  7. mysql创建自定义函数与存储过程

    mysql创建自定义函数与存储过程 一 创建自定义函数 在使用mysql的过程中,mysql自带的函数可能不能完成我们的业务需求,这时就需要自定义函数,例如笔者在开发过程中遇到下面这个问题 mysql ...

  8. Oracle存储过程和自定义函数

    新博客文章链接,欢迎大家评论探讨 概述 存储过程和存储函数是指存储在数据库中供所有用户程序调用的子程序叫存储过程.存储函数. 异同点: 存储过程和存储函数的相同点:完成特定功能的程序. 存储过程和存储 ...

  9. Jmeter连接DB2/ORACLE/MYSQL数据库

    连接DB2 1.将db2数据库驱动db2java.jar.db2jcc.jar放入jmeter的lib/下,同时也要放入本地jdk目录下例如:C:\Program Files\Java\jdk1.7. ...

随机推荐

  1. AcWing:110. 防晒(贪心)

    有C头奶牛进行日光浴,第i头奶牛需要minSPF[i]到maxSPF[i]单位强度之间的阳光. 每头奶牛在日光浴前必须涂防晒霜,防晒霜有L种,涂上第i种之后,身体接收到的阳光强度就会稳定为SPF[i] ...

  2. python学习之路(17)

    sorted 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大 ...

  3. jQuery 全选和反选demo

    前段时间做了一个全选和反选的功能,最近不忙了,做了一个简化版的demo. 全部代码如下: <!DOCTYPE html> <html> <head> <tit ...

  4. C++入门经典-例4.5-利用循环求n的阶乘

    1:代码如下: // 4.5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  5. C++入门经典-例2.9-输出十六进制数以及大写的十六进制数

    1:代码如下: #include "stdafx.h" #include <iostream> #include <iomanip> using names ...

  6. Java基础教程免费分享

    这是我自己早前听课时整理的Java全套知识,适用于初学者,也可以适用于中级进阶的人,你们可以下载,我认为是比较系统全面的,可以抵得上市场上90%的学习资料.讨厌那些随便乱写的资料还有拿出来卖钱的人!在 ...

  7. DS博客大作业--树

    1.树的存储结构说明 树节点结构体 data:文件名 brother:兄弟节点 child:孩子节点 type:节点的类型,0为文件,1为目录 h:节点所在的层次 2.树的函数说明 头文件 函数1:C ...

  8. react属性之exact

    exact是Route下的一个属性,react路由会匹配到所有能匹配到的路由组件,exact能够使得路由的匹配更严格一些. exact的值为bool型,为true时表示严格匹配,为false时为正常匹 ...

  9. Keepalived + LVS-NAT 实现高可用四层 TCP/UDP 负载均衡器

    目录 文章目录 目录 前文列表 在 LVS1/2 安装 Keepalived & LVS Keepalived + LVS-NAT 实现 TCP 负载均衡 IP 规划 网络架构参考 LVS1 ...

  10. Appium+python自动化-输入中文

    一.定位搜索 1.打开淘宝点击搜索按钮,进入到搜索页面 2.然后定位到搜索框后用sendkeys方法输入‘hao’,这里定位元素使用uiautomatorviewer工具即可 3.脚本如下,输入的是英 ...