hbwf / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

New Fluent Criteria API #538

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
3.0.6

I want to submit a proposal for a new Criteria API.

The classes generated with mybatis-generator-core-1.3.1 are in some cases 
limited since it is impossible to write some conditions like (a=1 OR b=2) AND 
(c=3 OR d=4)
You need to write (a=1 AND c=3) OR (a=1 AND d=4) OR (b=2 AND c=3) OR (b=2 AND 
d=4)

The attached API uses fluent interfaces intensivly, so you can chained 
conditions in a simple and effective way.

// Example: a field is equals to a value
mapper.countByExample(criteria.getIdUser().isEquals(1));

// Example: a field is lesser than a value
mapper.countByExample(criteria.getIdUser().isLesser(3));

// Example: a field is in a list of values
mapper.countByExample(criteria.getIdUser().isIn(Arrays.asList(1, 2, 3)));

// Example: a complexe condition
mapper.selectByExample(criteria
        .getIdUser()
        .isIn(Arrays.asList(1, 2, 3))
.or(criteria.getIdUser().isEquals(4)                .and(criteria.getIdUser().isNotEquals(44
4))));

//Example: working with date, if needed, use TO_CHAR
mapper.selectByExample(criteria.getBirthday().toChar("YYYY-DD-MM").isGreater("19
83-01-14"));

Since the classes generated with the generator are pojos, the only classes to 
modify are <bean name>BeanCriteria, and the method applyWhere in the <bean 
name>BeanSqlProvider

Are you interested in such evolution ? 
Do you want me to invastigate more on the subject ?
Can you provide me some help on the generator, I don't understand well how to 
customise / extends / rewrite the classes.

Original issue reported on code.google.com by flumi...@gmail.com on 5 Mar 2012 at 4:29

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I think this is an interesting idea.  The current implementation of the example 
class works the way it does because of limitations in the dynamic XML tags.  It 
is somewhat better in MyBatis3 over iBatis2, but I tried to keep a consistent 
API between the two.

There is certainly an opportunity with MyBatis3 to make things better.  I do 
think that what you describe is probably only possible with annotations/sql 
providers, but it is worth looking into.

I'll spend some time thinking about this.  Let me know your progress and feel 
free to ask questions on the user list.

Original comment by jeffgbut...@gmail.com on 9 Mar 2012 at 6:58

GoogleCodeExporter commented 9 years ago
Effectivly, the solution presented dosen't work with XML and only work with 
annotations and sql providers.

I know that you have to keep a consistant API. Perhaps, this new API could be 
an option in the configuration file of the generator. So users will be able to 
use the current API or the new one.

Original comment by flumi...@gmail.com on 9 Mar 2012 at 7:16

GoogleCodeExporter commented 9 years ago
Enclosed a proposal patch for an implementation for the fluent API.

I introduced a new type for the javaClientGenerator tag : 
ANNOTATEDMAPPER_FLUENT.
Not sure this is a really good name, feel free to give me a new name. My 
english is poor, and I'm not good in finding new names.

If you are interested in this API, I could work on the XML version of the API.

Do not hesitate to make feedbaks.

Original comment by flumi...@gmail.com on 14 Jan 2013 at 3:00

Attachments: