Mybatis中Mapper.xml动态SQL where和set要注意的两个问题

  • 1、where标签只有帮你动态去掉and和or的作用,它不能帮你动态添加!
        select * from emp
        <where>
            <if test="name!=null">`name` like concat('%', #{name}, '%')</if>
            <if test="gender!=null">and gender = #{gender}</if>
            <if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if>
        </where>

这样才是对的,千万不要以为能自动给你加上and

  • 2、set标签只有帮你动态去掉逗号的作用,它不能帮你动态添加!
	<set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>

逗号你自己不能省去!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records. Contents What is MyBatis? .. 5 Getting Started ...... 5 Building SqlSessionFactory from XML ............. 5 Building SqlSessionFactory without XML ........ 6 Acquiring a SqlSession from SqlSessionFactory ............. 6 Exploring Mapped SQL Statements . 7 A Note about Namespaces........... 8 Scope and Lifecycle .......... 9 Mapper Configuration XML  10 properties ........ 11 settings ............ 12 typeAliases ...... 13 typeHandlers ... 14 objectFactory .. 15 plugins ............. 16 environments .. 17 transactionManager .... 18 dataSource .. 19 mappers .......... 21 SQL Map XML Files ............. 21 select  22 insert, update, delete ...... 24 sql ..... 26 Parameters ...... 26 MyBatis 3 - User Guide 5 November 2010 4 resultMap ........ 28 Advanced Result Mapping ......... 30 id, result ...... 32 Supported JDBC Types  33 constructor .. 33 association .. 34 collection ..... 37 discriminator .............. 40 cache  41 Using a Custom Cache . 43 cache-ref ......... 44 Dynamic SQL ....... 44 if ....... 44 choose, when, otherwise  45 trim, where, set .............. 45 foreach ............ 47 Java API  49 Directory Structure ........ 49 SqlSessions ...... 50 SqlSessionFactoryBuilder ........... 50 SqlSessionFactory....... 52 SqlSession .... 54 SelectBuilder ....... 60 SqlBuilder ............ 63
目录 什么是MyBatis?............................................................................................................. 5 入门................................................................................................................................. 5 从XML构建SqlSessionFactory .............................................................................. 5 不使用XML构建SqlSessionFactory........................................................................... 6 从SqlSessionFactory获取SqlSession....................................................................... 6 探究已映射的SQL语句 ............................................................................................ 7 命名空间的一点注释 .......................................................................................... 8 范围和生命周期 ........................................................................................................ 8 SqlSessionFactoryBuilder ..................................................................................... 8 SqlSessionFactory................................................................................................ 9 SqlSession .......................................................................................................... 9 Mapper实例 ....................................................................................................... 9 XML映射配置文件 ........................................................................................................ 10 properties................................................................................................................. 10 Settings ....................................................................................................................11 typeAliases .............................................................................................................. 12 typeHandlers ............................................................................................................ 13 objectFactory ........................................................................................................... 14 plugins .................................................................................................................... 15 environments............................................................................................................ 16 transactionManager ............................................................................................ 17 dataSsource....................................................................................................... 17 mappers ................................................................................................................... 19 SQL映射的XML文件 ................................................................................................... 19 select....................................................................................................................... 20 insert,update,delete .............................................................................................. 21 sql........................................................................................................................... 23 Parameters ............................................................................................................... 24 resultMap ................................................................................................................ 25 高级结果映射................................................................................................... 27 id,result.......................................................................................................... 29 支持的JDBC类型 ............................................................................................ 30 构造方法.......................................................................................................... 30 关联................................................................................................................. 31 集合................................................................................................................. 34 鉴别器 ............................................................................................................. 36 缓存........................................................................................................................ 38 使用自定义缓存 ............................................................................................... 38 参照缓存................................................................................................................. 39 动态SQL ................................................................................................................ 39 if ..................................................................................................................... 40 choose, when, otherwise ..................................................................................... 40 trim, where, set .................................................................................................. 41 foreach ............................................................................................................. 43 Java API ......................................................................................................................... 43 应用目录结构.......................................................................................................... 43 SqlSessions .............................................................................................................. 44 SqlSessionFactoryBuilder ................................................................................... 44 SqlSessionFactory.............................................................................................. 46 SqlSession ........................................................................................................

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值