【MyBatis】四、MyBatis中的动态SQL标签

动态SQL

动态SQL语句是动态的拼接Mybatis中SQL语句的情况,可以动态的在Mybatis中使用SQL

if语句

if语句的xml文件:

xml 复制代码
<!--    List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="Emp">
        select * from t_emp where 1 = 1
        <if test="empName != null and empName != ''">
            emp_name = #{empName}
        </if>
        <if test="sex != null and sex != ''">
            and sex = #{sex}
        </if>
        <if test="age != null and age != ''">
            and age = #{age}
        </if>
        <if test="email != null and email != ''">
            and email = #{email}
        </if>
    </select>

传入对象来进行调用:

java 复制代码
    @Test
    public void test() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> empList = mapper.getEmpByCondition(new Emp(null, "", 23, "男", "123@qq.com", null));
        System.out.println(empList);
    }

WHERE语句

where标签中的and会被自动去掉,并且若没有合适的内容,则不会添加where关键字

注意:where标签只能去掉条件前的and、五福去掉条件后的and

xml 复制代码
    <select id="getEmpByCondition" resultType="Emp">
        select * from t_emp
        <where>
            <if test="empName != null and empName != ''">
                emp_name = #{empName}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="age != null and age != ''">
                and age = #{age}
            </if>
            <if test="email != null and email != ''">
                and email = #{email}
            </if>
        </where>
    </select>

trim标签

trim标签会在其内容中没有合适的内容时不添加其内容,若trim标签内有内容时,则会添加这个内容

xml 复制代码
<!--    如果trim中的if有一个成立,就会把where添加进去,如果if语句后没有语句,则会将末尾的and或or去掉-->
    <select id="getEmpByCondition" resultType="Emp">
        select * from t_emp
        <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName != ''">
                emp_name = #{empName} and
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex} or
            </if>
            <if test="age != null and age != ''">
                age = #{age} and
            </if>
            <if test="email != null and email != ''">
                email = #{email}
            </if>
        </trim>
    </select>

choose、when、otherwise

choose是when和otherwise的父标签,when和otherwise要写在choose中,相当于编程语言中的if...else...if...else...

xml 复制代码
<!--    List<Emp> getEmpByChoose(Emp emp);-->
    <select id="getEmpByChoose" resultType="Emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName != null and empName != ''">
                    emp_name = #{empName}
                </when>
                 <when test="sex != null and sex != ''">
                     sex = #{sex}
                 </when>
                <otherwise>
                    did = 1
                </otherwise>
            </choose>
        </where>
    </select>

注意这里when至少有一个、otherwise至多有一个,也可以没有

foreach

通过数组实现批量删除

mapper接口:

java 复制代码
    /**
     * 通过数组实现批量删除(形参传入数组)
     */
    int deleteMoreByArray(@Param("eids") Integer[] eids);

xml:

xml 复制代码
<!--    int deleteMoreByArray(Integer[] eids);-->
<!--    foreach标签中,collection代表要遍历的数组名,item代表每次遍历得到的数据,separator代表每个数据之间的分隔,open
代表遍历前添加,close代表在遍历之后添加-->
    <delete id="deleteMoreByArray">
        DELETE from t_emp where eid in
            <foreach collection="eids" item="eid" separator="," open="(" close=")">
                #{eid}
            </foreach>
    </delete>

xml的第二种写法:

xml 复制代码
    <delete id="deleteMoreByArray">
        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        </foreach>
    </delete>

通过集合实现批量添加

建立接口:

java 复制代码
    /**
     * 通过list实现批量添加
     */
    int insertMoreByList(@Param("emps") List<Emp> list);

建立xml:

xml 复制代码
<!--    int insertMoreByList(List<Emp> list);-->
    <insert id="insertMoreByList">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null, #{emp.empName}, #{emp.age}, #{emp.sex}, #{emp.email}, null)
        </foreach>
    </insert>

sql标签

sql标签用来将常用的一些字段记录,下次在有这些字段出现时可以简易的替换这些字段:

使用include标签来引用定义的sql片段

xml 复制代码
    <sql id="empColumn">eid, emp_name, age, sex, email</sql>
    
    <select id="getEmpByConditionOne" resultType="Emp">
        select <include refid="empColumn"></include> from t_emp
    </select>
相关推荐
农民小飞侠1 小时前
python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能
开发语言·python
战神刘玉栋1 小时前
《程序猿之设计模式实战 · 观察者模式》
python·观察者模式·设计模式
敲代码不忘补水1 小时前
Python 项目实践:简单的计算器
开发语言·python·json·项目实践
hayhead1 小时前
高频 SQL 50 题(基础版)| 626. 换座位
sql·力扣
鸽芷咕2 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
子午3 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
执键行天涯3 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
风等雨归期3 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
Darling_003 小时前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode
Adolf_19933 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask