mybatis / generator

A code generator for MyBatis.
http://www.mybatis.org/generator/
5.27k stars 2.52k forks source link

generator need implements java.io.Serializable to suit the project running in distributed system. #72

Closed kk580kk closed 6 years ago

kk580kk commented 8 years ago

We used maven generator and generated XXXXExample class , but this class didn't implements java.io.Serializable, and we got error.

I have edited this java class SerializablePlugin.java and add this code

@Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);

        for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
            if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
                innerClass.addSuperInterface(serializable);
            }
            if ("Criteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
                innerClass.addSuperInterface(serializable);
            }
            if ("Criterion".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
                innerClass.addSuperInterface(serializable);
            }
        }

        return true;
    }

to fix this issues.

And if there a better solution for this problem.

ghost commented 7 years ago

@kk580kk image

I don't know SerializablePlugun specification. But I confirm model class implement java.io.Serializable like this:

image

kk580kk commented 7 years ago

@TroyTae We built micorservices by Dubbo framework , and it need java interface to connect each services. In this situation Example class need implement java.io.Serializable to send by rpc.

Now we used enterprise version of dubbo , named HSF . Same problem and same way to solve it.

jeffgbutler commented 7 years ago

The Example classes are not designed to be Serializable in general. In most cases it works, but there are cases (esp. user defined types and type handlers) where the Examples might not be Serializable.

This is also true of model classes in the case of user defined types.

So I think it is reasonable to leave it as is. It is very easy to write your own plugin (as you have done) in the case where you know the classes will be Serializable.

ghost commented 7 years ago

@kk580kk I agree with jeffbutler... In general case, people make APIs that have response consists of Model class to connect each other services.

kk580kk commented 7 years ago

@jeffgbutler @TroyTae I see ,and I almost agree on this point.

In this situation, I was a little lazy to write a query method and a query class model. And example is just enough to search database on most of query functions. So send a Example class to remote service is really easy way to solve it.

Maybe when I have time I need a refactor //todo