1. 程式人生 > >mybatis封裝多個引數的方法總結

mybatis封裝多個引數的方法總結

1 單個引數

<select id="get" parameterType="long" resultType="string"> 

select name from test

where id=#{id}

 </select>

2 多個引數之map

<select id="get" parameterType="java.util.Map" resultType="string"> 

select name from test

where id=#{id} and flag=#{flag}

 </select>

3 多個引數之bean

<select id="get" parameterType="Test" resultType="string"> 

select name from test

where id=#{id} and flag=#{flag}

 </select>

4 多個引數之list

<select id="get" parameterType="long" resultType="string">  

        select name from testwhere Id in  

        <foreach collection="list"   

            open="(" close=")" separator="," item="item" index="index">  

            #{item.id}  

        </foreach>  

  </select>  

批量插入

<insert id="add" useGeneratedKeys="true" parameterType="java.util.List">

<selectKey resultType="long" keyProperty="id" order="AFTER">

SELECT

LAST_INSERT_ID()

</selectKey>

insert into test (name,time,flag)

values

<foreach collection="list" item="item" index="index" separator="," >

(#{item.name},#{item.time},#{item.flag})

</foreach>

</insert>

5 多個引數之陣列

<select id="get" parameterType="long" resultType="string">  

        select name from testwhere Id in  

        <foreach collection="array"   

            open="(" close=")" separator="," item="item" index="index">  

            #{item.id}  

        </foreach>  

  </select>