常用sql-mapper

本文详细介绍了MyBatis中XML映射文件的使用,包括ResultMap用于对象属性与数据库列的映射,以及动态SQL元素如<if>、<where>、<foreach>等在条件查询中的应用。示例展示了如何根据查询参数动态生成SQL语句,实现灵活的数据查询、插入、更新和删除操作。
摘要由CSDN通过智能技术生成

sql-mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.smcv.xyx.user.operation.domain.ShufflePicMapper">

   <resultMap type="com.smcv.xyx.user.operation.domain.ShufflePicDO" id="ShufflePicDOResultMap">
      <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="title" property="title"  jdbcType="VARCHAR" />
        <result column="pic_path" property="picPath"  jdbcType="VARCHAR" />
        <result column="pic_url" property="picUrl"  jdbcType="VARCHAR" />
        <result column="url_type" property="urlType"  jdbcType="INTEGER" />
        <result column="wechat_id" property="wechatId"  jdbcType="VARCHAR" />
        <result column="pic_order" property="picOrder"  jdbcType="INTEGER" />
        <result column="effictive_start_time" property="effictiveStartTime"  jdbcType="TIMESTAMP" />
        <result column="effictive_end_time" property="effictiveEndTime"  jdbcType="TIMESTAMP" />
        <result column="create_id" property="createId"  jdbcType="BIGINT" />
        <result column="update_id" property="updateId"  jdbcType="BIGINT" />
        <result column="is_deleted" property="deleted"  jdbcType="BIT" />
        <result column="create_time" property="createTime"  jdbcType="TIMESTAMP" />
        <result column="update_time" property="updateTime"  jdbcType="TIMESTAMP" />
   </resultMap>

   <sql id="allColumnFields">
         id,title,pic_path,pic_url,url_type,wechat_id,pic_order,effictive_start_time,effictive_end_time,create_id,update_id,is_deleted,create_time,update_time
   </sql>
   
   <!-- 只有varchar char text类型 做 !='' 判断,其他类型时只做!=null 判断 0或fasle与空''是相同的,在mybatis中的动态语句中-->
   <!-- xml转义字符需要 <![CDATA[   ]]> 标签-->
   <!-- 包含(创建时间|修改时间字段区间查询)-->
   <sql id="dynamicWhereFieldsUseQuery">
      <where>
         <if test="id != null "> AND id=#{id,jdbcType=INTEGER} </if>
         <if test="title != null  and title != '' "> AND title=#{title,jdbcType=VARCHAR} </if>
         <if test="picPath != null  and picPath != '' "> AND pic_path=#{picPath,jdbcType=VARCHAR} </if>
         <if test="picUrl != null  and picUrl != '' "> AND pic_url=#{picUrl,jdbcType=VARCHAR} </if>
         <if test="urlType != null "> AND url_type=#{urlType,jdbcType=INTEGER} </if>
         <if test="wechatId != null  and wechatId != '' "> AND wechat_id=#{wechatId,jdbcType=VARCHAR} </if>
         <if test="picOrder != null "> AND pic_order=#{picOrder,jdbcType=INTEGER} </if>
         <if test="effictiveStartTime != null "> AND effictive_start_time=#{effictiveStartTime,jdbcType=TIMESTAMP} </if>
         <if test="effictiveEndTime != null "> AND effictive_end_time=#{effictiveEndTime,jdbcType=TIMESTAMP} </if>
         <if test="createId != null "> AND create_id=#{createId,jdbcType=BIGINT} </if>
         <if test="updateId != null "> AND update_id=#{updateId,jdbcType=BIGINT} </if>
         <if test="deleted != null "> AND is_deleted=#{deleted,jdbcType=BIT} </if>
         <if test="startCreateTime != null"><![CDATA[   AND create_time>=#{startCreateTime,jdbcType=Date} ]]></if>
         <if test="endCreateTime != null"><![CDATA[  AND create_time<=#{endCreateTime,jdbcType=Date} ]]></if>
         <if test="startUpdateTime != null"><![CDATA[   AND update_time>=#{startUpdateTime,jdbcType=TIMESTAMP} ]]></if>
         <if test="endUpdateTime != null"><![CDATA[  AND update_time<=#{endUpdateTime,jdbcType=TIMESTAMP} ]]></if>
      </where>
   </sql>
   
   <select id="selectById" parameterType="int" resultMap="ShufflePicDOResultMap">
   SELECT
      <include refid="allColumnFields" />
   FROM
      t_shuffle_pic
   WHERE
       id = #{id,jdbcType=INTEGER}  AND is_deleted =0
   </select>

    <insert id="insert" parameterType="com.smcv.xyx.user.operation.domain.ShufflePicDO" useGeneratedKeys="true" statementType="PREPARED" keyProperty="id">
        INSERT INTO t_shuffle_pic(
        id,
        title,
        pic_path,
        pic_url,
        url_type,
        wechat_id,
        pic_order,
        effictive_start_time,
        effictive_end_time,
        create_id,
        update_id,
        is_deleted,
        create_time,
        update_time
      )values(
        #{id,jdbcType=INTEGER},
        #{title,jdbcType=VARCHAR},
        #{picPath,jdbcType=VARCHAR},
        #{picUrl,jdbcType=VARCHAR},
        #{urlType,jdbcType=INTEGER},
        #{wechatId,jdbcType=VARCHAR},
        #{picOrder,jdbcType=INTEGER},
        #{effictiveStartTime,jdbcType=TIMESTAMP},
        #{effictiveEndTime,jdbcType=TIMESTAMP},
        #{createId,jdbcType=BIGINT},
        #{updateId,jdbcType=BIGINT},
        #{deleted,jdbcType=BIT},
        #{createTime,jdbcType=TIMESTAMP},
        #{updateTime,jdbcType=TIMESTAMP}
      )
    </insert>

  <insert id="insertSelective" parameterType="com.smcv.xyx.user.operation.domain.ShufflePicDO" keyProperty="id" useGeneratedKeys="true" statementType="PREPARED">
    insert into t_shuffle_pic
    <trim prefix="(" suffix=")" suffixOverrides=",">
       <if test="id != null">
         id,
       </if>
       <if test="title != null">
         title,
       </if>
       <if test="picPath != null">
         pic_path,
       </if>
       <if test="picUrl != null">
         pic_url,
       </if>
       <if test="urlType != null">
         url_type,
       </if>
       <if test="wechatId != null">
         wechat_id,
       </if>
       <if test="picOrder != null">
         pic_order,
       </if>
       <if test="effictiveStartTime != null">
         effictive_start_time,
       </if>
       <if test="effictiveEndTime != null">
         effictive_end_time,
       </if>
       <if test="createId != null">
         create_id,
       </if>
       <if test="updateId != null">
         update_id,
       </if>
       <if test="deleted != null">
         is_deleted,
       </if>
       <if test="createTime != null">
         create_time,
       </if>
       <if test="updateTime != null">
         update_time,
       </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">
          #{id,jdbcType=INTEGER},
        </if> 
        <if test="title != null">
          #{title,jdbcType=VARCHAR},
        </if> 
        <if test="picPath != null">
          #{picPath,jdbcType=VARCHAR},
        </if> 
        <if test="picUrl != null">
          #{picUrl,jdbcType=VARCHAR},
        </if> 
        <if test="urlType != null">
          #{urlType,jdbcType=INTEGER},
        </if> 
        <if test="wechatId != null">
          #{wechatId,jdbcType=VARCHAR},
        </if> 
        <if test="picOrder != null">
          #{picOrder,jdbcType=INTEGER},
        </if> 
        <if test="effictiveStartTime != null">
          #{effictiveStartTime,jdbcType=TIMESTAMP},
        </if> 
        <if test="effictiveEndTime != null">
          #{effictiveEndTime,jdbcType=TIMESTAMP},
        </if> 
        <if test="createId != null">
          #{createId,jdbcType=BIGINT},
        </if> 
        <if test="updateId != null">
          #{updateId,jdbcType=BIGINT},
        </if> 
        <if test="deleted != null">
          #{deleted,jdbcType=BIT},
        </if> 
        <if test="createTime != null">
          #{createTime,jdbcType=TIMESTAMP},
        </if> 
        <if test="updateTime != null">
          #{updateTime,jdbcType=TIMESTAMP},
        </if> 
    </trim>
  </insert>

   <update id="updateById" parameterType="com.smcv.xyx.user.operation.domain.ShufflePicDO" statementType="PREPARED">
      UPDATE t_shuffle_pic
      SET
         title = #{title,jdbcType=VARCHAR},
         pic_path = #{picPath,jdbcType=VARCHAR},
         pic_url = #{picUrl,jdbcType=VARCHAR},
         url_type = #{urlType,jdbcType=INTEGER},
         wechat_id = #{wechatId,jdbcType=VARCHAR},
         pic_order = #{picOrder,jdbcType=INTEGER},
         effictive_start_time = #{effictiveStartTime,jdbcType=TIMESTAMP},
         effictive_end_time = #{effictiveEndTime,jdbcType=TIMESTAMP},
         create_id = #{createId,jdbcType=BIGINT},
         update_id = #{updateId,jdbcType=BIGINT},
         is_deleted = #{deleted,jdbcType=BIT},
         update_time = #{updateTime,jdbcType=TIMESTAMP}
      WHERE
         id = #{id,jdbcType=INTEGER}  AND is_deleted =0
   </update>

   <update id="logicDeleteById" parameterType="java.util.Map">
      update t_shuffle_pic set is_deleted=1,update_id= #{updateId,jdbcType=BIGINT},update_time=now() WHERE id = #{id,jdbcType=INTEGER}   AND is_deleted =0
   </update>
   
   <update id="logicDeleteByIds" parameterType="java.util.Map">
      update t_shuffle_pic set is_deleted=1,update_id= #{updateId,jdbcType=BIGINT},update_time=now() WHERE id in 
     <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
           #{item}
     </foreach>
       AND is_deleted =0
      
   </update>

   <update id="updateDynamic" parameterType="com.smcv.xyx.user.operation.domain.ShufflePicDO">
      UPDATE t_shuffle_pic
      <set>
         <if test="title != null  and title != '' ">title=#{title,jdbcType=VARCHAR},</if>
         <if test="picPath != null  and picPath != '' ">pic_path=#{picPath,jdbcType=VARCHAR},</if>
         <if test="picUrl != null  and picUrl != '' ">pic_url=#{picUrl,jdbcType=VARCHAR},</if>
         <if test="urlType != null ">url_type=#{urlType,jdbcType=INTEGER},</if>
         <if test="wechatId != null  and wechatId != '' ">wechat_id=#{wechatId,jdbcType=VARCHAR},</if>
         <if test="picOrder != null ">pic_order=#{picOrder,jdbcType=INTEGER},</if>
         <if test="effictiveStartTime != null ">effictive_start_time=#{effictiveStartTime,jdbcType=TIMESTAMP},</if>
         <if test="effictiveEndTime != null ">effictive_end_time=#{effictiveEndTime,jdbcType=TIMESTAMP},</if>
         <if test="createId != null ">create_id=#{createId,jdbcType=BIGINT},</if>
         <if test="updateId != null ">update_id=#{updateId,jdbcType=BIGINT},</if>
         <if test="deleted != null ">is_deleted=#{deleted,jdbcType=BIT},</if>
         <if test="updateTime != null ">update_time=#{updateTime,jdbcType=TIMESTAMP},</if>
      </set>
      WHERE id = #{id,jdbcType=INTEGER}    AND is_deleted =0
   </update>

   <select id="dynamicSelectList" resultMap="ShufflePicDOResultMap" parameterType="com.smcv.xyx.user.operation.query.ShufflePicQuery">
        SELECT
          <include refid="allColumnFields" />
       FROM
         t_shuffle_pic 
         <include refid="dynamicWhereFieldsUseQuery" /> 
       ORDER BY update_time DESC 
        limit #{limitSize}    
   </select>

   <select id="dynamicSelectEffectiveList" resultMap="ShufflePicDOResultMap" parameterType="com.smcv.xyx.user.operation.query.ShufflePicQuery">
      SELECT
      <include refid="allColumnFields" />
      FROM
      t_shuffle_pic
      <include refid="dynamicWhereFieldsUseQuery" />
      AND effictive_start_time &gt; getdate()
      AND effictive_end_time &lt; getdate()
      ORDER BY pic_order ACS
   </select>
    
   <select id="dynamicSelectOne" resultMap="ShufflePicDOResultMap" parameterType="com.smcv.xyx.user.operation.query.ShufflePicQuery">
        SELECT
          <include refid="allColumnFields" />
       FROM
         t_shuffle_pic 
         <include refid="dynamicWhereFieldsUseQuery" /> 
        limit #{limitSize}    
   </select>

   <select id="dynamicSelectCount" resultType="long" parameterType="com.smcv.xyx.user.operation.query.ShufflePicQuery">
        SELECT
         count(id)
       FROM
         t_shuffle_pic
      <include refid="dynamicWhereFieldsUseQuery" />
   </select>

   <select id="dynamicSelectPageQuery" resultMap="ShufflePicDOResultMap" parameterType="com.smcv.xyx.user.operation.query.ShufflePicQuery">
        SELECT
          <include refid="allColumnFields" />
       FROM
         t_shuffle_pic
      <include refid="dynamicWhereFieldsUseQuery" />
        ORDER BY pic_order
        <if test="start != null and pageSize!=null">
      LIMIT #{start},#{pageSize}
      </if>
   </select>
   
   <select id="selectByIds" resultMap="ShufflePicDOResultMap" parameterType="java.util.List">
     SELECT 
         <include refid="allColumnFields" />
     FROM 
         t_shuffle_pic
     WHERE id in
     <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
           #{item}
     </foreach>
      AND is_deleted =0
   </select>

</mapper>

foreach

<update id="logicDeleteByIds" parameterType="java.util.List">
   update t_praise set is_delete=1,update_time=now() WHERE id in 
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
  </foreach>
   AND is_delete=0
</update>
<select id="selectCount" resultType="long" parameterType="com.smcv.xyx.mifa.query.TopicQuery">
   SELECT
   count(id)
   FROM
   t_topic
   <where>
      <if test="topicTypeId != null "> AND topic_type_id=#{topicTypeId,jdbcType=INTEGER} </if>
      <if test="content != null  and content != '' "> AND tt.content=#{content,jdbcType=LONGVARCHAR} </if>
      <if test="userId != null "> AND user_id=#{userId,jdbcType=BIGINT} </if>
      <if test="topicStatus != null "> AND topic_status=#{topicStatus,jdbcType=INTEGER} </if>
      <if test="aduitStatus != null "> AND aduit_status=#{aduitStatus,jdbcType=INTEGER} </if>
      <if test="isInform != null "> AND is_inform=#{isInform,jdbcType=INTEGER} </if>
      <if test="highLight != null "> AND high_light=#{highLight,jdbcType=INTEGER} </if>
      <if test="aduitRemark != null  and aduitRemark != '' "> AND aduit_remark=#{aduitRemark,jdbcType=VARCHAR} </if>
      <if test="reportReasonId != null  and reportReasonId != '' "> AND report_reason_id=#{reportReasonId,jdbcType=INTEGER} </if>
      <if test="sysUserId != null "> AND sys_user_id=#{sysUserId,jdbcType=BIGINT} </if>
      <if test="isDelete != null "> AND is_delete=#{isDelete,jdbcType=INTEGER} </if>
      <if test="startCreateTime != null"><![CDATA[   AND create_time>=#{startCreateTime,jdbcType=TIMESTAMP} ]]></if>
      <if test="endCreateTime != null"><![CDATA[  AND create_time<=#{endCreateTime,jdbcType=TIMESTAMP} ]]></if>
      <if test="startUpdateTime != null"><![CDATA[   AND update_time>=#{startUpdateTime,jdbcType=TIMESTAMP} ]]></if>
      <if test="endUpdateTime != null"><![CDATA[  AND update_time<=#{endUpdateTime,jdbcType=TIMESTAMP} ]]></if>
      <if test="userIds != null">
         AND user_id in
         <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
            #{item}
         </foreach>
      </if>
   </where>
</select>

leftjoin

<select id="selectListByQuery"  parameterType="com.smcv.xyx.mifa.query.TopicCommentQuery" resultType="com.smcv.xyx.mifa.model.vo.TopicCommentVO">
   select *,u1.nick_name as userNickName,u2.nick_name as reUserNickName
   from t_topic_comment t
   LEFT JOIN t_user u1 on t.user_id = u1.id
   LEFT JOIN t_user u2 on t.reply_user_id = u2.id
   where t.is_delete = 0 and t.topic_id = #{topicId,jdbcType=INTEGER}
   <if test="aduitStatus != null">
      and t.aduit_status = #{aduitStatus,jdbcType=INTEGER}
   </if>
   order by t.create_time
</select>


<select id="selectListByQuery"  parameterType="com.smcv.xyx.mifa.query.TopicCommentQuery" resultType="com.smcv.xyx.mifa.model.vo.TopicCommentVO">
	select *,u1.nick_name as userNickName,u2.nick_name as reUserNickName
	from t_topic_comment t
	LEFT JOIN t_user u1 on t.user_id = u1.id
	LEFT JOIN t_user u2 on t.reply_user_id = u2.id
	where t.is_delete = 0 and t.topic_id = #{topicId,jdbcType=INTEGER}
	<if test="aduitStatus != null">
		and t.aduit_status = #{aduitStatus,jdbcType=INTEGER}
	</if>
	order by t.create_time
</select>

批量插入

    <insert id="insertChatDataBaseForeach" parameterType="java.util.List" useGeneratedKeys="false">
    insert into t_chat_data
    ( msgid,_action,_from,tolist,roomid,msgtime,msgtype,seq,msg,create_date,create_by,update_date,update_by)
    values
    <foreach collection="list" item="item" index="index" separator=",">
        (
        #{item.msgid},
        #{item.action},
        #{item.from},
        #{item.tolistStr},
        #{item.roomid},
        #{item.msgtime},
        #{item.msgtype},
        #{item.seq},
        #{item.msg},
        CURRENT_TIMESTAMP,
        -1,
        CURRENT_TIMESTAMP,
        -1
        )
    </foreach>
</insert>

时间类型

<select id="dynamicSelectEffectiveList" resultMap="ShufflePicDOResultMap" parameterType="com.smcv.xyx.user.operation.query.ShufflePicQuery">
   SELECT
   <include refid="allColumnFields" />
   FROM
   t_shuffle_pic
   <include refid="dynamicWhereFieldsUseQuery" />
   AND effictive_start_time &lt; NOW()
   AND effictive_end_time &gt; NOW()
   ORDER BY pic_order ASC
</select>

模糊查询

<select id="matchSecretByAgentId" resultType="com.smcv.xyx.wework.dto.master.FixCodeDTO">
  select * from t_fix_code
  where code_type = #{type,jdbcType=DECIMAL}
     and special_value like concat('%',#{agentId},'%')
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值