mybatis的动态sql语句
2017-11-11 18:08
288 查看
具体教程在mybatis的官网中有,这里只是一些小例子。
动态sql语句在test中使用的是OGNL表达式
trim
在条件前添加where,条件后删除and
<select id="findEmps" resultMap="empMap" >
select * from emp
<!--
prefix 给sql语句增加前缀
prefixOverrides 覆盖前缀
suffix 给sql语句增加后缀
suffixOverrides 覆盖后缀
-->
<trim prefix="where" suffixOverrides="and" >
<if test="lastName!=null and lastName.trim!=''">
ename like #{lastName} and
</if>
<if test="job!=null">
job = #{job} and
</if>
<if test="salary!=null and salary>100">
salary > #{salary}
</if>
</trim>
</select>
where
<!--where 增加where关键字 去除前面多余的and -->
<select id="findEmpsByWhere" resultMap="empMap" >
select * from emp
<where>
<if test="lastName!=null && lastName.trim!=''">
ename like #{lastName}
</if>
<if test="job!=null">
and job = #{job}
</if>
<if test="salary!=null and salary>100">
and salary > #{salary}
</if>
</where>
</select>
set
用于update语句
<update id="updateEmp">
update emp
<set>
<if test="lastName!=null and lastName.trim()!=''">
ename = #{lastName},
</if>
<if test="job!=null">
job = #{job},
</if>
<if test="salary>100">
salary = #{salary}
</if>
</set>
<if test="empno!=null">
where empno = #{empno}
</if>
</update>
foreach
<sql id="empSql">
select * from emp
</sql>
<select id="findEmpsByEmpnos" resultMap="empMap">
<!--
原来的sql语句格式为insert into emp values(....),(...),(....)
而且这里使用了sql语句的引用include,这样重复的sql语句片段就不需要写多次
-->
<include refid="empSql"/>
where empno in
<!--(1,2,3) collection要么写list,要么使用@Param取别名,这里就是取别名 -->
<foreach collection="empnos" item="empno" open=" ("
close=")" separator=",">
#{empno}
</foreach>
</select>
动态sql语句在test中使用的是OGNL表达式
trim
在条件前添加where,条件后删除and
<select id="findEmps" resultMap="empMap" >
select * from emp
<!--
prefix 给sql语句增加前缀
prefixOverrides 覆盖前缀
suffix 给sql语句增加后缀
suffixOverrides 覆盖后缀
-->
<trim prefix="where" suffixOverrides="and" >
<if test="lastName!=null and lastName.trim!=''">
ename like #{lastName} and
</if>
<if test="job!=null">
job = #{job} and
</if>
<if test="salary!=null and salary>100">
salary > #{salary}
</if>
</trim>
</select>
where
<!--where 增加where关键字 去除前面多余的and -->
<select id="findEmpsByWhere" resultMap="empMap" >
select * from emp
<where>
<if test="lastName!=null && lastName.trim!=''">
ename like #{lastName}
</if>
<if test="job!=null">
and job = #{job}
</if>
<if test="salary!=null and salary>100">
and salary > #{salary}
</if>
</where>
</select>
set
用于update语句
<update id="updateEmp">
update emp
<set>
<if test="lastName!=null and lastName.trim()!=''">
ename = #{lastName},
</if>
<if test="job!=null">
job = #{job},
</if>
<if test="salary>100">
salary = #{salary}
</if>
</set>
<if test="empno!=null">
where empno = #{empno}
</if>
</update>
foreach
<sql id="empSql">
select * from emp
</sql>
<select id="findEmpsByEmpnos" resultMap="empMap">
<!--
原来的sql语句格式为insert into emp values(....),(...),(....)
而且这里使用了sql语句的引用include,这样重复的sql语句片段就不需要写多次
-->
<include refid="empSql"/>
where empno in
<!--(1,2,3) collection要么写list,要么使用@Param取别名,这里就是取别名 -->
<foreach collection="empnos" item="empno" open=" ("
close=")" separator=",">
#{empno}
</foreach>
</select>
相关文章推荐
- [MyBatis]动态输出SQL语句
- Mybatis之动态构建SQL语句
- MyBatis第八步、动态SQL语句
- MyBatis 向Sql语句中动态传参数·动态SQL拼接
- mybatis 动态sql语句(3)
- mybatis动态SQL语句
- MyBatis知识六 动态SQL语句
- MyBatis动态SQL语句
- MyBatis 动态SQL语句
- mybatis动态SQL语句
- Mybatis:在sqlBuilder中动态的生成sql语句
- 最常用的动态sql语句梳理Mybatis
- MyBatis注解应用之动态SQL语句
- mybatis 动态SQL 语句
- mybatis动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- MyBatis3-动态SQL语句
- MyBatis教程之分页和动态SQL语句基础
- mybatise自定义插件或者叫mybatise拦截器,动态修改sql语句