应用场景

  • 有时候需要根据实际传入的参数来动态的拼接SQL语句

应用

在EmployeeMapper接口中添加一个方法:

public List<Employee> getEmployeeByConditionIf(Employee employee);

按需查询:

<select id="getEmployeeByConditionIf" resultType="com.neuedu.entity.Employee">select *from tbl_employee where <!--test:判断表达式(OGNL)OGNL参照PPT或者官方文档。c:if test从参数中取值进行判断遇见特殊符号,应该去写转义字符(&):参考W3CSchool>>HTML>>ISO8859--><if test="id != null">id = #{id}</if><if test="userName != null &amp;&amp; userName !=''">and user_name = #{userName} </if><if test="email != null and email.trim() != &quot;&quot;">and email = #{email}</if><!-- ognl会进行字符串和数字的转换判断;"0"==0,"1"==1 --><if test="gender == 0 or gender == 1">and gender = #{gender} </if></select>
  • mybatis可以使用where标签来将所有的查询条件包括在内。mybatis就会将where标签中拼装的sql, 多出来的and或者or去掉,但是where标签只会去掉第一个多出来的and或者or
  • 使用trim标签

    <select id="getEmployeeByConditionIf" resultType="com.neuedu.entity.Employee">select *from tbl_employee<!-- 后面多出的and或者or where标签不能够解决prefix="":前缀:trim标签体重是整个字符串拼串后的结果。prefix给拼串后的整个字符串加一个前缀prefixOverrides="":前缀覆盖:去掉整个字符串前面多余的字符suffix="":后缀suffix给拼串后的整个字符串加一个后缀suffixOverrides="":后缀覆盖:去掉整个字符串后面多余的字符--> <trim prefix="where" suffixOverrides="and"><if test="id != null">id = #{id} and</if><if test="userName != null &amp;&amp; userName !=''">user_name = #{userName} and  </if><if test="email != null and email.trim() != &quot;&quot;">email = #{email} and </if><!-- ognl会进行字符串和数字的转换判断;"0"==0,"1"==1 --><if test="gender==0 or gender==1">gender = #{gender}</if>     </trim></select>
    
  • choose标签
    只会加载一个when标签

    <select id="getEmployeeByConditionChoose" resultType="com.neuedu.entity.Employee">select *from tbl_employee<where><choose><when test="id != null">id = #{id}</when><when test="userName != null">user_name like #{userName}</when><when test="email != null">email = #{email}</when><otherwise><!-- 查询全部-->1=1</otherwise></choose></where></select>
    
  • update中的set标签【set标签可以将字段后面的逗号去掉】

     <update id="updateEmp">update tbl_employee <set><if test="userName != null">user_name = #{userName},</if><if test="email != null">email = #{email},</if><if test="gender != null">gender = #{gender},</if></set>where id = #{id}
    </update>
    
  • trim标签代替set标签

     <update id="updateEmp">update tbl_employee <trim prefix="set" suffixOverrides=","><if test="userName != null">user_name = #{userName},</if><if test="email != null">email = #{email},</if><if test="gender != null">gender = #{gender},</if></trim>where id = #{id}</update>
    
  • foreach:遍历元素
    批量查询
    public List getEmpsByConditionForeach(@Param(“ids”) List ids);

     <select id="getEmpsByConditionForeach" resultType="com.neuedu.entity.Employee">select * from  tbl_employee where id in<!-- collection:指定要遍历的集合item:将当前遍历出的元素赋值给指定的变量separator:每个元素之间的分隔符open:遍历出所有记过拼接一个开始的字符close:遍历出所有结果拼接一个结束的字符--><foreach collection="ids" open="(" close=")" separator="," item="id">#{id}</foreach></select>
    

    批量插入数据

    <insert id="addEmps">INSERT INTO tbl_employee(user_name,gender,email,d_id) VALUES<foreach collection="emps" item="emp" separator=",">(#{emp.userName},#{emp.gender},#{emp.email},#{emp.depart.id})</foreach></insert>
    

Dynamic SQL(动态SQL)相关推荐

  1. sql动态sql给变量复值_在动态SQL中使用变量

    sql动态sql给变量复值 Before we delve into these SQL concepts, note that I like to do all my development in ...

  2. PL/SQL -- 动态SQL调用包中函数或过程

    动态SQL主要是用于针对不同的条件或查询任务来生成不同的SQL语句.最常用的方法是直接使用EXECUTE IMMEDIATE来执行动态SQL语句字符串或字符串变量.但是对于系统自定义的包或用户自定的包 ...

  3. PL/SQL -- 动态SQL

    --==================== -- PL/SQL --> 动态SQL --==================== 使用动态SQL是在编写PL/SQL过程时经常使用的方法之一.很 ...

  4. Oracle 动态游标 PL/SQL 动态SQL语句 open for [using] 语句

    PL/SQL:open for [using] 语句 2017年07月19日 09:52:55 学孩儿无牙哭做粥 阅读数:681 标签: oracleSQLPLSQL 更多 个人分类: ORACLES ...

  5. 8-Mybatis 的动态 SQL 语句

    Mybatis 的映射文件中,前面我们的 SQL 都是比较简单的,有些时候业务逻辑复杂时,我们的 SQL 是动态变 化的,此时在前面的学习中我们的 SQL 就不能满足要求了. 参考的官方文档,描述如下 ...

  6. 第十三章 使用动态SQL(一)

    文章目录 第十三章 使用动态SQL(一) 动态SQL简介 动态SQL与嵌入式SQL `%SQL.Statement`类 创建一个对象实例 %SelectMode属性 %SchemaPath属性 %Di ...

  7. (Mybatis)动态SQL

    文章目录 动态SQL 1.环境搭建 2.IF 3.choose (when, otherwise) 4.trim (where,set) 5.SQL片段 6.Foreach 动态SQL 动态SQL就是 ...

  8. MyBatis 实践 -动态SQL/关联查询

    MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...

  9. mybatis入门(四)之动态SQL

    转载自  mybatis 动态SQL 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦. ...

最新文章

  1. 插入的表单控制下拉框怎么设置_想要告别表单重复填写?这一个功能就够了
  2. UVa1062 - Containers贪心算法
  3. linux中添加PHP的mongoDB支持扩展
  4. 安天365第二期线上交流
  5. Linux apt-get install无法定位问题
  6. CDH HUE集成yarn
  7. matlab simulink_运用MATLAB和Simulink开发自动驾驶控制系统
  8. Mr.J-- jQuery学习笔记(三)--静态方法详解
  9. db2 设置表 not null
  10. 电脑音箱常见故障排查技巧
  11. hutol json null值没了_详解Java去除json数据中的null空值问题
  12. Sublime Text编写80×86汇编.asm文件的语法高亮插件
  13. yolov5继续训练_震惊! 它来了!YOLOv5它来了!
  14. 1.创建一个 Slim 应用
  15. html js 处理元素的数据属性(data)
  16. 批量删除新浪微博关注
  17. 蓝桥杯 Java 算法提高 盾神与积木游戏
  18. c语言如何编程出声音,C语言播放声音最简单的两种方法
  19. vs 应用程序无法正常启动0xc000007b 可能与VS中DLL引用有关
  20. SAR/GMTI-概述及常用抑制杂波方法DPCA

热门文章

  1. 【python】python制作暗黑2 建号器
  2. android 自定义View 视差动画
  3. QTabWidget的tabbar右侧背景色设置
  4. IT售前咨询能力范围
  5. MAR在计算机组成原理上是啥,《计算机组成原理》(答案已补全).doc
  6. 出门吃饭,手机没电,有感——为什么共享充电宝能活?
  7. 襄阳文理学院计算机分数线,湖北文理学院理工学院历年录取分数线多少及各省最低投档线统计表...
  8. iOS 使用Moya网络请求
  9. 2022年中国企业500强研究报告
  10. 这年头,能坐上火箭的东西不多啊,Java版本号算一个