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标签嵌到外面即可解决~

相关推荐
程序员小假4 分钟前
线程池执行过程中遇到异常该怎么办?
java·后端
稚辉君.MCA_P8_Java8 分钟前
DeepSeek Java 单例模式详解
java·spring boot·微服务·单例模式·kubernetes
洛_尘16 分钟前
数据结构--4:栈和队列
java·数据结构·算法
pccai-vip21 分钟前
系分论文《论非关系型数据库(NoSQL)在社交媒体内容管理系统中的应用》
数据库·nosql·媒体
疯癫的老码农23 分钟前
【小白入门docker】创建Spring Boot Hello World应用制作Docker镜像并运行
java·spring boot·分布式·docker·微服务
谱写秋天24 分钟前
软考-系统架构设计师 NoSQL数据库详细讲解
数据库·系统架构·软考架构师
观望过往27 分钟前
非关系型数据库(NoSQL):特性、类型与应用指南
数据库·nosql
Mr.Entropy31 分钟前
Hibernate批量查询方法全面解析
java·后端·hibernate
绝顶少年1 小时前
Spring 框架中 RestTemplate 的使用方法
java·后端·spring
小趴菜82271 小时前
安卓人机验证View
android·java·前端