ibatis SQL记录


iBATIS在使用过程中SQL遇到的一些问题,记录以供学习!


<resultMap id="POMap"
type="PO">
<result property="name" column="NAMW" />
<result property="age" column="AGE" />
</resultMap>

<select id="aa"
parameterType="VO"
resultMap="POMap">
select name,age from 
user  p
where 
<![CDATA[
p.chg_time >= #{fromDate}

and  p.chg_time < #{toDate}
]]>
and p.seat_status in
<foreach collection="b" item="age" separator=","
open="(" close=")">
'${age}'
</foreach>
</select>

其中id="aa"代表mapper中方法的别名,parameterType是传递的参数类型,resultMap是查询结果(一般都为bean),参数可以使用EL表达式#{}在传递参数

#{}可以直接吧类型


<insert id="bb" 
    parameterType="Vo" >
    INSERT INTO db_user (
    NAME,
    AGE
    ) 
    (
      select t.name as name,
    t.age as age
    from sys_user t
    where 
    t.age is not null
    <if test="fromDate != null">
         <![CDATA[
               and t.tkt_time >= #{fromDate}
             ]]>
    </if>
    <if test="toDate != null">
           <![CDATA[
               and t.tkt_time < #{toDate}
             ]]>
    </if>
    )
  </insert>


<insert id="cc" parameterType="PO">
   INSERT INTO db_user (
    name,
    time,
    age
    )
    VALUES (
      #{name javaType=String, jdbcType=VARCHAR},
      #{time javaType=java.sql.Timestamp, jdbcType=TIMESTAMP},
      #{age javaType=int, jdbcType=NUMERIC},
    )
  </insert>


  <update id="dd" parameterType="PO">
      update db_user
    set NAME = #{name javaType=java.sql.Timestamp, jdbcType=TIMESTAMP},
      AGE = #{age javaType=int, jdbcType=NUMERIC},
    where ID=#{id}
  </update>


<delete id="ee" parameterType="String">
  delete from db_user   where ID = #{id}
</delete>


还有一些循环,判断的写法:


if判断加foreach循环:

<if test="status != null">
WHERE t.status in

<foreach collection="status" item="s" separator="," open="(" close=")">
${s}
</foreach>
</if>


choose用法:

<choose>
   <when test="orderKey == 'age'">
       t.age
   </when>

<otherwise>
       t.age
   </otherwise>
   </choose>


以上只是简单的列举了iBATIS中用到的一些SQL和条件语句,做个备忘!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值