PostgreSQL数据库执行mapper.xml时遇到结果映射(resultMap)关联的嵌套 Select 查询,多个参数传参入坑记
Posted Hi,all
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL数据库执行mapper.xml时遇到结果映射(resultMap)关联的嵌套 Select 查询,多个参数传参入坑记相关的知识,希望对你有一定的参考价值。
目录
2.mapper.xml 主查询,当前传参方式导致PostgreSQL数据库执行时,大写转小写
1.环境
PostgreSQL数据库、SpringBoot 2.2.5.RELEASE、mybatis-plus 3.31
2.mapper.xml 主查询,当前传参方式导致PostgreSQL数据库执行时,大写转小写
因为:当前${type}传参方式,被认为是普通的字符串。PostgreSQL数据库会自动把大写转换为小写执行
<select id="getUsers" resultMap="getUsersMap">
select u.*,cast(${type} as varchar) as orderType
from
sys_user u
</select>
3.mapper.xml 主查询,保证传参时大写不改变
<select id="getUsers" resultMap="getUsersMap">
select u.*,cast(#{type} as varchar) as orderType
from
sys_user u
</select>
4.结果映射(resultMap)
<resultMap id="getUsersMap" type="com.xxx.SysUser">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="state" property="state"/>
<!--一对多-->
<collection property="orders" select="queryOrders" javaType="list"
ofType="com.xxx.SysOrder" column="{id = id,orderType=orderType}">
<id column="id" property="id"/>
<result column="userId" property="userId"/>
<result column="order_no" property="orderNo"/>
<result column="order_state" property="orderState"/>
<result column="order_type" property="orderType"/>
<result column="address" property="address"/>
</collection>
</resultMap>
5.关联的嵌套 Select 查询
<select id="queryOrders" resultType="com.xxx.SysOrder">
SELECT o.* from sys_order o
<where>
<if test="id != null ">
AND o.user_id = #{id}
</if>
<if test="orderType != null and orderType != '' ">
AND o.order_type = #{orderType}
</if>
</where>
</select>
6. 执行的SQL日志--主查询
SELECT
u.*, CAST ('PAY' AS VARCHAR) AS orderType
FROM
sys_user u
7.执行的SQL日志-关联的嵌套 Select 查询
SELECT
o.*
FROM
sys_order o
WHERE
o.user_id = 1
AND o.order_type = 'PAY'
以上是关于PostgreSQL数据库执行mapper.xml时遇到结果映射(resultMap)关联的嵌套 Select 查询,多个参数传参入坑记的主要内容,如果未能解决你的问题,请参考以下文章
mybatis如何通过接口查找对应的mapper.xml及方法执行详解
mybatis如何通过接口查找对应的mapper.xml及方法执行详解