Mybatisplus的or语句写法

在开发的时候,我们发现Mybatis-plus想实现or语句的写法的时候总对不上位置?
  • 比如

select * from student where id = ? and age = ? and (age > ? or mark > ?);

这种sql语句我们怎么都写不成?(T . T)

只需按照以下的Mybatis-plus格式来写,你就可以拥有我们想要的sql语句!
LambdaQueryWrapper<T> wrapper = getInitQueryWrapper()
        .eq(Student::getStudentId, student.getId())
        .eq(Student::getRegisterTime, registerTime)
        .and(o -> o.gt(Student::getAge, BaseConstant.TEN).or().gt(Student::getMark, BaseConstant.TEN));

错误示范

QueryWrapper<User> userWrapper = new QueryWrapper<User>();
userWrapper.eq("name", name);
userWrapper.eq("pwd", pwd).or().eq("phone", phone);

这种写法最后的sql语句是

select * from student where id = ? and age = ? and age > ? or mark > ?;

### MyBatisPlus禁用SQL打印的配置方法 在开发过程中,如果希望关闭MyBatisPlus的日志输出功能,则可以通过调整日志级别来实现这一目标。以下是具体的解决方案: #### 方法一:通过Spring Boot配置文件设置日志级别 可以在`application.properties`或`application.yml`中修改日志级别的配置,从而控制MyBatisPlus的日志行为。 对于`application.properties`文件: ```properties logging.level.com.baomidou.mybatisplus=ERROR ``` 对于`application.yml`文件: ```yaml logging: level: com.baomidou.mybatisplus: ERROR ``` 上述配置会将MyBatisPlus相关的日志级别设为`ERROR`,这意味着只有错误信息会被记录下来,而普通的SQL查询语句则不会被打印到控制台[^1]。 #### 方法二:自定义LoggerInterceptor拦截器 如果需要更精细地管理SQL日志输出,可以创建一个自定义的拦截器类并覆盖默认的行为。例如,在项目中添加如下代码片段即可完成此操作: ```java import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Invocation; public class CustomSqlLogInterceptor implements InnerInterceptor, Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 不做任何处理直接返回结果 return invocation.proceed(); } @Override public void beforeStatementExecute(StatementHandler var1) {} @Override public boolean handlerException(Throwable throwable) {return false;} } ``` 接着注册该拦截器至MyBatis Plus插件链路之中[^2]。 #### 方法三:全局配置方式(适用于非Spring环境) 当不处于Spring框架下运行时,可通过编程手段动态设定GlobalConfig对象属性达到相同效果: ```java import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.core.config.GlobalConfig; // 创建新的全局配置实例 GlobalConfig globalConfig = new GlobalConfig(); // 获取当前使用的MyBatsis Configuration 对象 MybatisConfiguration configuration = (MybatisConfiguration)sqlSessionFactory.getConfiguration(); // 关闭调试模式下的SQL显示开关 configuration.setLogEnabled(false); globalConfig.setDbConfig(new DbConfig().setMetaObjectHandler(null)); ``` 以上三种途径均能有效抑制不必要的SQL语句输出干扰正常业务逻辑分析过程中的注意力集中度提升效率减少混乱情况发生几率同时提高程序性能表现水平[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值