Mybatis Generator: What's the best way to separate out “auto generated” and “hand edited files”

前端 未结 4 974
终归单人心
终归单人心 2020-12-22 23:32

I am on a project that uses both Mybatis (for persisting java to database) and Mybatis Generator (to automatically generate the mapper xml files and java i

4条回答
  •  悲哀的现实
    2020-12-22 23:46

    I have give a working answer but it's complex and not easy to understand due to the huge configurations.

    Now I have found a better and more concise and easy answer.
    I'm inspired by Emacarron's post: Fix #35

    I have use mbg and in generatorConfig.xml I put to generate java interface and xml map configuration on folder mapper.

    so in my example on mapper folder I have:

    • AnagraficaMapper.java
    • AnafigraficaMapper.xml

    in model folder I have

    • Anagrafica.java
    • AnagraficaKey.java
    • AnagraficaExample.java

    The first two are object model and extend they is trivial.
    To extends mapper I simply copy and empty
    AnagraficaMapper.java --> AnagraficaExMapper.java
    AnagraficaMapper.xml --> AnagraficaExMapper.xml
    in this two new file I put my new code.
    Making an example I decide to add a new sql selectByPrimaryKeyMy

    public interface AnagraficaExMapper extends AnagraficaMapper {
        Anagrafica selectByPrimaryKeyMy(AnagraficaKey key);
    }  
    

    This is my interface extending mgb generated AnagraficaMapper interface.
    in AnagraficaExMapper.xml

    
    
    
      
      
    

    How can you see the namespace is ...AnagraficaExMapper pointing to the new extending interface.

    By the solution on Fix #35 when MyBatis searched for code in AnagraficaExMapper.java and found selectByPrimaryKeyMy method, this was founded in AnagraficaExMapper.xml too;

    but when searched for hierarchic method like selectByPrimaryKey this was not found in AnagraficaExMapper.xml but thank to Fix #35 the code was searched on parent name too, binding all extended interfeces method in the old AnagraficaMapper.xml

    To include fragment included in old xml file you have to use a full path to old xml file like in:

    Now you can simply have to configure MyBatis for automatic mapper scan and all interfaces where correctly bounded to xml mapper.
    when you use mbg to feel db change the interface was regenerate but new extending interface are not override so your code is save.

    regards

提交回复
热议问题