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?
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With