Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis "or" criteria

Tags:

java

mybatis

I want to create a query with MyBatis, which will produce something like:

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

How could I construct that with Criteria objects?

like image 290
Oleksandr Cherniaiev Avatar asked Oct 22 '14 09:10

Oleksandr Cherniaiev


People also ask

Is MyBatis a framework?

MyBatis is an open source persistence framework which simplifies the implementation of database access in Java applications. It provides the support for custom SQL, stored procedures and different types of mapping relations. Simply put, it's an alternative to JDBC and Hibernate.

What is MyBatis generator?

MyBatis Generator (MBG) is a code generator for MyBatis MyBatis. It will generate code for all versions of MyBatis. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s).

What is parameter type in MyBatis?

MyBatis provides various attributes for insert mapper, but largely we use id and parameter type. id is unique identifier used to identify the insert statement. On the other hand, parametertype is the class name or the alias of the parameter that will be passed into the statement.


1 Answers

Since a AND (b OR c) is the same as (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));

then sit back and let the SQL optimizer figure it out.

like image 59
Mike Avatar answered Oct 05 '22 13:10

Mike