Parameter ‘deptName‘ not found.

173 阅读1分钟

Parameter 'runState' not found.

报错信息为Parameter 'deptName' not found.时,是因为xml中的sql标签写法出现错误

dao接口代码:

public List<CargoBudgetReport> query(String[] ids);

mapper.xml的错误代码:

<select id="queryClassReport" parameterType="list" resultType="com.duay.mavenoa.bean.ClassReport">
	select c.* from classreport c, dept, emp
	<where>
		c.empId=emp.empId and emp.deptId=dept.deptId
		
		<if test="deptName !=null" >
			<trim prefix="and">
				<foreach collection="array" open="deptName in(" close=")" item="deptName" separator=",">
					#{deptName}
				</foreach>
			</trim>
		</if>
	</where>
</select>

mapper.xml的正确代码:

<select id="queryClassReport" parameterType="list" resultType="com.duay.mavenoa.bean.ClassReport">
	select c.* from classreport c, dept, emp
	<where>
		c.empId=emp.empId and emp.deptId=dept.deptId
		
		<if test="array !=null" >
			<trim prefix="and">
				<foreach collection="array" open="dept.deptName in(" close=")" item="deptName" separator=",">
					#{deptName}
				</foreach>
			</trim>
		</if>
	</where>
</select>

错误修改的地方为:<if test="deptName !=null" > 改为:<if test="array !=null" >

<foreach collection="array" open="deptName in(" close=")" item="deptName" separator=",">

改为:

<foreach collection="array" open="dept.deptName in(" close=")" item="deptName" separator=",">

如果是多参数往xml.中传值,需要以下变化,不然也会报Parameter '字段' not found.

错误写法:

public List<queryReport> queryClassReport( String[] deptName, String runState, String projectState);

正确写法:

多参数的话必须加上@Param不然mapper文件是不认识的

public List<queryReport> queryClassReport(@Param("deptName") String[] deptName, @Param("runState") String runState, @Param("projectState") String projectState);

它匹配的对应的正确的mapper.xml文件的sql写法

需要注意的一点事,多参数的xml的sql要把parameterType这个属性去掉不要写

<select id="queryClassReport" resultType="com.duay.mavenoa.bean.ClassReport">
	select c.* from classport c, dept, emp
	<where>
		c.empId=emp.empId and emp.deptId=dept.deptId
		<if test="runState != null">
			and c.runState=#{runState}
		</if>
		<if test="projectState != null">
			and c.projectState=#{projectState}
		</if>

		<if test="deptName !=null" >
			<trim prefix="and">
				<foreach collection="deptName" open="dept.deptName in(" close=")" item="deptname" separator=",">
					#{deptname}
				</foreach>
			</trim>
		</if>
	</where>
</select>

其他

自己建立了一个技术群,大家愿意主动学习和分享,愿意一块实现一些有意思的技术,进行理论和实践的交流。这里面有前端和后段,不是单一的一种,方便大家沟通前后端兼容问题

4530a92078099847d0eec48ac91353b.jpg