网站首页 > 教程分享 正文
动态SQL是MyBatis最强大的特性之一。用于实现动态SQL的主要元素如下:
1、if
EmpMapper.xml配置
<select id="getEmpByIf" resultType="Emp" parameterType="Emp"> select * from emp where 1 = 1 <if test="job != null and job != ''"> and job = #{job} </if> <if test="deptno != null "> and deptno = #{deptno} </if> </select>
2、choose、when、otherwise
类似于Java中的switch case default
EmpMapper.xml配置
<select id="getEmpByChoose" resultType="Emp" parameterType="Emp"> select * from emp where 1 = 1 <choose> <when test="job != null"> and job = #{job} </when> <when test="deptno != null"> and deptno = #{deptno} </when> <otherwise> and mgr = #{mgr} </otherwise> </choose> </select>
3、where
EmpMapper.xml配置
<select id="getEmpByWhere" resultType="Emp" parameterType="Emp"> select * from emp <where> <if test="job != null and job != ''"> and job = #{job} </if> <if test="deptno != null"> and deptno = #{deptno} </if> </where> </select>
4、set
EmpMapper.xml配置
<update id="updateEmpBySet" parameterType="Emp"> update emp <set> <if test="ename != null and ename != ''"> ename = #{ename}, </if> <if test="job != null and job != ''"> job = #{job}, </if> </set> where empno = #{empno}</update>
猜你喜欢
- 2024-09-09 SQL Server优化50法(sql server 优化)
- 2024-09-09 SQLServer-高级篇(sqlserver ag)
- 2024-09-09 2022-12-17:订单最多的客户。以下数据,结果输出3。请问sql语句
- 2024-09-09 springboot整合mybatis使用xml实现sql语句的查询配置
- 2024-09-09 Qt的数据库(Driver类、Query类、Model类、View类)
- 2024-09-09 VBA+ADO+SQL语句,小试牛刀。(vba的sql)
- 2024-09-09 MS SQL Server——SQL语句导入导出大全
- 2024-09-09 mysql根据条件执行sql(mysql根据条件查询)
- 2024-09-09 MyBatis3-动态SQL语句(navicat怎么写sql语句)
- 2024-09-09 SQL优化——IN和EXISTS谁的效率更高
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)