• Mybatis--动态sql


    XML映射文件(简单的SQL用注解,复杂的用xml)

    规范:
    XML映射文件的名称和Mapper接口名称一样(同包同名)注意:不能直接用.创建文件夹,用/分层
    xml映射文件的namespace属性为mapper接口全限定名一致
    xml映射文件的sql语句和mapper接口的方法名一致,并保持返回类型一致

    xml约束
    
    DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    • 1
    • 2
    • 3
    • 4
    示例
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.mapper.EmpMapper">
        <select id="select" resultType="com.example.demo.pojo.Emp">
    --         resultType 单条记录封装的类型
            select * from emp where username like concat('%',#{name},'%') and gender = #{gender} order by create_time desc
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    namespace

    在这里插入图片描述

    MybatisX 插件

    快速定位 方便

    ctrl+ALT+L SQL语句格式化(好用)

    Mybatis 动态SQL

    动态SQL :if 标签
    示例1

    查找的时候没输入条件则全部查找

    select * from emp where username like concat('%',#{name},'%') and gender = #{gender} order by create_time desc
    
    • 1

    where标签可以自动去除sql中的 and where

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.mapper.EmpMapper">
        <select id="select" resultType="com.example.demo.pojo.Emp">
    --         resultType 单条记录封装的类型
            select * from emp
            <where>
                <if test="name != null and name!= ''">
                    username like concat('%',#{name},'%')
                if>
                <if test="gender != null">
                    and gender = #{gender}
                if>
            where>
            order by create_time desc
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    示例2

    更新时候如果没传值会变成null -->有值更新无值不更新
    set标签 能去掉sql里面的,

    update emp set username =#{username},image = #{image},update_time =#{updateTime} where id =#{id}
    
    • 1
      <update id="update">
            update emp
            <set>
                <if test="username != null">
                    username=#{username},
                if>
                <if test="image != null">
                    image = #{image},
                if>
                <if test="updateTime != null">
                    update_time =#{updateTime}
                if>
            set>
            where id = #{id}
        update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    动态SQL :foreach 标签

    批量删除

    delete from emp where id in(9,10,11);
    
    • 1
     <delete id="deleteByIds">
            delete
            from emp
            where id in
            
            <foreach collection="ids" item="id" separator="," open="(" close=")">
                #{id}
            foreach>
        delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    重复的sql片段
    <sql id="xxx">
    sql>
    <include refid="xxx">
    include>
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    [C++] - GCC和LLVM对方法 warning: non-void function does not return a value [-Wreturn-type] 的处理差异
    SpringCloud(15)之SpringCloud Gateway
    openssl3.2 - note - Writing OpenSSL Provider Skeleton
    二叉树根节点到叶子节点的所有路径和
    设计模式案例
    【高等数学重点题型篇】——不定积分
    数据科学家的经验,python中dataframe的最常用设置!
    Codeforces Round #815 (Div. 2)
    C生万物之函数
    Bootstrap5简单实现登录页:上下左右居中
  • 原文地址:https://blog.csdn.net/qq_44761778/article/details/133808958