MyBatis – Create your custom Example class

Create own Example class with MyBatis

Sometimes it happens you need to extend the Example class generated with myBatis with some a specific criteria requirement.

For example, with a query like this:

the PersonExample model may not be enough, due to the criteria related to tableCar at query last line. How could we write a custom criteria ?
There are severaly ways to do this, however our primary goal is to keep untouched the generated example/critera classes.

The solution comes with extending the PersonExample and Criteria class, in order to achieve an elegant solution like this:


Here’s the hint code to write the PersonExtendedCriteria class:

and PersonExampleCustom class: