zoukankan      html  css  js  c++  java
  • mybatis映射和条件查询

    mybatis

    mybatis绑定接口

    <mapper namespace="com.ruoyi.web.practice.mapper.CmsLinkMapper">
    

    mybatis映射(数据库的字段与实体类的映射)

    <resultMap type="CmsLink" id="CmsLinkResult">
            <result property="linkId"    column="link_id"    />
            <result property="siteId"    column="site_id"    />
            <result property="linkName"    column="link_name"    />
            <result property="linkUrl"    column="link_url"    />
            <result property="linkImage"    column="link_image"    />
            <result property="clicks"    column="clicks"    />
            <result property="createTime"    column="create_time"    />
            <result property="updateTime"    column="update_time"    />
            <result property="sort"    column="sort"    />
            <result property="isShow"    column="is_show"    />
            <result property="status"    column="status"    />
            <result property="remark"    column="remark"    />
     </resultMap>
    

    把公共的的sql语句提取出来

    <sql id="selectCmsLinkVo">
    select link_id, site_id, link_name, link_url, link_image, clicks, create_time, update_time, sort, is_show, status, remark from cms_link
    </sql>
    

    把公共的的sql语句通过Id引过来

    <select id="selectCmsLinkList" parameterType="CmsLink" resultMap="CmsLinkResult">
      <include refid="selectCmsLinkVo"/>
          <where>
             <if test="siteId != null "> and site_id = #{siteId}</if>
             <if test="linkName != null  and linkName != ''"> and link_name like concat('%', #{linkName}, '%')</if>
             <if test="linkUrl != null  and linkUrl != ''"> and link_url = #{linkUrl}</if>
             <if test="linkImage != null  and linkImage != ''"> and link_image = #{linkImage}</if>
             <if test="clicks != null "> and clicks = #{clicks}</if>
             <if test="sort != null  and sort != ''"> and sort = #{sort}</if>
             <if test="isShow != null  and isShow != ''"> and is_show = #{isShow}</if>
             <if test="status != null  and status != ''"> and status = #{status}</if>
            </where>
    </select>
    

    插入语句设置条件

     <insert id="insertCmsLink" parameterType="CmsLink" useGeneratedKeys="true" keyProperty="linkId">
            insert into cms_link
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="siteId != null">site_id,</if>
                <if test="linkName != null and linkName != ''">link_name,</if>
                <if test="linkUrl != null">link_url,</if>
                <if test="linkImage != null">link_image,</if>
                <if test="clicks != null">clicks,</if>
                <if test="createTime != null">create_time,</if>
                <if test="updateTime != null">update_time,</if>
                <if test="sort != null">sort,</if>
                <if test="isShow != null">is_show,</if>
                <if test="status != null">status,</if>
                <if test="remark != null">remark,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="siteId != null">#{siteId},</if>
                <if test="linkName != null and linkName != ''">#{linkName},</if>
                <if test="linkUrl != null">#{linkUrl},</if>
                <if test="linkImage != null">#{linkImage},</if>
                <if test="clicks != null">#{clicks},</if>
                <if test="createTime != null">#{createTime},</if>
                <if test="updateTime != null">#{updateTime},</if>
                <if test="sort != null">#{sort},</if>
                <if test="isShow != null">#{isShow},</if>
                <if test="status != null">#{status},</if>
                <if test="remark != null">#{remark},</if>
            </trim>
        </insert>
    

    修改语句设置条件

    <update id="updateCmsLink" parameterType="CmsLink">
            update cms_link
            <trim prefix="SET" suffixOverrides=",">
                <if test="siteId != null">site_id = #{siteId},</if>
                <if test="linkName != null and linkName != ''">link_name = #{linkName},</if>
                <if test="linkUrl != null">link_url = #{linkUrl},</if>
                <if test="linkImage != null">link_image = #{linkImage},</if>
                <if test="clicks != null">clicks = #{clicks},</if>
                <if test="createTime != null">create_time = #{createTime},</if>
                <if test="updateTime != null">update_time = #{updateTime},</if>
                <if test="sort != null">sort = #{sort},</if>
                <if test="isShow != null">is_show = #{isShow},</if>
                <if test="status != null">status = #{status},</if>
                <if test="remark != null">remark = #{remark},</if>
            </trim>
            where link_id = #{linkId}
        </update>
    

    删除语句

    <delete id="deleteCmsLinkByIds" parameterType="String">
            delete from cms_link where link_id in
            <foreach item="linkId" collection="array" open="(" separator="," close=")">
                #{linkId}
            </foreach>
    </delete>
    <!--foreach元素的属性主要有item,index,collection,open,separator,close-->
    
    <!--collection该属性指定你要遍历的集合名称,然后在调用该方法的时候,通过参数的方式传递过来-->
    
    <!--item用来临时存放迭代集合中当前元素的值,便于在foreach中使用-->
    
    <!--index这个属性用来指定用来访问迭代集合下标的名称。如:index="myIndex",则#{myIndex}用来访问当前迭代的下标。下标从0开始。-->
    
    <!--open将该属性指定的值添加到foreach迭代后拼出字符串的开始。如:拼凑in子语句的开始部分“(”-->
    
    <!--close: 将该属性指定的值添加到foreach迭代拼出字符串的结尾。如:拼凑in子语句的介绍部分")"。-->
    
    <!--separator用来分割foreach元素迭代的每个元素-->
    
  • 相关阅读:
    1104 Sum of Number Segments (20 分)(数学问题)
    1092 To Buy or Not to Buy (20 分)(hash散列)
    1082 Read Number in Chinese (25 分)(字符串处理)【背】
    1105 Spiral Matrix (25 分)(模拟)
    初识网络安全及搭建网站(内网)
    HTML5开发者需要了解的技巧和工具汇总(转)
    native+web开发模式之web前端经验分享
    移动平台3G手机网站前端开发布局技巧汇总(转)
    Asp.net 中图片存储数据库以及页面读取显示通用方法详解附源码下载
    使用H3Viewer来查看VS2010的帮助文档
  • 原文地址:https://www.cnblogs.com/striver20/p/14244892.html
Copyright © 2011-2022 走看看