MyBatis传入多参数问题

本文详细介绍了在MyBatis 3.5.6中如何处理多参数传入的问题,包括使用arg0、arg1的方式,使用Map封装参数以及推荐的注解方法。针对报错'Parameter ‘employee’ not found',提出了修改映射文件和接口的解决方案,并提供了测试代码以验证正确性。最后,强调了注解方式的简洁性和易读性,推荐在实际开发中使用。
摘要由CSDN通过智能技术生成

1.情景再现 Mybatis3.5.6

想通过传入的id修改表中对应数据的lastName字段值

1.1Dao接口

public interface EmployeeDao {
	public void updateEmployeeById(Employee employee,Integer id);
}

1.2映射文件EmployeeMapper.xml

<mapper namespace="com.dao.EmployeeDao">
    <update id="updateEmployee" >
        update employee set lastName=#{employee.lastName}where id=#{id} 
    </update>
</mapper>

1.3测试代码

@Test
    public void testUpdate()
    {
        SqlSessionFactory sqlSessionFactory=GetSqlSessionFactory.getInstance();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        try{
	        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
	        mapper.updateEmployee(new Employee(null,"test",null,null),5);
        }finally{
        	sqlSession.close();
        }
    }

1.4报错

org.apache.ibatis.exceptions.PersistenceException:
Error updating database. Cause: org.apache.ibatis.binding.BindingException: Parameter ‘employee’ not found.Available parameters are [arg1, arg0, param1, param2]
The error may exist in EmployeeMapper.xml

关键是:Available parameters are [arg1, arg0, param1, param2] MyBatis3.4之前是Available parameters are [0, 1, param1, param2]

2.多参数传入的解决方法

2.1使用arg0、agr1…

原理分析:MyBatis将多个参数封装成一个HashMap,其中key为:“arg0”、“agr1”;value为传入的参数值

根据报错提示信息,修改mapper.xml为

<mapper namespace="com.dao.EmployeeDao">
    <update id="updateEmployee" >
        update employee set lastName=#{arg0.lastName}where id=#{arg1} 
    </update>
</mapper>

2.2使用Map封装传入参数

修改Dao接口为

public interface EmployeeDao {
	public void updateEmployeeById(Map<?,?>map);
}

修改mapper.xml,key为映射文件中填入的名字即#{key}

<mapper namespace="com.dao.EmployeeDao">
    <update id="updateEmployee" >
        update employee set lastName=#{lastName}where id=#{id} 
    </update>
</mapper>

修改测试代码,key为映射文件中填入的名字即#{key}

public void testUpdate()
    {
        Map<String,Object> map=new HashMap<>();
        SqlSessionFactory sqlSessionFactory=GetSqlSessionFactory.getInstance();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        try{
	        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
	        map.put("lastName","test");
	        map.put("id",5);
	        mapper.updateEmployee(map);
	    }finally{
        	sqlSession.close();
        }
    }

2.3使用注解(推荐使用)

原理分析:MyBatis将多个参数封装成一个HashMap,用注解来明确指定key值,而不是自动生成;value为传入参数。

修改Dao接口为

public interface EmployeeDao {
	    public void updateEmployee(@Param("employee")Employee employee,@Param("id")Integer id);
}

修改mapper.xml,这种方法参数名简单直观,即注解中的参数名

<mapper namespace="com.dao.EmployeeDao">
    <update id="updateEmployee" >
        update employee set lastName=#{employee.lastName}where id=#{id} 
    </update>
</mapper>

测试代码

public void testUpdate()
    {
        SqlSessionFactory sqlSessionFactory=GetSqlSessionFactory.getInstance();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        try{
	        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
	        mapper.updateEmployee(new Employee(null,"test",null,null),5);
        }finally
        {
            sqlSession.close();
        }
    }

3.总结

推荐使用注解方法解决多参数传入,另外在映射文件中使用句点运算符访问传入对象的属性,如#{employee.lastName},使用的是Field的反射

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值