栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis动态SQL多条件查询1 - if 标签

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis动态SQL多条件查询1 - if 标签

Mybatis框架的动态SQL技术是一种根据特定条件动态拼接SQL语句的过程。它的存在是为了解决拼接SQL语句字符串的痛点问题。创建新的Mapper接口和新的mapper映射文件。

目录

1.创建DynamicSqlMapper接口

2.创建DynamicSqlMapper映射文件

3.测试类

4.测试结果

5. 引伸 : 其中一样传入信息不符合

5.1 and后的参数不提供

 5.1.1运行效果

5.2 where后的参数不提供

5.2.1 运行效果

5.2.2 解决方法

6. 总结


1.创建DynamicSqlMapper接口
package com.mybatis.mapper;

import com.mybatis.pojo.Emp;

import java.util.List;


public interface DynamicSqlMapper {
    
    List getEmpByCondition(Emp emp);
}

2.创建DynamicSqlMapper映射文件

if test标签后不需要写原来的${}或者#{} 




    
    
        select * from t_emp where
        
            emp_name = #{empName}
        
        
            and age = #{age}
        
        
            and sex = #{sex}
        
        
            and email = #{email}
        
    

3.测试类
    @Test
    public void testGetEmpByCondition(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSqlMapper mapper = sqlSession.getMapper(DynamicSqlMapper.class);
        List emps = mapper.getEmpByCondition(new Emp(null, "张三", 32, "男", "123@qq.com"));
        System.out.println(emps);
    }

4.测试结果
11:08:49:276 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 1234250905.
11:08:49:292 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==>  Preparing: select * from t_emp where emp_name = ? and age = ? and sex = ? and email = ?
11:08:49:338 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 张三(String), 32(Integer), 男(String), 123@qq.com(String)
11:08:49:390 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <==      Total: 1
[Emp{eid=1, empName='null', age=32, sex='男', email='123@qq.com', dept=null}]

5. 引伸 : 其中一样传入信息不符合

5.1 and后的参数不提供

and后的参数是只, 如上图mapper映射文件中, age, sex, email

我们将age和email都设置成null, 在运行一次,看看效果。

        List emps = mapper.getEmpByCondition(new Emp(null, "张三", null, "男", null));

 5.1.1运行效果
11:22:57:465 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 769530879.
11:22:57:496 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==>  Preparing: select * from t_emp where emp_name = ? and sex = ?
11:22:57:622 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 张三(String), 男(String)
11:22:57:763 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <==      Total: 1
[Emp{eid=1, empName='null', age=32, sex='男', email='123@qq.com', dept=null}]

总结: and后的参数不提供, 不会影响sql运行,而且我们从LOG中可以看出,and后设置成null的参数,就不会参与sql运行。

5.2 where后的参数不提供
        List emps = mapper.getEmpByCondition(new Emp(null, null, 32, "男", "123@qq.com"));

5.2.1 运行效果

运行错误,我们可以从log里看出, where后跟的是and,那么sql语法肯定是错误的。

1:25:12:513 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 769530879.
11:25:12:513 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==>  Preparing: select * from t_emp where and age = ? and sex = ? and email = ?
11:25:12:670 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 32(Integer), 男(String), 123@qq.com(String)

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and age = 32
         
         
            and sex = '男'
         
         ' at line 4

5.2.2 解决方法

在mapper映射文件中where语句后添加恒成立的条件 1=1, 映射文件如下。


        select * from t_emp where 1=1
        
            emp_name = #{empName}
        
        
            and age = #{age}
        
        
            and sex = #{sex}
        
        
            and email = #{email}
        
    

 运行结果: 成功运行且不报错。我们可以从LOG看出,恒成立条件1=1之后拼接and就不会报错。

11:32:01:649 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 769530879.
11:32:01:649 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==>  Preparing: select * from t_emp where 1=1 and age = ? and sex = ? and email = ?
11:32:01:727 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 32(Integer), 男(String), 123@qq.com(String)
11:32:01:806 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <==      Total: 1
[Emp{eid=1, empName='null', age=32, sex='男', email='123@qq.com', dept=null}]

6. 总结

恒成立1=1的作用:

1. where后跟着的参数是null的时候方便拼接sql

2.当参数都是null时候,sql也可以成功运行。

动态SQL中:

if 标签根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到sql中去

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866906.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号