【Mybatis】org.apache.ibatis.binding.BindingException: Parameter ‘XXXX‘ not found.

一个参数或一个对象

Dao层

public interface StudentDao {
 
	int selectById(int id);
	
	int insert(Student stu);
	
}

对应的mapper文件

<select id="selectById" resultMap="BaseResultMap">
		select <include refid="Base_Column_List" />
		from student
		 <where>
            <if test="id != null">
            	id = #{id}
            </if>
		</where>
</select>

<insert id="insert" parameterType="com.yoho.crm.dal.model.student">
		 insert into student
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null and username!= ''">user_name,</if>
            <if test="age!= null">age,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null and username!= ''">#{username},</if>
            <if test="age!= null">#{age},</if>  
         </trim>
</insert>

多个参数

每个参数前加上@Param 注解,括号中注解的名称就是mapper中映射的值

import org.apache.ibatis.annotations.Param;

@Mapper
public interface CustomerInfoMapper extends BaseMapper<CustomerInfo> {

    List<CustomerInfoVO> search(@Param("searchVal") String searchVal, 
                                @Param("delegateCompany") String delegateCompany);
}   
    <select id="search" resultType="com.test.vo.CustomerInfoVO">
        select a.*
        from customer_info a 
        <where>
            a.delete_flag=0
            <if test="searchVal != null and searchVal != ''">
                and a.company_name like CONCAT('%',#{searchVal},'%')
                <!-- and a.company_name like CONCAT('%',#{param1},'%')		-->
            </if>
            <if test="delegateCompany != null and delegateCompany != ''">
                and a.company_name like CONCAT('%',#{delegateCompany},'%')
                <!-- and a.company_name like CONCAT('%',#{param2},'%')		-->
            </if>
        </where>
    </select>

既有参数又有对象时

对象也需要注解,一般参数的直接取,对象的需要对象.属性

public interface SysMenuMapper extends BaseMapper<SysMenu> {

    public List<SysMenu> selectMenuTreeByUserId(@Param("userId")Long userId, 
                                                @Param("menu")Menu menuSource);
}                                                

注意,在动态sql里面也需要在判断的时候使用对象.属性进行判断

    <select id="selectMenuTreeByUserId" resultMap="SysMenuResult">
		select *
		from sys_menu m
		where u.user_id = #{userId} 
		<!-- where u.user_id = #{param1}		-->
		<if test="menu.menuSource != null and menu.menuSource != ''">
            AND m.menu_source = #{menu.menuSource}
		</if>
		<!-- <if test="param2.menuSource != null and param2.menuSource != ''">		-->
		<!-- AND m.menu_source = #{param2.menuSource}		-->
		<!-- </if>		-->
	</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值