generator icon indicating copy to clipboard operation
generator copied to clipboard

support for two-level model classes generation (abstract base class and stub class)

Open redguy666 opened this issue 11 years ago • 6 comments

I could not find it anywhere if mybatis already supports such scenario or is it possible with plugins... The case is: I want to generate two cvlasses for every model/table, for example for table 'table' I want to have:

abstract class BaseTable { //all mybatis generated stuff goes here } class Table extends BaseTable { //empty }

now - the point is that subsequent generations can overwrite BaseTable class, but final Table class where developers can put some logic must be left intact, mappers should by default return always 'Table' class instances and operate on that level.

is it possible with current mybatis, with some plugins, or with custom code modyfications? Or maybe it could be built-in in mybatis next release? :)

redguy666 avatar Mar 15 '13 17:03 redguy666

This is possible with a plugin - although it might be a somewhat complex one.

You would need to change the types in the mapper methods, and change the types in the generated XML, and generate the classes that extend the base record class.

It's not a bad idea for something to be included as an option (I do something similar with the Example.Criteria inner classes already). I'll look into it - probably easier to do it in the base code than through a plugin.

jeffgbutler avatar Mar 15 '13 18:03 jeffgbutler

is there any progress? Did you manage to look at this feature?

redguy666 avatar Jul 09 '13 08:07 redguy666

Hi,

I think I've done just that if you are still interested.

Look at my plugin CreateSubPackagePlugin: https://github.com/dcendents/mybatis-generator-plugins

It will generate the BaseTable (although I add a suffix so would be TableBase), optionally it its own package. You are left to create the Table class that extands TableBase manually, this way the plugin will not override it next time you run the generator.

Cheers

dcendents avatar May 25 '15 20:05 dcendents

@redguy666 I have a idea, this requires official support and a lot of work. even if two different classes are generated, the next time you regenerate, you need to consider whether to override the original subclasses. If overridden, the custom attributes of subclasses disappear. If not covered, it may not be the result you want. If mybatis provides a method like this: in Mybatis3 targetRuntime:

<T extends Model> List<T> list = modelMapper.selectByExample(modelExample, Class<T> clazz);

in MyBatis3DynamicSql targetRuntime:

    default <T extends Model> QueryExpressionDSL<MyBatis3SelectModelAdapter<List<T>>> selectByExample(Class<T> clazz) {
        return SelectDSL.selectWithMapper(this::selectMany, allColumn)
                .from(ad).withClass(clazz);
    }

so it's not just mapping to a fixed class, it's all the subclasses of that class.

@jeffgbutler What do you think?

pky920216344 avatar Jul 15 '19 03:07 pky920216344

I'm not sure I fully understand your idea. Maybe if you add some more example code.

But I will say that MyBatis Dynamic SQL is not building any objects or doing any introspection - that work is all done by MyBatis itself. So your idea might be something that would need to be added to the core MyBatis library - or implemented through some kind of custom ObjectFactory.

jeffgbutler avatar Jul 16 '19 00:07 jeffgbutler

I've written my thoughts in the mybatis-3 project mybatis-3#1608

pky920216344 avatar Jul 16 '19 11:07 pky920216344

Closing due to no activity. This can be accomplished with a plugin if anyone is still interested.

jeffgbutler avatar Apr 21 '23 20:04 jeffgbutler