MyBatis

 

动态sql

1.if+where标签
<select id="listEmp" parameterType="Emp" resultType="Emp">
    select * from emp 
    <where>
        <if test="job!=null and job!=''">
            and job like concat('%',#{job},'%')
        </if>
        <if test="deptno!=null and deptno!=''">
            and deptno = #{deptno}
        </if>
    </where>
    order by empno
</select>

if+where会实现以下功能:

  1. 自动添加where
  2. 不需要考虑where后是否加and,mybatis会自动处理
  3. 不需要考虑是否加空格,mybatis会自动处理
  4. 没有 else 标签,也没有 else if 标签。

注意: job!='' 此处只可以判断是否为空,不能判断是否为某个值。也就是说:job!='经理' 是不好使的。

 

 2.choose标签

<select id="listEmp" parameterType="Emp" resultType="Emp">
    select * from emp 
    <where>
        <choose>
            <when test="job!=null and job!=''">
                and job like concat('%',#{job},'%')
            </when>
            <when test="deptno!=null and deptno!=''">
                and deptno = #{deptno}
            </when>
            <otherwise></otherwise>
        </choose>
    </where>
    order by empno
</select>

choose会实现如下功能:

  1. 多个 when 标签中,只能执行一个。也就是说:当一个 when 条件满足并执行后,其它的 when 将不再执行。
  2. 当所有 when 都不满足条件时,执行 otherwise 标签。

if 与 choose 的区别:if 相当于java中的if语句; choose相当于java中的switch语句。

 

3.trim标签

<insert id="insertEmp1" parameterType="Emp">
    insert into emp
    <trim prefix="(" suffix=")" suffixOverrides=",">
        ename,deptno,
        <if test="job!=null and job!=''">
            job,
        </if>
        <if test="hiredate!=null and hiredate!=''">
            hiredate,
        </if>
    </trim>
    <trim prefix="values(" suffix=")" suffixOverrides=",">
        #{ename},#{deptno},
        <if test="job!=null and job!=''">
            #{job},
        </if>
        <if test="hiredate!=null and hiredate!=''">
            #{hiredate},
        </if>
    </trim>
</insert>

  1. prefix与suffix可以在sql语句中拼接出一对小括号。
  2. suffixOberrides可以将最后一个逗号去掉。

 

4.set标签

<update id="updateEmp" parameterType="Emp">
    update emp 
    <set>
        <if test="job!=null and job!=''">
            job=#{job},
        </if>
        <if test="sal!=null and sal!=''">
            sal=#{sal},
        </if>
    </set>
    where empno=#{empno}
</update>

  1. set可以在sql语句中自动加上set关键词。
  2. 可以自动将最后的逗号去掉。
  3. 上面写法中,必须要保证有一个if成立

 

5.foreach标签

<delete id="deleteEmp" parameterType="int">
    delete from emp where empno in
    <foreach collection="array" item="empno" open="(" close=")" separator=",">
        #{empno}
    </foreach>
</delete>

foreach标签的属性:

  1. collection:需要遍历的类型,值有:list、array
  2. item:表示遍历出来的对象
  3. open:表示语句的开始部分
  4. close:表示语句的结束部分
  5. separator:表示每次迭代之间以什么符号为间隔
  6. index:每次迭代的位置索引,就是循环变量

 

关联查询

1.多对一关联查询    emp是多的一方,dept是一的一方。

<resultMap type="Emp" id="empResultMap">
    <id property="empno" column="empno"/>
    <result property="ename" column="ename"/>
    <result property="job" column="job"/>
    <result property="hiredate" column="hiredate"/>
    <result property="sal" column="sal"/>
    <result property="deptno" column="deptno"/>
    <association property="dept" javaType="Dept">
        <id property="deptno" column="ddeptno"/>
        <result property="dname" column="ddname"/>
        <result property="loc" column="dloc"/>
    </association>
</resultMap>
<select id="getEmpById" parameterType="int" resultMap="empResultMap">
    select e.*,
           d.deptno ddeptno,
           d.dname ddname,
           d.loc dloc
    from emp e left join dept d
         on e.deptno=d.deptno
    where e.empno = #{empno}
</select>

 

2.一对多关联查询   Dept是一的一方,Emp是多的一方

<resultMap type="Dept" id="deptResultMap">
    <id property="deptno" column="deptno"/>
    <result property="dname" column="dname"/>
    <result property="loc" column="loc"/>
    <collection property="empList" ofType="Emp">
        <id property="empno" column="eempno"/>
           <result property="ename" column="eename"/>
        <result property="job" column="ejob"/>
        <result property="hiredate" column="ehiredate"/>
        <result property="sal" column="esal"/>
        <result property="deptno" column="edeptno"/>
    </collection>
</resultMap>
<select id="getDeptById" parameterType="int" resultMap="deptResultMap">
    select d.*,
           e.empno eempno,
           e.ename eename,
           e.job ejob,
           e.hiredate ehiredate,
           e.sal esal,
           e.deptno edeptno
    from dept d left join emp e
         on d.deptno=e.deptno
    where d.deptno = #{deptno}
</select>

 

3.多对一关联

<select id="getDeptByIdLazy" parameterType="int" resultType="Dept">
select * from dept where deptno = #{deptno}
</select>

<resultMap type="Emp" id="empResultMap">
    <id property="empno" column="empno"/>
    <result property="ename" column="ename"/>
    <result property="job" column="job"/>
    <result property="hiredate" column="hiredate"/>
    <result property="sal" column="sal"/>
    <result property="deptno" column="deptno"/>
    <association property="dept" javaType="Dept" 
           select="getDeptByIdLazy" column="deptno" />
</resultMap>
<select id="getEmpById" parameterType="int" resultMap="empResultMap">
    select * from emp where empno = #{empno}
</select>

 

4.一对多关联

<select id="getEmpByIdLazy" parameterType="int" resultType="Emp">
select * from emp where deptno = #{deptno}
</select>

<resultMap type="Dept" id="deptResultMap">
    <id property="deptno" column="deptno"/>
    <result property="dname" column="dname"/>
    <result property="loc" column="loc"/>
    <collection property="empList" ofType="Emp"
           select="com.neusoft.mapper.EmpMapper.getEmpByIdLazy" column="deptno"/>
</resultMap>
<select id="getDeptById" parameterType="int" resultMap="deptResultMap">
    select * from dept where deptno = #{deptno}
</select>

 

常用

在今天时间之后
now() &lt;= ni.`trade_time_end`

如果这个属性时间大于当前时间为false, 否则为true
(case
when tapp.end_time &gt; NOW() then false
when tapp.end_time &lt; NOW() then true
end
) isExpires

合并这个字段
GROUP_CONCAT(b.court_id) AS courtIdStr

拼接括号里的值
concat(tapp.pool_name, '积分卡') pointsCardName
获取年月日
DATE(a.order_time)

按照首字母排序
CONVERT( name USING gbk ) ASC

 

posted @ 2022-09-06 17:20  不想当将军的好士兵  阅读(48)  评论(0编辑  收藏  举报