mybatis使用技巧总结

  • 动态sql使用

    一般我们会涉及多个动态查询条件,一般我们是通过 where 1 = 1,这样可以处理where后面对应全空的情况,我们可以使用标签,该标签可以自动处理。

    <where>  
    <if test="name != nulll">  
       and name like concat('%',trim(#{name,jdbcType=VARCHAR}),'%')   
    </if>  
    </where>  
  • 重复的sql片段

    定义:
    <sql id="Base_Column_List" >  
      id, name, url, priority, logo, img  
    </sql>  
    引用:
    <include refid="Base_Column_List" /> 
  • 插入时主键返回

    <insert id="insert" parameterType="org.andy.shop.model.CourseInfo" >  
      insert into course_info (id, cname, caddress)  
      values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR})  
    
       <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">  
         select last_insert_id()  
       </selectKey>  
    </insert>  

    <insert id="insert" parameterType="org.andy.shop.model.CourseInfo" useGeneratedKeys="true" keyProperty="id">  
      insert into course_info (id, cname, caddress)  
      values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR})  
    </insert>  

    非自增主键:但我们的ID为uuid字符类型的32为长度时,我们使用mysql的uuid()查询主键,是在查询之后在插入。

    <insert id="insert" parameterType="org.andy.shop.model.CourseInfo" >  
      <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">  
        select uuid()  
      </selectKey>  
      insert into course_info (id, cname, caddress)  
      values (#{id,jdbcType=VARCHAR}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR})  
    </insert>  

    使用oracle数据库时:使用 序列名.nextval()函数获取。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值