4.Mybatis学习-动态sql、缓存、逆向工程

动态sql

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语 句字符串时的痛点问题。

if标签

DynamicSQLMapper.java

java 复制代码
package com.light.mybatis.mappers;
import com.light.mybatis.pojo.User;
import java.util.List;

public interface DynamicSQLMapper {
    /**
     * 这里返回值有可能是多个,也可能是一个。所以返回值是List<User>
     * @return
     */
    List<User> getUserListConditionForIf(User user);
}

DynamicSQLMapper.xml

xml 复制代码
<?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="com.light.mybatis.mappers.DynamicSQLMapper">

    <select id="getUserListConditionForIf" resultType="user">
        <!-- 1 = 1 为了防止第一个if不成立,sql会拼成 ... where and ...-->
        select * from user where 1 = 1
        <!--
            test 里面的account变量来自于调用mapper方法参数传过来的对象,只不过没有加#{}
            test 中不支持 && ||, 但是可以是用and, or
        -->
        <if test="account != null and account != ''">
            <!--拼接sql-->
            and account = #{account}
        </if>
        <if test="age > 5">
            and age = #{age}
        </if>
    </select>
</mapper>

生成sql:select * from user WHERE account = ? and age = ?

where标签

where 元素只会在子元素返回任何内容的情况下才插入 "WHERE" 子句。而且,若子句的开头 为 "AND" 或 "OR",where 元素也会将它们去除。

xml 复制代码
<select id="getUserListConditionForIf" resultType="user">
    select * from user
    <!--自动加上where-->
    <where>
        <if test="account != null and account != ''">
            <!--
            where标签里可以去掉子句开头的and或者or,所以使用where标签and和or通常写在开头,
            这样写account = #{account} and,如果后面的条件不成立,就会多出来个and,where标签也不会去掉,会报错。
            -->
            and account = #{account}
        </if>
        <if test="age > 5">
            and age = #{age}
        </if>
    </where>
</select>

生成sql:select * from user WHERE account = ? and age = ?

trim标签

若标签中有内容时:

  • prefix|suffix:将trim标签中内容前面或后面添加指定内容 , |隔开可以添加多个内容
  • suffixoverrides|prefixoverrides:将trim标签中内容前面或后面去掉指定内容 , |隔开可以添加多个内容
  • 标签中没有内容时,trim标签也没有任何效果
xml 复制代码
<select id="getUserListConditionForIf" resultType="user">
    select * from user
    <trim prefix="where" suffixOverrides="and|or">
        <if test="account != null and account != ''">
            account = #{account}
        </if>
        <if test="age > 5">
            and age = #{age} and
        </if>
    </trim>
</select>

choose, when, otherwise标签

choose,when,otherwise相当于if...else if...else。和if的区别就是只会执行一个when或者otherwise里面的内容。if标签都要执行,所有if需要写and或者or。而when...otherwise不需要写。

  • choose就是一个父标签,表示一套when,otherwise集合
  • when 表示if...else if...
  • otherwise 表示else...
xml 复制代码
<select id="getUserListConditionForIf" resultType="user">
    select * from user
    <where>
        <choose>
            <when test="account != null and account != ''">
                account = #{account}
            </when>
            <when test="name != null and name != ''">
                name = #{name}
            </when>
            <otherwise>
                id = 1
            </otherwise>
        </choose>
    </where>
</select>

生成sql:select * from user WHERE account = ?

foreach标签

xml 复制代码
<delete id="deleteMoreUser">
    delete from user where id in
    <!--
        foreach标签
        collection: 集合
        item: 集合中每一个元素
        separator: 每一个#{id}之间的分隔符
        open:以什么开始
        close:以什么结束
        下面foreach 出来的结果就是(1,2,3)
    -->
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</delete>
<delete id="deleteMoreUser1">
    delete from user where
    <!-- 结果为 id=1 or id=2 or id=3 -->
    <foreach collection="ids" item="id" separator="or">
        id=#{id}
    </foreach>
</delete>

sql标签

设置SQL片段: <sql id="empcolumns">id,emp_name,age,sex,email</sql>

引用SQL片段: <include refid="empcolumns"></include>

xml 复制代码
<sql id="usercolumns">id,emp_name,age,sex,email</sql>
<select id="getUserListConditionForIf" resultType="user">
    select <include refid="usercolumns"></include> from user
    <!--自动加上where-->
    <where>
        <choose>
            <when test="account != null and account != ''">
                account = #{account}
            </when>
            <when test="name != null and name != ''">
                name = #{name}
            </when>
            <otherwise>
                id = 1
            </otherwise>
        </choose>
    </where>
</select>
相关推荐
毕业设计7031 天前
(免费领源码) SpringBoot 游戏交易平台17600-java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
java·spring boot·mysql·决策树·mybatis·idea·推荐算法
程序员JerrySUN2 天前
Jetson Edge AI 实战01:Nano、Xavier、Orin 怎么选?Jetson 硬件选型详解【视频讲解】
java·数据库·redis·安全·mybatis
linmengmeng_13142 天前
【总结】MyBatis-Plus批量插入与清表的正确姿势
java·spring boot·mysql·mybatis
vx_BS813302 天前
【项目编号:project51381】Spring Boot 婚纱摄影管理系统:套餐预约、在线咨询、支付与客片展示的一站式业务实现
java·spring boot·eclipse·tomcat·mybatis
jaysee-sjc3 天前
【苍穹外卖】Day01:从零认识企业级项目开发
java·开发语言·数据库·mysql·spring·intellij-idea·mybatis
程序猿乐锅3 天前
【黑马点评 | 第二篇】Redis 缓存更新策略与商铺缓存实现
java·网络·redis·spring·mybatis
萧瑟余晖3 天前
MyBatis 核心原理详解
架构·mybatis
MZA6663 天前
实战:基于 XXL-JOB + 策略模式 + Nacos 配置化的 CSV 导出任务系统设计
java·spring boot·mybatis
干到60岁退休的码农6 天前
15.创建获取用户接口与JWT认证器
java·spring boot·mybatis