Mybatis DAO层参数传递


众所周知,Mybatis DAO层可以只写接口方法,在mapper的XML配置文件中定义与接口方法同名的查询节点就可以实现操作数据库的功能。那么问题来了,如何传递合适的参数给SQL语句(配置文件中定义的,当然配置文件也可以换成注解的方式,不是本文重点,在此不表)呢?根据参数的数量和类型做如下说明: 
1. 单个普通参数(简单数据类型)

    int deleteById(Integer id);// 数据类型最好用封装类型,不要用简单类型int,这样做有助于拼接SQL的语句的时候判断参数是不是    
    Null,因为基本类型没有主动赋值的话,会有默认值,而不是Null。

          <delete id="deleteById" parameterType="java.lang.Integer" >
            DELETE FROM gov_pub_search
               WHERE id = #{id,jdbcType=INTEGER}
          </delete>
    这里parameterType可以不写
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2. 多个普通参数 
这里可以写成下面①的样子,这样多个参数时,Mybatis会自动把这些参数放到一个map里面,用param1,param2作为key,有两种引用 
方式1)以序号引用,#{1}代表第一个参数,2)以参数名param1(代表第一个参数)。但是作为一个一个良好的习惯,应当给每个变量命

    int deleteById(Integer id,String name);  ---①
    int deleteById(@Param(value=”id”Integer id, @Param   
           (value=”name”String name)) ;---②
    <delete id="deleteById">
          DELETE FROM gov_pub_search
             WHERE id = #{id,jdbcType=INTEGER}
             <if test="name != null" >
                 ,p_name = #{name}
            </if>  
    </delete>                       
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.单个(多个)集合类型参数 
当需要批量操作数据库的时候,就会传入一批参数,这时就用到了集合(这里包括数组)参数,集合参数传入方式如下: 
3.1. 数组 
int batchUpdateStatusOfChecking(@Param(value=”ids”)String[] ids);

            <update id="batchUpdateStatusOfChecking">
                   update gov_pub_search
                   <set>
                        status = 5
                   </set>
                   <if test="ids != null &amp;&amp; ids != ''">
                         where status in(2,4)
                            and pub_id in
                            <foreach collection="ids" item="item" index="index" open="("
                               separator="," close=")">
                              #{item}
                            </foreach>
                  </if>
            </update>

     这里参数只有一个数组,多个数组类似。在引用的时候,collection表示传入的数组参数的名字,因为这里我们用@Param显示给数组命名 
     了 ,所以可以直接引用这个名字,如果不给数组显示命名,Mybatis会把参数放到一个map里,并且以array作为key。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.2. 列表(List)

     int batchInsertStudent(List<String> students);

     <insert id="batchInsertStudent"     
         parameterType="java.util.List">  
      insert into student (id,name,sex,tel,address) values
      <foreach collection="list" item="item" index="index"   
       separator="," >  
           (#{item.id},#{item.name},#{item.sex},#{item.tel},# 
           {item.address})  
      </foreach>  
     </insert>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
这里没有给参数显示命名,Mybatis会以list作为可以把参数放到map里。当然作为一
个良好的编程习惯,最好显示的给出一个有意义的名字。
  • 1
  • 2
  • 3

3.3. Map

     int batchUpd(@Param(value="map") Map<String,String> map,
                  @Param("updateBy")String updateBy);
         <update id="batchUpdateStatusForChecking"> 
           <foreach collection="map" index="key"  item="ent" separator=";"> 
              update search
              set status = 2
              <if test="updateBy!= null">
                  ,update_by = #{updateBy}
              </if>
                  ,update_time = #{ent}
                  where pub_id = #{key}
                  and status = 1
            </foreach>
        </update>

        Map<String,String> map = new HashMap<String,String>();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里collection的名字只要和接口方法里的参数名字一直就可以了。 
3.4. Map(复杂元素)

public List<BpmDefUser> getByMap(Map<String,List<Long>> map){
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("relationMap", map);
        return this.getBySqlKey("getByMap", params);

    }
<select id="getByMap" resultMap="BpmDefUser">

            <foreach collection="relationMap" index="key"  item="ent" separator="union">
                SELECT *
                FROM BPM_DEF_USER
                where  RIGHT_TYPE=#{key}
                and OWNER_ID in 
                <foreach collection="ent"  item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </foreach>

    </select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4.混合参数(既有简单类型,同时又有集合类型)

    int batchUpdateStatusOfChecking(@Param(value="ids")String[]  ids,@Param(value="lastUpdateBy")String lastUpdateBy);

    <update id="batchUpdateStatusOfChecking">
          update gov_pub_search
          <set>
                status = 5
                <if test="lastUpdateBy != null" >
                     ,last_update_by = #{lastUpdateBy}
                </if>
          </set>
          <if test="ids != null &amp;&amp; ids != ''">
             where status in(2,4)
                and pub_id in
                <foreach collection="ids" item="item" index="index" open="("
                             separator="," close=")">
                             #{item}
                 </foreach>
           </if>
    </update>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wutian713/article/details/50467043

猜你喜欢

转载自blog.csdn.net/xuchuangqi/article/details/80526132