在使用mybaitis传参数的时候,如果仅传入一个类型为String的参数,那么在 xml文件中应该使用_parameter来代替参数名。
正确的写法:
<select id="getDeptInfo" parameterType="String" resultType="com.gskdht.pushmessage.model.DeptInfo">
SELECT DEPT_ID,PARENT_ID,DEPT_NAME,DEPT_LEVEL from PARA_DEPT
<where>
<if test="_parameter != '1'.toString() ">
PARENT_ID = #{_parameter}
</if>
</where>
</select>
错误的写法:
<select id="getDeptInfo" parameterType="String" resultType="com.gskdht.pushmessage.model.DeptInfo">
SELECT DEPT_ID,PARENT_ID,DEPT_NAME,DEPT_LEVEL from PARA_DEPT
<where>
<if test="parentId != '1'.toString() ">
PARENT_ID = #{parentId}
</if>
</where>
</select>
也可以在mapper的接口中,给这个方法的参数加上@Param(value=“parentId”),这样就能在.xml中使用#{parentId} 了。
如:public List<DeptInfo> getDeptInfo(@Param("parentId") String parentId)
这样也是可以的。
转载自:https://blog.youkuaiyun.com/ybcljay/article/details/80831607
本文详细介绍了在MyBatis中如何正确地处理单个String类型的参数传递,特别是在XML映射文件中的使用方法。通过对比正确与错误的示例,阐述了使用_parameter作为参数标识符的重要性,并展示了如何通过@Param注解实现更灵活的参数命名。
357

被折叠的 条评论
为什么被折叠?



