一篇文章搞懂Mybatis SQL动态处理

Mybatis SQL动态处理

Where 、if

  • where 标签:相当于sql语法中的where

  • if 判断标签

    • test 判断条件 ,满足条件,执行if标签中sql语句
<select id="findEmpByCondition" resultType="emp">
    select * from emp
    <where>
        <if test="empno != null">
            and empno= #{empno}
        </if>
    </where>
</select>

choose 、 when、otherwise

  • choose选择标签

  • when : 相当于 java 中case语法

  • otherwise :when标签都不满足,执行otherwise标签中内容

<select id="findEmpByCondition2" resultType="emp">
    select * from emp
    <where>
        <choose>
            <when test="empno != null">
                and empno= #{empno}
            </when>
            <when test="ename != null and ename != ''">
                and ename= #{ename}
            </when>
            <when test="job != null and job != ''">
                and job= #{job}
            </when>
            <when test="mgr != null ">
                and mgr= #{mgr}
            </when>
            <when test="hiredate != null ">
                and hiredate= #{hiredate}
            </when>
            <when test="sal != null">
                and sal= #{sal}
            </when>
            <when test="comm != null ">
                and comm =#{comm}
            </when>
            <when test="deptno != null ">
                and deptno= #{deptno}
            </when>
            <otherwise>
      				AND featured = 1
    		</otherwise>
        </choose>
    </where>
</select>

set

set 代替sql中set 语法

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>

trim

trim标签 增加前缀 、后缀 , 删除前缀、后缀

trim标签中属性:

  • prefix 增加前缀

  • suffix 增加后缀

  • prefixOverrides 删除前缀

  • suffixOverrides 删除后缀

<update id="updateAuthorIfNecessary">
  update Author
    <trim prefix="SET" suffixOverrides=",">
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio},</if>
    </trim>
  where id=#{id}
</update>

foreach

  • coollection 要迭代的集合,如:list 、 set 、map 、array
  • item 迭代临时变量值
  • index 当前迭代序号
  • open 以什么开头后开始遍历
  • close 以什么后结束遍历
<delete id="delectById">
    delete from project where project_id in
    <foreach collection="array" item="project_id" index="index"
             open="(" separator="," close=")">
        
            #{project_id}
    </foreach>
</delete>

bind

bind表达式以外创建一个变量,并将其绑定到当前的上下文

<select id="findAll2" resultMap="selectAll3">
    SELECT * FROM orders 
    <where>
        <if test="order_name !=null and order_name != ''">
            <bind name="tn" value="'%'+order_name+'%'"  />
            order_name like #{tn}
        </if>
    </where>
</select>

sql片段

将sql语句共同的部分抽出来形成一个独立的片段,哪里需要该片段就引用该片段

<sql id="empColumns">
    empno,ename,job,mgr,hiredate,sal,comm,deptno
</sql>
<sql id="baseEmpQuery">
    select
    <include refid="empColumns"/>
    from emp
</sql>

主键自增回填

方式1

  • useGeneratedKeys=“true” 表示要使用自增的主键

  • keyProperty=“deptno” 表示把自增的主键赋给JavaBean的哪个成员变量。

    使用对象get方法可以获取主键

<insert id="addDept" parameterType="dept" useGeneratedKeys="true"keyProperty="deptno" >
    
    insert into dept values(default,#{dname},#{loc})
    
</insert>

方式2

  • order:取值AFTER|BEFORE,表示在新增之后|之前执行selectKey标签中的SQL命令
  • keyProperty: 执行select @@identity后结果填充到哪个属性中
  • resultType: 结果类型
<insert id="addDept2" parameterType="dept">
  <selectKey order="AFTER" keyProperty="deptno"  resultType="int">
            select @@identity
   </selectKey>
    insert into dept values(null,#{dname},#{loc})
</insert>

模糊查询

方式1: 使用concat() 函数,对模糊条件进行拼接

<select id="getByName" parameterType="string" resultType="emp">
	select * from emp  where ename like concat( '%',#{ename},'%') 
</select>

方式2:使用bind标签

<select id="selectMore" resultMap="BaseResultMap2">
    select * from user 
    <where>
        <if test="name!=null and name!=''">
            <bind name="li" value="'%'+project.projectName+'%'"/>
            name like #{li}
        </if>
        <if test="age!=null and age!=''">
            <bind name="age" value="'%'+age+'%'"/>
            and  age=#{age}
        </if>
    </where>
</select>

resultMap 映射处理

单表映射关系处理

  • id 表示表主键字段
  • result 表示表普通字段

属性

  • type 返回类型

  • property 对应javaBean 属性

  • column 对应表字段 或 对映射表

  • javaType 表映射对象类型

<resultMap id="empMap" type="emp">
    <id property="empno" column="empno"></id>
    <result property="ename" column="ename"></result>
    <result property="job" column="job"></result>
    <result property="sal" column="sal"></result>
    <result property="hiredate" column="hiredate"></result>
    <result property="mgr" column="mgr"></result>
    <result property="comm" column="comm"></result>
    <result property="deptno" column="deptno"></result>
</resultMap>

一对一映射关系处理

标签

  • id 表示表主键字段
  • result 表示表普通字段
  • association 表连接一对一关系映射

属性

  • type 返回类型

  • property 对应javaBean 属性

  • column 对应表字段 或 对映射表

  • javaType 表映射对象类型

<!--使用resultMap定义一对一映射关系-->
 <resultMap id="empJoinDept" type="emp">
     <!--处理emp原有8个属性 不能省略-->
     <id property="empno" column="empno"></id>
     <result property="ename" column="ename"></result>
     <result property="job" column="job"></result>
     <result property="sal" column="sal"></result>
     <result property="hiredate" column="hiredate"></result>
     <result property="mgr" column="mgr"></result>
     <result property="comm" column="comm"></result>
     <result property="deptno" column="deptno"></result>

     <!--将deptno dname loc 映射景dept属性
          association 处理一对一映射关系
          property 给实体类属性赋值
          javaType 要映射的属性是哪个类
      -->
     <association property="dept" javaType="dept" >
         <!--处理一对一属性映射-->
         <id property="deptno" column="deptno"></id>
         <result property="dname" column="dname"></result>
         <result property="loc" column="loc"></result>
     </association>
 </resultMap 
 <select id="findByJoinDept" resultMap="empJoinDept">
    select * from emp e
    left outer join dept d
    on e.deptno=d.deptno where empno=#{empno}
 </select>

一对多映射关系处理

标签

  • id 表示表主键字段
  • result 表示表普通字段
  • collection 表连接多对多关系映射

属性

  • type 返回类型

  • property 对应javaBean 属性

  • column 对应表字段 或 对映射表

  • javaType 表映射对象类型

<resultMap id="empJoinPs" type="emp" >
    <id property="empno" column="empno"></id>
    <result property="ename" column="ename"></result>
    <result property="job" column="job"></result>
    <result property="sal" column="sal"></result>
    <result property="hiredate" column="hiredate"></result>
    <result property="mgr" column="mgr"></result>
    <result property="comm" column="comm"></result>
    <result property="deptno" column="deptno"></result>
    <collection property="projectrecords" ofType="projectrecords">
        <id property="empno" column="empno"></id>
        <id property="pid" column="pid"></id>
    </collection>
</resultMap>
 <select id="getEmpPs" resultMap="empJoinPs">
        select * from emp e
        join  projectrecords s
        on e.empno=s.empno
        where e.empno=#{empno}
  </select>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值