mybatis中resultmap用法问题

mybatis中resultmap用法问题,第1张

使用现有的Service接口,或者自己在编写一些用到的接口,手动使用Java代码来分别调用Service接口来查出各个model,然后在业务层将model转换为vo,最后返回给前端json串。
为需求相关的页面定义自己的vo,在vo中只定义前端用到的字段。而不是像第一种方式一样vo中一层一层的嵌套model。然后使用sql语句进行表关联,查询用到的字段。组装为vo直接返回
<resultMap id="checkAccountReturn" type="javautilHashMap">
<id property="account_id" column="account_id" />
<result property="sub_account_id" column="sub_account_id"/>
<result property="status" column="status"/>
<result property="account_status" column="account_status"/>
<result property="total_count" column="total_count"/>
<result property="sms_sign" column="sms_sign"/>
</resultMap>

<select id="checkSub_account" resultMap="checkAccountReturn">
SELECT account_id,sub_account_id,status,account_status,total_count,sms_sign
FROM sub_account
<where>
<if test="account_id!=null">
account_id=#{account_id}
</if>
<if test="sub_account_id!=null">
and sub_account_id = #{sub_account_id} and status=1 and total_count!=0 and account_status=1
</if>
</where>
</select>

mybatis的mapperxml里通过判断值来选择不同的语句,xml文件部分内容如下:

项目跑起来后发现两个都没生效,经过资料查找,最后发现由于MyBatis是使用的OGNL表达式,所以单个的字符要写到双引号里面才行,改为<if test=' contain == "0" ' >或者改为<if test=" contain == '0'toString() ">,问题解决。

完全可以

<!-- 根据条件模糊查询 -->
<select id="findTrackerByPuzzy" parameterType="javautilMap"
resultMap="trackerDevice_resultMap">
select
d,ureal_name as u_name
from tb_tracker_device d
left outer join
tb_user u
on uid=duser_id
<where>
<if test="condition !=null and condition !=''">
(dtracker_id like
CONCAT('%',#{condition},'%')
or
dnumber like
CONCAT('%',#{condition},'%')
or 
dimei like
CONCAT('%',#{condition},'%')
or 
dsim like
CONCAT('%',#{condition},'%')
or 
dtelephone like
CONCAT('%',#{condition},'%')
or 
(ureal_name like 
           CONCAT('%',#{condition},'%')
           <if test="channel !=null">
and uchannel=#{channel}
</if>
           ))
</if>
<if test="channel !=null">
and dchannel=#{channel}
</if>
</where>
order by dsync_time desc
</select>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/10540448.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-09
下一篇2023-05-09

发表评论

登录后才能评论

评论列表(0条)

    保存