1. 程式人生 > >Mybatis傳遞引數的幾種方法

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>