• <MyBatis>入门六 动态sql


    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>
  • 相关阅读:
    分享诗集中国原创诗歌创作分享中心,欢迎博客园喜欢诗词的加入【诗词在线】
    转让上海水族馆票【吐血转让】08年10月有效【100¥】
    winform 里Control.Margin 属性到底是干嘛的?
    亚交联盟注册指南
    sqlserver 替换回车换行
    如何配置 imail 中域名的MX记录
    张良、萧何与韩信:汉初三杰悲情录[转]
    FBD内存全局缓冲内存 比dd2 ecc还要好啊。服务器内存和普通PC内存的区别[转载]
    脆弱的ASP.NET AJAX
    无法连接到服务器。 帐户: 'mail.bb.cn', 服务器: '*******', 协议: SMTP, 端口: 25, 安全(SSL): 否, 套接字错误: 10061, 错误号: 0x800CCC0E
  • 原文地址:https://www.cnblogs.com/mapleins/p/10119139.html
Copyright © 2020-2023  润新知