码迷,mamicode.com
首页 > 数据库 > 详细

<MyBatis>入门六 动态sql

时间:2018-12-14 14:57:02      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:ace   upd   nbsp   get   字符   port   mapper   ati   util   

package org.maple.mapper;

import org.apache.ibatis.annotations.Param;
import org.maple.pojo.Employee;

import java.util.List;
import java.util.Map;

/**
 * @author mapleins
 * @Date 2018-12-13 17:39
 * @Desc 动态sql的接口
 **/
public interface EmployeeMapperDynamicSQL {

    /**
     * 测试if和where
     */
    List<Employee> getEmpByConditionIf(Employee employee);

    /**
     * 测试trim
     */
    List<Employee> getEmpByConditionTrim(Employee employee);

    /**
     * 测试choose
     */
    List<Employee> getEmpByConditionChoose(Employee employee);

    /**
     * 测试set
     */
    void updateEmp(Employee employee);

    /**
     * 测试foreach循环list
     */
    List<Employee> getEmpByConditionForeachList(@Param("ids") List ids);

    /**
     * 测试foreach循环map
     */
    List<Employee> getEmpByConditionForeachMap(@Param("map") Map map);

    /**
     * 测试foreach插入数据
     */
    Integer addEmps(@Param("emps") List<Employee> emps);
}
<?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="org.maple.mapper.EmployeeMapperDynamicSQL">

    <!-- =================================sql片段的使用====================================== -->
    <sql id="selectEmp">
        SELECT id,last_name name,gender,email from tbl_employee
    </sql>

    <!-- =================================if标签的使用====================================== -->
    <!--if test="" 进行判断,如果为true,则拼装,否则不拼装-->
    <!--where 后可以跟 1=1 -->
    <!-- =================================where标签的使用====================================== -->
    <!--3.ognl
    1.where 1=1
    2.使用where标签,只会去掉第一个多出来的and或者or
    -->
    <!-- 查询员工,要求:携带了哪个字段查询条件就带上这个字段的值 -->
    <select id="getEmpByConditionIf" resultType="org.maple.pojo.Employee">
        <include refid="selectEmp"/>
        <where>
            <if test="id!=null">
                id =#{id}
            </if>
            <if test="name!=null and name!=&quot;&quot;">
                and last_name like concat(‘%‘,#{name},‘%‘)
            </if>
            <if test="gender==0 or gender == 1">
                and gender = #{gender}
            </if>
            <if test="email!=null and email!=&quot;&quot;">
                and email = #{email}
            </if>
        </where>
    </select>

    <!-- =================================trim标签的使用====================================== -->
    <!--4.trim标签
    prefix="" 给trim标签拼串后的结果 最前面加一个字符
    prefixOverrides=""  去掉整个字符串前面多余的字符
    suffix="" 给trim标签拼串后的结果 最后面加一个字符
    prefixOverrides="" 去掉整个字符串最后面多余的字符
    -->
    <select id="getEmpByConditionTrim" resultType="org.maple.pojo.Employee">
        SELECT id,last_name name,gender,email from tbl_employee
        <trim prefix="where" prefixOverrides="" suffixOverrides="and">
            <if test="id!=null">
                id =#{id} and
            </if>
            <if test="name!=null and name!=&quot;&quot;">
                last_name like concat(‘%‘,#{name},‘%‘) and
            </if>
            <if test="gender==0 or gender == 1">
                gender = #{gender} and
            </if>
            <if test="email!=null and email!=&quot;&quot;">
                email = #{email}
            </if>
        </trim>
    </select>

    <!-- ==============================choose when,otherwise标签的使用====================================== -->
    <!--如果带了id,就用id查,如果带了name,就用name查,只会进入其中一个-->
    <select id="getEmpByConditionChoose" resultType="org.maple.pojo.Employee">
        SELECT id,last_name name,gender,email from tbl_employee
        <where>
            <choose>
                <when test="id!=null">
                    id = #{id}
                </when>
                <when test="name!=null and name!=&quot;&quot;">
                    last_name like concat(‘%‘,#{name},‘%‘)
                </when>
                <when test="email!=null and email!=&quot;&quot;">
                    email = #{email}
                </when>
                <otherwise>
                    gender = 0;
                </otherwise>
            </choose>
        </where>
    </select>

    <!-- =============================set标签的使用====================================== -->
    <!--会把多余的逗号去掉,也可以使用trim来做-->
    <update id="updateEmp">
        update tbl_employee
        <set>
            <if test="name!=null and name!=&quot;&quot;">
                last_name = #{name},
            </if>
            <if test="gender==0 or gender == 1">
                gender = #{gender},
            </if>
            <if test="email!=null and email!=&quot;&quot;">
                email = #{email},
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <!-- =============================foreach标签的使用====================================== -->
    <!--list的遍历 item是当前值,index是list的索引-->
    <select id="getEmpByConditionForeachList" resultType="org.maple.pojo.Employee">
        SELECT id,last_name name,gender,email
        FROM tbl_employee
        WHERE id
        <foreach collection="ids" item="id" open="in (" close=")" separator="," index="i">
            #{id}
        </foreach>
    </select>

    <!--map的遍历 index是map的key,item是map的值-->
    <select id="getEmpByConditionForeachMap" resultType="org.maple.pojo.Employee">
        SELECT id,last_name name,gender,email
        FROM tbl_employee
        WHERE id
        <foreach collection="map" item="id" open="in (" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <!--foreach 的批量插入-->
    <insert id="addEmps">
        INSERT INTO tbl_employee(last_name, gender, email)
        VALUES
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.name},#{emp.gender},#{emp.email})
        </foreach>
    </insert>

</mapper>

 

<MyBatis>入门六 动态sql

标签:ace   upd   nbsp   get   字符   port   mapper   ati   util   

原文地址:https://www.cnblogs.com/mapleins/p/10119139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!