Mybatis的动态SQL~

MyBatis有一个强大特性就是它的动态SQL。在实际项目开发中,经常需要根据不同条件拼接SQL语句,拼接时还要确保不能忘了必要的空格,有时候还要注意省掉列名列表最后的逗号...等等。在使用JDBC 或其他类似持久层框架操作数据库时,处理这种情况是非常麻烦的,甚至可以用痛苦来形容,而在MyBatis中利用动态SQL这一特性可以很简单地解决这个问题。

动态SQL元素和使用JSTL或其他类似基于XML的文本处理器相似,MyBatis采用功能强大的基于OGNL的表达式来完成动态SQL。OGNL 的表达式可以被用在任意的SQL 映射语句中。


如上,假设有如上一张表,我们想查询如下条件:

  • status=1
  • 公司名包含【华为】
  • 品牌名包含【华为】

则在映射文件中书写如下sql语句:

XML 复制代码
    <select id="selectByCondition" resultMap="brandResultMap">
        select * from tb_brand where status=#{status} and company_name like #{companyName}
        and brand_name like #{brandName}
    </select>

接口中的方法名:

java 复制代码
    List<Brand> selectByCondition(@Param("status") int status,
                                  @Param("companyName")String companyName,
                                  @Param("brandName")String brandName);

测试类如下:

java 复制代码
public void testselectByCondition() throws IOException {

        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%"+companyName+"%";
        brandName="%"+brandName+"%";
//        1.获取SqlSessionFactory
        String resource ="mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//        2.获取SqlSession对象
        SqlSession sqlSession= sqlSessionFactory.openSession();
//        3.获取Mapper接口的代理对象
        BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
//        4.执行方法
        List<Brand> brands=brandMapper.selectByCondition(status,companyName,brandName);
        System.out.println(brands);
//        5.释放资源
        sqlSession.close();
    }

查询结果:

但是在实际场景中,可能并不需要同时查询3个条件,这样的话,参数值无法传入给sql中的占位符,后台会发生语法错误~

此时用到Mybatis中的动态sql技术:

修改sql语句如下:

sql 复制代码
    <select id="selectByCondition" resultMap="brandResultMap">
        select * from tb_brand where
                <if test="status!=null">
                    status=#{status}
                </if>
                <if test="company_name!=null and company_name!=''">
                and company_name like #{companyName}
                </if>

                <if test="brand_name!=null and brand_name!=''">
                and brand_name like #{brandName}
                </if>

    </select>

如上,当有条件不存在时,会直接省略对该条件的判断。

sql 复制代码
    <select id="selectByCondition" resultMap="brandResultMap">
        select * from tb_brand
                <where>
                    <if test="status!=null">
                        status=#{status}
                    </if>
                    <if test="companyName!=null and companyName!=''">
                        and company_name like #{companyName}
                    </if>

                    <if test="brandName!=null and brandName!=''">
                        and brand_name like #{brandName}
                    </if>
                </where>


    </select>

但是还存在bug,当第一个条件不存在时会直接导致关键字where和and相连接导致语法错误,将where标签嵌到外面即可解决~

相关推荐
万法若空8 小时前
【算法-查找】查找算法
java·数据结构·算法
阿成学长_Cain8 小时前
Linux ipcs 命令超全详解:查看共享内存 / 消息队列 / 信号量,IPC 运维必备
linux·运维·服务器·网络·数据库
HZZD_HZZD10 小时前
DL/T 645-2026新国标深度解读与智能电表协议适配实战:从帧结构变化到Java采集器升级的全链路改造方案
java·开发语言
OceanBase数据库官方博客10 小时前
OceanBase AI 时代,数据库的变与不变(技术解析与实践)
数据库·人工智能·oceanbase
阿成学长_Cain12 小时前
Linux dirs命令详解|Bash目录堆栈管理快速切换目录实战教程
linux·运维·前端·数据库
ywl47081208712 小时前
Mysql 平衡二叉树、红黑树、B树、B+树区别以及应用场景(五)
数据库·b树·mysql
山登绝顶我为峰 3(^v^)312 小时前
C/C++ 交叉编译方法
java·c语言·c++
IvorySQL12 小时前
IvorySQL Agent 探索与实践
数据库·人工智能·postgresql·oracle·ivorysql
一叶飘零_sweeeet13 小时前
Codex 与 Claude Code 深度拆解:两代 AI 编程智能体的技术本质与 Java 实战指南
java·ai·codex·claude code