原文地址:https://blog.csdn.net/qq_25221835/article/details/86711987
贴个图片,方便查看重点:
或者:
看了上面,大家就应该要猜到我等下要说什么了,我再贴下我之前的问题代码:
错误代码一:
1.所有参数都用 #{}
错误原因:错误的关键就在于 #{table} ,其实本质还是 #{} 和 ${} 的区别
简单理解就是 #{} 会给 你的实参加个 单引号 ,所以 表名就变成了 'tb_plan_result_detail',然后你执行sql:
SELECT a.error_detail as "errorDetail",a.only_id AS "onlyId" from 'tb_plan_result_detail' a 肯定就会出错了
所以表名 必须使用 ${table}。但是你若是 在条件参数中使用 $,就要加 单引号,因为 ${} 不会默认加个单引号,如上面的
a.tablename ='${tableName}'
<select id="findPlanResultDetailInfo" resultType="PlanResultDeatil">
select * from(
SELECT a.error_detail as "errorDetail",
a.only_id AS "onlyId"
from #{table} a
<where>
<if test="resultId != null and resultId != ''">
a.result_id = #{resultId}
</if>
<if test="tableName != null and tableName != ''">
AND a.tablename = #{tableName}
</if>
<if test="fieldName != null and fieldName != ''">
AND a.fieldname = #{fieldName}
</if>
<if test="ruleType != null and ruleType != ''">
AND a.rule_type = #{ruleType}
</if>
<if test="errorType != null and errorType != ''">
AND a.error_type = #{errorType}
</if>
</where>
ORDER BY a.tablename,a.fieldname
)where rownum <= 3
</select>
错误代码二:
这个错误是关于 接口的,直接看代码:
public List<PlanResultDeatil> findPlanResultDetailInfo(@Param("planResultDetail") PlanResultDeatil planResultDetail);
我加这个注解,planResultDetail , 但我 xml中,直接使用的是 planResultDetail类中它的属性,比如#{tableName},其实不使用
@Param("planResultDetail") 反而不会出错,使用了@Param("planResultDetail") ,就一定得在 xml中表明你使用的参数,不然xml无法识别它,因为你声明的是@Param("planResultDetail") ,你凭什么直接使用 #{tableName}。
正解就是 #{planResultDetail.tableName}
总结:
其实,这些都是非常基础的问题,但由于以习惯去认识问题,浪费了很多时间。要多总结,才能多多进步啊~
————————————————
版权声明:本文为CSDN博主「一彡十」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_25221835/article/details/86711987