wqq5610 2022-06-16 11:43 采纳率: 36.4%
浏览 35
已结题

编写mapper文件

在mapper.xml中如何添加 SQL语句 start with ...connect by条件判断,如果有参数添加否则不添加

  • 写回答

3条回答 默认 最新

  • 渴望技术的猿 2022-06-16 11:53
    关注

    可以使用标签
    if 标签

      <select id="queryBookByif" resultType="com.zking.mybatis01.model.Book">
        select <include refid="Base_Column_List"></include> from t_book where 1=1
           <if test="null!=bookType and ''!=bookType">
    --     book_type 代表数据库字段
    --     bookType代表实体属性不需要类名点
    --     #代表取值
             and book_type=#{bookType}
           </if>
      </select>
    

    2 trim mybatis中trim是动态拼接;java中表示去除前后空格
    prefix 前缀
    suffix后缀
    suffixOverrides去除后缀指定字符
    prefixOverrides 去除前缀指定字符

    <insert id="insertSelective" parameterType="com.zking.mybatis01.model.Book" >
        insert into t_book
        --trim 动态拼接
        --prefix 前缀 suffix后缀 suffixOverrides去除后缀指定字符  prefixOverrides 去除前缀指定字符
        <trim prefix="(" suffix=")" suffixOverrides=","  >
          <if test="bookId != null" >
            book_id,
          </if>
          <if test="bookName != null" >
            book_name,
          </if>
          <if test="bookNamePinyin != null" >
            book_name_pinyin,
          </if>
          <if test="bookPrice != null" >
            book_price,
          </if>
          <if test="bookType != null" >
            book_type,
          </if>
        </trim>
    

    3 foreach
    collection j有循环的集合
    item每次遍历的数据项名称
    separator分割符,每次循环后添加,最后一次不添加
    open 开始位置 close结束位置

      <select id="queryBookByForeach" resultType="com.zking.mybatis01.model.Book">
        select <include refid="Base_Column_List"></include> from t_book where 1=1 and book_id in
    --     collection j有循环的集合 item每次遍历的数据项名称 separator分割符,每次循环后添加,最后一次不添加 open 开始位置 close结束位置
        <foreach collection="ids" item="id" separator="," open="(" close=")">
          #{id}
        </foreach>
      </select>
    

    4 choose/set/where
    choose(判断参数) - 按顺序将实体类 Book 第一个不为空的属性作为:where条件
    判断成功则执行,不成功则不执行

    <!--  choose(判断参数) - 按顺序将实体类 Book 第一个不为空的属性作为:where条件 -->
      <select id="queryBookByChoose" resultType="com.zking.mybatis01.model.Book">
        select <include refid="Base_Column_List"/> from t_book where 1 = 1
        <choose>
          <when test="bookName != null">
            and book_name = #{bookName}
          </when>
          <when test="bookType != null">
            and book_type = #{bookType}
          </when>
          <otherwise>
            and book_id = 100
          </otherwise>
        </choose>
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月28日
  • 已采纳回答 6月20日
  • 创建了问题 6月16日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度