IT虾米网

java之MyBatis "or"条件

bluestorm 2023年10月07日 编程语言 225 0

我想用 MyBatis 创建一个查询,它会产生如下内容:

SELECT first_field, second_filed, third_field 
WHERE first_field > 1 AND (second_field > 0 OR third_field < 0) 

我如何使用 Criteria 对象构造它?

请您参考如下方法:

因为 a AND (b OR c)(a AND b) or (a AND c) 相同

TestTableExample example = new TestTableExample(); 
example.createCriteria() 
  .andField1GreaterThan(1) 
  .andField2GreaterThan(0); 
example.or(example.createCriteria() 
  .andField1GreaterThan(1) 
  .andField3LessThan(0)); 

然后坐下来让 SQL 优化器解决。


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!