MyBatis多参数入参的三种方法

MyBatis多参数入参的三种方法

  1. @Param1(别名)Object xxx,@Param1(别名)Object xxx
    Mapper类:

    public List<SysUser> findUserByidandName(@Param("a")String realName,@Param("b")String roleId);
    

    Mapper.xml:

     <select id="findUserByidandName" resultType="com.liu.pojo.SysUser">
            select * from t_sys_user
            <trim prefix="where" prefixOverrides="and">
                <if test="realName != null and realName != ''">
                     and realName = #{a}
                </if>
                <if test="roleId != null and roleId != ''">
                     and roleId = #{b}
                </if>
            </trim>
        </select>
    
     测试类:
    
     @Test
    	    public void findUserByIdandName(){
    	        SqlSession sqlSession=MybatisUtil.getSqlSession();
    	        sysUserMapper mapper=sqlSession.getMapper(sysUserMapper.class);
    	        List<SysUser> userList=mapper.findUserByidandName("彭", 1);
    	        for(SysUser sysUser : userList) {
    	            System.out.println(sysUser);
    	        }
    	    }
    
  2. 使用对象传参
    Mapper类:

     public int updateUser(SysUser sysUser);
    

    Mapper.xml:

    <update id="updateUser" parameterType="com.liu.pojo.SysUser">
     update t_sys_user
         <trim prefix="set" suffixOverrides="," suffix="where id=#{id}">
              <if test="realName != null and realName != ''">
                 realName=#{realName},
              </if>
              <if test="password != null and password != ''">
                 password=#{password},
              </if>
         </trim>
    </update>
    

    测试类:

    @Test
    public void updateUser(){
     SqlSession sqlSession=MybatisUtil.getSqlSession();
     sysUserMapper mapper=sqlSession.getMapper(sysUserMapper.class);
     SysUser sysUser = new SysUser();
     sysUser.setId(1);
     sysUser.setRealName("彭乐攀");
     System.out.println(mapper.updateUser(sysUser));
     sqlSession.commit();
    }
    
  3. 使用HashMap
    Mapper类:

    public int updatePassWord(Map map);
    
      Mapper.xml:
    
    <update id="updatePassWord" parameterType="map">
    	   update t_sys_user set password = #{password} where id = #{id}
    </update>
    
      测试类:
    
    @Test
    	    public void updatepassword(){
    	        SqlSession sqlSession=MybatisUtil.getSqlSession();
    	        sysUserMapper mapper=sqlSession.getMapper(sysUserMapper.class);
    	        Map map = new HashMap();
    	        map.put("password","131313");
    	        map.put("id",1);
    	        System.out.println(mapper.updatePassWord(map));
    	        sqlSession.commit();
    	    }		  
    
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值