MybatisGenerator可以很方便地生成dao层代码,一般都是生成xml文件,但MybatisGenerator也可以生成注解,但是有一点不好的是生成的@Results注解没有id属性(图1),这个id属性类似于xml文件里的ResultMap,下面来教大家怎么给生成的@Results注解加上id属性。
![图1](https://i-blog.csdnimg.cn/blog_migrate/6bd535a5a4e39a6c9d9d0a3ae9760836.png)
定义一个MybatisGenerator的插件,重写clientSelectByPrimaryKeyMethodGenerated方法即可
public class AddResultMapIdPlugin extends PluginAdapter {
@Override
public boolean validate(List<String> warnings) {
return true;
}
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
List<String> annotations = method.getAnnotations();
Integer index = annotations.indexOf("@Results({");
annotat