mybatis的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="cn.ssm.dao.EmpDao">  
  
    <!-- 查询全部的员工 -->  
  <select id="findAll" resultType="cn.ssm.entity.Emp">  
    select * from t_emp  
  </select>  
    
  <!-- if的用法 -->  
  <select id="findByDept" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition">  
    select * from t_emp  
        <if test="deptno !=null">  
            where deptno = #{deptno}  
        </if>  
  </select>  
    
  <!-- chose的用法 -->  
  <select id="findBySalary" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition">  
    select * from t_emp  
    <choose>  
        <when test="salary>3000">  
            where sal>#{salary}  
        </when>  
        <otherwise>  
            where sal>3000  
        </otherwise>  
    </choose>  
  </select>  
    
  <!-- where -->  
  <!-- 查询当前部门下,大于当前收入的员工-->  
  <select id="findByDeptAndSalary" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition">  
    select * from t_emp  
    <where>  
        <if test="deptno !=null">  
            and deptno=#{deptno}  
        </if>  
        <if test="salary!=null">  
            and sal>#{salary}  
        </if>  
    </where>  
  </select>  
    
  <!-- update -->  
  <!-- 更新员工信息 -->  
  <update id="update"  parameterType="cn.ssm.entity.Condition">  
        update t_emp  
        <set>  
            <if test="ename!=null">  
                ename=#{ename}  
            </if>  
            <if test="job!=null">  
                job=#{job}  
            </if>  
        </set>  
        where empno=#{empno}  
  </update>  
    
  <!-- 使用trim代替where -->  
  <!-- 查询 -->  
  <select id="findByDeptAndSalary2" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition">  
    select * from t_emp  
        <trim prefix="where" prefixOverrides="and" >  
            <if test="deptno!=null">  
                and deptno=#{deptno}  
            </if>  
            <if test="salary !=null">  
                and sal>#{salary}  
            </if>  
        </trim>  
  </select>  
  <!-- 使用trim代替set -->  
  <!-- 更新 -->    
  <update id="update2">  
    update t_emp  
        <trim prefix="set" prefixOverrides="," >   
            <if test="ename!=null">  
                ename=#{ename},  
            </if>  
            <if test="job!=null">  
                job=#{job},  
            </if>  
        </trim>  
        where empno=#{empno}  
  </update>  
    
  <!-- 根据id查询 -->  
  <select id="findById" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition">  
    select * from t_emp where empno in  
        <foreach collection="empnos" open="(" close=")" separator="," item="id">  
            #{id}     
        </foreach>  
  </select>  
</mapper>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值