Mybatis传递参数的几种方法




1.传递单个参数。单个参数可以直接写,可以是String类型,也可以是基本数据类型。

public String getUserById(Long Id);

--------XML-------------

<span style="font-family:SimSun;font-size:18px;"><select id="findById" parameter="String" resultType="String">

  select  id from TblUser where uid = #{id}

</select>
</span>

2.传递单个参数,但参数类型是封装对象,java中不需要增加什么,xml中增加说明就行。

public int getuser(User user);

--------XML------------

<span style="font-family:SimSun;"> <update id="heart"  <span style="color:#ff6666;">parameterType="com.svse.bean.User"></span>

        update User set xtsj = #{th.xtsj}, zdbb = #{th.zdbb}

        where zdbh = #{th.zdbh}

</update> 
</span>

3.传递多个参数,可以直接传递,也可以封装到Map里传值

 3.1 直接传值:用#{索引}取值

public List<User> getUserList<String uid,String ucode>;

-------XML------------

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select>

3.2 封装Map

action

    List<String> orgIds = new ArrayList<String>();
    Object orgId = userMap.get("bizframe_curr_user_org_id"); //当前用户所属部门
    orgIds.add(orgId.toString());

    Object[] orgIdArray = orgIds.toArray();

    Map<Sring,Object> condition = new HashMap<String,Object>();

    condition.put("projectId", projectId);

    condition.put("orgIds", orgIds);

    List<String> userList = commonQueryService.queryUserInfoByOrgId(condition OrgIds);

-----------------------------------------------XML---------------------------------------

 <select id="queryUserInfoByOrgId" resultType="java.lang.String" parameterType="java.util.Map">
   select
   a.user_id
   from tsys_user a
   where a.org_id IN
   <foreach item="item" index="index" collection="orgIds" open="(" separator="," close=")"> 
     #{item} 
   </foreach>

   and a.pid = #{projectId}
 </select>

4、直接传参数,使用@Param标注

public int login(@Param("uid") String uid,@Param("ucode") String ucode);


<span style="font-family:SimSun;">  <select id="login" parameterType="String" resultType="Integer">
        select count(*) from User 
        where uid = #{uid} and ucode = #{ucode} and scbz = 0
    </select>
</span>


猜你喜欢

转载自blog.csdn.net/yu799225625/article/details/51387082