UDAL 不支持自定義函數,可以用mybatis中的sql標簽進行改造替換
MyBatis中sql標簽定義SQL片段,
include標簽引用,可以復用SQL片段
sql標簽中id屬性對應include標簽中的refid屬性。通過include標簽將sql片段和原sql片段進行拼接成一個完整的sql語句進行執行。
<sql id="sqlid">
res_type_id,res_type
</sql>
<select id="selectbyId" resultType="com.property.vo.PubResTypeVO">
select
<include refid="sqlid"/>
from pub_res_type
</select>
- 引用同一個xml中的sql片段
<include refid="sqlid"/>
- 引用公用的sql片段
<include refid="namespace.sqlid"/>
include標簽中也可以用property標簽,用以指定自定義屬性。在sql標簽中通過${}取出對應的屬性值。
<select id="queryPubResType" parameterType="com.property.vo.PubResTypeVO" resultMap="PubResTypeList">
select a.res_type_id,
<include refid="com.common.dao.FunctionDao.SF_GET_LNG_RES_TYPE">
<property name="AI_RES_TYPE_ID" value="a.res_type_id"/>
<property name="lng" value="#{lngId}"/>
<property name="female" value="'女'"/>
</include> as res_type
from pub_res_type a
</select>
定義一個公用的sql標簽,用databaseId來區分不同數據庫類型
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.common.dao.FunctionDao">
<sql id="SF_GET_LNG_RES_TYPE" databaseId="oracle">
SF_GET_LNG_RES_TYPE(${AI_RES_TYPE_ID}, ${lng})
</sql>
<sql id="SF_GET_LNG_RES_TYPE" databaseId="mysql">
CASE WHEN ${AI_RES_TYPE_ID}=101 THEN
CASE WHEN ${lng} = 1 THEN '男' ELSE 'male' END
CASE WHEN ${AI_RES_TYPE_ID}=102 THEN
CASE WHEN ${lng} = 1 THEN ${female} ELSE 'female' END
END
</sql>
</mapper>
拼接之后
select a.res_type_id,
CASE WHEN a.res_type_id=101 THEN
CASE WHEN #{lngId} = 1 THEN '男' ELSE 'male' END
CASE WHEN a.res_type_id=102 THEN
CASE WHEN #{lngId} = 1 THEN '女' ELSE 'female' END
END as res_type
from pub_res_type a
改造之前,是調用數據庫中的函數
<select id="queryPubResType" parameterType="com.property.vo.PubResTypeVO" resultMap="PubResTypeList">
select a.res_type_id,
SF_GET_LNG_RES_TYPE(a.res_type_id, #{lngId}) as res_type
from pub_res_type a
</select>