mybatis结合mysql批量操做及查询sql

MySQL数据库 
批量操做主要使用的是Mybatis的foreach,遍历参数列表执行相应的操做,因此批量插入/更新/删除的写法是相似的,只是SQL略有区别而已。MySql批量操做须要数据库链接配置allowMultiQueries=true才能够。 java

(0)批量查询:mysql

 <select id="selectUserDataList" parameterType="list" resultType="String">
        select userData from tbl_hbb_user_info where mobile in (
        <foreach collection="list" item="item" index="index"
                 separator=",">
            #{item.mobile}
        </foreach>
        )
    </select>


(1)批量插入  sql

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            insert into user (name, age,dept_code) values  
            (#{item.name,jdbcType=VARCHAR},  
            #{item.age,jdbcType=INTEGER},  
             #{item.deptCode,jdbcType=VARCHAR}  
            )  
        </foreach>  
    </insert>  



上面演示的是MySql的写法(表主键自增的写法),由于MySql支持主键自增,因此直接设置useGeneratedKeys=true,便可在插入数据时自动实现主键自增;不须要自增时就不须要设置useGeneratedKeys,并且插入SQL包含全部字段便可。实际Mysql还有另一种写法,就是拼接values的写法,这种方法我测试过比多条insert语句执行的效率会高些。不过须要注意一次批量操做的数量作必定的限制。具体写法以下:  数据库

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
        insert into user (name, age,dept_code) values  
        <foreach collection="list" index="index" item="item" open="" close="" separator=",">  
            (#{item.name,jdbcType=VARCHAR},  
            #{item.age,jdbcType=INTEGER},  
             #{item.deptCode,jdbcType=VARCHAR}  
            )  
        </foreach>  
    </insert>  


对于Oracle不支持主键自增,须要序列替换,因此在SQL写法上略有不一样,须要在insert语句前加个 <selectKey>...</selectKey>告知Mybatis主键如何生成(selectKey中间的内容有省略,实际是生成主键的SQL)。 

(2)批量更新  mybatis

<update id="batchUpdate" parameterType="java.util.List">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            update user set name=#{item.name,jdbcType=VARCHAR},age=#{item.age,jdbcType=INTEGER}  
            where id=#{item.id,jdbcType=INTEGER}  
        </foreach>  
    </update>  



(3)批量删除  函数

<delete id="batchDelete" parameterType="java.util.List">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            delete from user  
            where id=#{item.id,jdbcType=INTEGER}  
        </foreach>  
    </delete>  



2、模糊查询  测试

<select id="selectLikeName" parameterType="java.lang.String" resultMap="BaseResultMap">  
        select  
        <include refid="Base_Column_List" />  
        from user  
        where name like CONCAT('%',#{name},'%' )   
    </select>  


上面的模糊查询语句是Mysql数据库的写法示例,用到了Mysql的字符串拼接函数CONCAT,其它数据库使用相应的函数便可。 

3、多条件查询 

多条件查询经常使用到Mybatis的if判断,这样只有条件知足时,才生成对应的SQL。  spa

<select id="selectUser" parameterType="map" resultMap="BaseResultMap">  
        select  
        <include refid="Base_Column_List" />  
        from user  
        <where>  
            <if test="name != null">  
                name = #{name,jdbcType=VARCHAR}  
            </if>  
            <if test="age != null">  
                and age = #{age,jdbcType=INTEGER}  
            </if>  
        </where>  
    </select>  



4、联表查询 
联表查询在返回结果集为多张表的数据时,能够经过继承resultMap,简化写法。例以下面的示例,结果集在User表字段的基础上添加了Dept的部门名称.net

<resultMap id="ExtResultMap" type="com.research.mybatis.generator.model.UserExt" extends="BaseResultMap">  
     <result column="name" jdbcType="VARCHAR" property="deptName" />  
  </resultMap>  
      
    <select id="selectUserExt" parameterType="map" resultMap="ExtResultMap">  
        select  
            u.*, d.name  
        from user u inner join dept d on u.dept_code = d.code  
        <where>  
            <if test="name != null">  
                u.name = #{name,jdbcType=VARCHAR}  
            </if>  
            <if test="age != null">  
                and u.age = #{age,jdbcType=INTEGER}  
            </if>  
        </where>  
    </select>  

 

复制代码
<update id="stockDetailOkBatchUpdate" parameterType="map">
      <foreach collection="items" index="index" item="item" open="begin" close=";end;" separator=";">
          update T_MM_ADD_STOCK_DETAIL t
          set
          t.REMARK=#{item.remark}, t.modify_time=sysdate, t.modify_user_code=#{currentUser} where t.id=#{item.id} <if test="index==items.size-1"> ; update T_MM_ADD_STOCK t set t.modify_time=sysdate, t.modify_user_code=#{currentUser}, t.remark=#{remark}, t.STORAGE_STATE='待录价' where t.id=#{mainId} </if> </foreach> </update>
复制代码
相关文章
相关标签/搜索