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:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax