标签:mode size sql where model class mybatis c编程 use
问题:使用mybatis时 有一个sql查询不到结果 日志也显示查询为o 但是从日志中取出执行的sql到数据库客户端手动执行,可以查到数据;
原因:MyBatis看到 #{}会认为你在给sql中的变量赋值,就像JDBC编程中给问号赋值一样(自动在前后加单引号)也就是说,他把你传入的字符串并没有当做多个值,而是当做一个大的字符串,所以查询不到值
而MyBatis看到${}的时候会直接将之替换成变量的值而不做任何处理;
解决方案:将查询条件中的“#”替换成“$”。
<select id="findAllToExcel" parameterType="com.aspire.sms2019.model.dto.ProductQueryCondition" resultType="map"> SELECT cp.ID id, cp.`NAME` `name`, cp.`STATUS` `status`, cp.CREATE_BY createBy, DATE_FORMAT(cp.CREATE_TIME,‘%Y-%m-%d %H:%i:%s‘) createTime, cp.UPDATE_BY updateBy, DATE_FORMAT(cp.UPDATE_TIME,‘%Y-%m-%d %H:%i:%s‘) updateTime, c1.`NAME` cmppName, c4.`NAME` cmppName1, c5.`NAME` cmppName2, cp.CHANNEL_CMPP_USED cmppNameUsed, GROUP_CONCAT(distinct concat(c13.`NAME`,‘:‘, case cpr1.STATUS when 0 THEN ‘无效‘ ELSE ‘有效‘end ) ORDER BY cpr1.PRIORITY desc) cmppPriorityName, c2.`NAME` sgipName, c7.`NAME` sgipName1, c8.`NAME` sgipName2, cp.CHANNEL_SGIP_USED sgipNameUsed, GROUP_CONCAT(distinct concat(c14.`NAME`,‘:‘, case cpr2.STATUS when 0 THEN ‘无效‘ ELSE ‘有效‘end ) ORDER BY cpr2.PRIORITY desc) sgipPriorityName, c3.`NAME` smgpName, c10.`NAME` smgpName1, c11.`NAME` smgpName2, cp.CHANNEL_SMGP_USED smgpNameUsed, GROUP_CONCAT(distinct concat(c15.`NAME`,‘:‘, case cpr3.STATUS when 0 THEN ‘无效‘ ELSE ‘有效‘end ) ORDER BY cpr3.PRIORITY desc) smgpPriorityName FROM channel_product cp LEFT OUTER JOIN channel c1 ON cp.CHANNEL_CMPP_ID = c1.id LEFT OUTER JOIN channel c4 ON cp.CHANNEL_CMPP_ID1 = c4.id LEFT OUTER JOIN channel c5 ON cp.CHANNEL_CMPP_ID2 = c5.id LEFT OUTER JOIN channel c2 ON cp.CHANNEL_SGIP_ID = c2.id LEFT OUTER JOIN channel c7 ON cp.CHANNEL_SGIP_ID1 = c7.id LEFT OUTER JOIN channel c8 ON cp.CHANNEL_SGIP_ID2 = c8.id LEFT OUTER JOIN channel c3 ON cp.CHANNEL_SMGP_ID = c3.id LEFT OUTER JOIN channel c10 ON cp.CHANNEL_SMGP_ID1 = c10.id LEFT OUTER JOIN channel c11 ON cp.CHANNEL_SMGP_ID2 = c11.id left outer join channel_priority cpr1 on (cpr1.PRODUCT_ID = cp.ID and cpr1.CHANNEL_TYPE=0) left outer join channel c13 on (cpr1.CHANNEL_ID =c13.ID and cpr1.CHANNEL_TYPE = 0) left outer join channel_priority cpr2 on (cpr2.PRODUCT_ID = cp.ID and cpr2.CHANNEL_TYPE=1) left outer join channel c14 on (cpr2.CHANNEL_ID =c14.ID and cpr2.CHANNEL_TYPE = 1) left outer join channel_priority cpr3 on (cpr3.PRODUCT_ID = cp.ID and cpr3.CHANNEL_TYPE=2) left outer join channel c15 on (cpr3.CHANNEL_ID =c15.ID and cpr3.CHANNEL_TYPE = 2) <where> <if test="productName != null and productName != ‘‘"> AND cp.NAME like CONCAT(‘%‘,${productName},‘%‘) </if> <if test="status != null"> AND cp.STATUS = #{status} </if> </where> GROUP BY cp.ID ORDER BY cp.ID DESC </select>
开发中遇到的问题---【使用mybatis时 有一个sql查询不到结果 日志也显示查询为o 但是从日志中取出执行的sql到数据库客户端手动执行,可以查到数据】
标签:mode size sql where model class mybatis c编程 use
原文地址:https://www.cnblogs.com/hujunwei/p/12515130.html