SSM框架之Mybatis动态sql配置
2017-11-18 22:20
666 查看
SSM框架之Mybatis框架的动态sql配置
Mybatis官方中文文档http://www.mybatis.org/mybatis-3/zh/index.html
在实际的场景中,经常会遇到动态SQL的增、删、改、查问题,这里就必要说明何谓动态SQL,我们举一个实际的例子,比如,在一个web工程中,经常会有一个搜索框,并且在搜索之前通常会进行一个关键词的过滤,比如可以过滤的条件有:姓名、年龄等,这样的话当我们姓名和年龄都不选,则等价于下面的SQL语句
select * from students; – 不去限制姓名和年龄
1
当我们将年龄选择为>20时,相当于下面的SQL语句:
select * from students where age>20;
1
当我们同时选择条件姓名为:张三,年龄>20,则相当于下面的SQL语句:
select * from students where age>20 and name=’张三’;
1
当我们有很多的条件时,此时就需要我们去组合这些条件,并动态的生成一个可执行的SQL语句,这样就不是一个简单的SQL语句能够解决问题,那么我们该怎么办呢?在MyBatis中同样是支持这种动态SQL的写法,具体见下面的内容。
MyBatis动态SQL支持
动态SQL之查询
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yztc.yx.pojo.Emp"> <!-- resultMap标签将了工程entity实体类中的对象与数据库中的表对应起来 resultMap中的id属性是一个唯一的名字 子标签中的id属性用来指定主键 result标签用来指定其他的键,其中property属性是指实体中的字段,对应的 column属性表示的数据库中的响应的字段 --> <!-- 动态查询SQL语句 --> <!-- 多条件查询 xml中对于特殊符号的处理,有两种思路: 1.使用<![CDATA[我的文本]]> 2.使用转义字符来替代特殊符号 或者between and --> <select id="findEmpBySalNameJob" parameterType="com.yztc.yx.pojo.EmpCondition" resultType="com.yztc.yx.pojo.Emp"> <!-- <![CDATA[select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where ename like '%${kw}%' a 4000 nd job=#{job} and sal>=#{losal} and sal<=#{hisal}]]> --> select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where ename like '%${kw}%' and job=#{job} and sal<=#{hisal} and sal>=#{losal} </select> </mapper>
动态SQL之插入
<!-- 动态insert --> <!-- 定义两个sql片段,第一个对应字段名,id属性值任意并且唯一 --> <sql id="key"> <!--后缀判断的条件 --> <trim suffixOverrides=","> <if test="ename!=null"> ename, </if> <if test="job!=null"> job, </if> <if test="mgr!=null"> mgr, </if> <if test="hiredate!=null"> hiredate, </if> <if test="sal!=null"> sal, </if> <if test="comm!=null"> comm, </if> <if test="deptno!=null"> deptno, </if> </trim> </sql> <sql id="value"> <trim suffixOverrides=","> <if test="ename!=null"> #{ename},<!-- ognl表达式 --> </if> <if test="job!=null"> #{job}, </if> <if test="mgr!=null"> #{mgr}, </if> <if test="hiredate!=null"> #{hiredate}, </if> <if test="sal!=null"> #{sal}, </if> <if test="comm!=null"> #{comm}, </if> <if test="deptno!=null"> #{deptno}, </if> </trim> </sql><!-- 添加 --> <insert id="add" parameterType="com.yztc.yx.pojo.Emp"> insert into emp( <include refid="key" /> ) values ( <include refid="value" /> ) </insert>
动态SQL之删除
<!-- 动态批量删除 --> <delete id="delete" parameterType="int[]"> delete from emp where empno in <!-- foreach:用来循环 collection : 用来指定循环的数据的类型 可以填的值有:array,list,map item : 循环中为每个循环的数据指定一个别名 index : 循环中循环的下标,于参数名一致 open : 开始 close : 结束 separator : 数组中元素之间的分隔符 --> <foreach collection="array" item="arrs" open="(" separator="," close=")">#{arrs}</foreach> </delete>
动态SQL之更新
<!-- 动态的修改 --> <update id="update" parameterType="com.yztc.yx.pojo.Emp"> update emp <set><!--set会删除多余条件的逗号 --> <if test="ename">ename=#{ename},</if> <if test="job">job=#{job},</if> <if test="mgr">mgr=#{mgr},</if> <if test="hiredate">hiredate=#{hiredate},</if> <if test="sal">sal=#{sal},</if> <if test="comm">comm=#{comm},</if> <if test="deptno">deptno=#{deptno},</if> </set> where empno=#{empno} <!-- trim : 自定义标签 prefix : 标签名 prefixOverrides : 自动去除第一个条件的内容 suffix : 标签名 suffixOverrides : 自动去除最后一个条件的内容 --> <!-- <trim prefix="where" prefixOverrides="and | or" ></trim> <trim suffix="set" suffixOverrides="," ></trim> --> </update>
动态SQL之条件不确定删选
<!-- 多条件查询,条件个数不确定:select <include refid="myempcolumn"/> from emp 通过动态sql语句 ,达到筛选条件的目的 --> <select id="findEmpChanges" parameterType="com.yztc.yx.pojo.EmpCondition" resultType="com.yztc.yx.pojo.Emp"> select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp <where><!--可自动去除条件中的and --> <if test="kw!=null">and ename like '%${kw}%'</if> <if test="job!=null">and job=#{job}</if> <if test="losal!=null&&hisal!=null">and sal between #{losal} and #{hisal} </if> </where> <!-- 类似于java中的switch结构,每次执行的时候,选择其中的一种情况来执行 上面的where标签,条件成立的情况下,三个条件都会执行 --> <!-- <choose> <when test="kw!=null"> ename like '%${kw}%' </when> <when test="job!=null"> and job=#{job} </when> <otherwise>....</otherwise> </choose> --> </select>
相关文章推荐
- SSM框架day02-MyBatis——040——动态SQL-SQL片段
- ssm框架学习---mybatis中动态sql中的sql片段
- ssm框架学习---mybatis中动态sql中的if片段
- 【MyBatis框架】mapper配置文件-关于动态sql
- 【MyBatis框架】mapper配置文件-关于动态sql
- 【MyBatis框架】mapper配置文件-关于动态sql
- ssm项目使用Mybatis动态拼接sql语句,生成的sql中文全部显示为???的问题(配置文件sql语句中文解析问题)
- SSM框架day02-MyBatis——066——动态SQL查询、067 MyBatis注解-动态SQL增删改、068 MyBatis注解-动态SQL的SQL类
- SSM框架day02-MyBatis——034——符合查询问题演示、035——动态SQL
- ssm框架学习---mybatis中动态sql中的foreach片段
- ssm框架配置log4j打印mybatis的sql语句
- SSM框架之MyBatis 动态SQL使用
- SSM框架day02-MyBatis——036 动态SQL-where、037 动态SQL-choose、038 动态SQL-foreach数组、039 动态SQL-foreachList
- 【MyBatis框架】mapper配置文件-关于动态sql
- MyBatis框架(三)动态SQL,分页,二进制存入数据库图片
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
- Mybatis步步进阶(三)——1:N关系配置 && 动态SQL拼接
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
- MyBatis中实现动态的SQL语句、分页以及mybatis的常用的配置
- MyBatis框架的 动态SQL