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 interfaces from a database schema).
Mybatis generator does a good job at generating the files necessary for basic crud operation.
Context
For some of the tables/classes, we will need more "stuff" (code queries, etc) than the "crud stuff" generated by the MyBatis Generator tool.
Is there any way to have "best of both worlds", i.e use auto generation as as well as "custom code". How do you separate out and structure the "hand edited files" and "automatically generated files".
Proposal
I was thinking about the following, i.e. for table "Foo"
Auto-Generated
- FooCrudMapper.xml
- interface FooCrud.java
(where "Crud" stands for "Create Read Update Delete")
Hand Edited
- FooMapper.xml
- interface Foo extends FooCrud
The notion: if the schema changed, you could always safely autogenerate the "Crud" xml and .java files without wiping out any of the custom changes.
Questions
Would mybatis correctly handle this scenario, i.e. would this mapper correctly execute the auto-generated 'crud code'?
FooMapper fooMapper = sqlSession.getMapper(FooMapper.class);
What approach do you recommend?
Edit 1: * Our db design uses a 'core table' ("element") with other tables 'extending' that table and adding extra attributes (shared key) . I've looked at docs and source concluded that I cannot use Mybatis Generator in conjunction with such 'extension' without any hand editing:
i.e. This does not work. -ElementMapper extends "ElementCrudMapper" -FooMapper.xml extends both "ElementCrudMapper" and "FooCrudMapper"
thanks all!
I can seperate out generated files and hand edited files.
I use mybatis-spring and spring to manage dao interfaces. This library allows MyBatis to participate in Spring transactions, takes care of building MyBatis mappers and SqlSessions and inject them into other beans, translates MyBatis exceptions into Spring DataAccessExceptions, and finally, it lets you build your application code free of dependencies on MyBatis, Spring or MyBatis-Spring.
For DAO Interfaces, I write a generic MybatisBaseDao to represent base interface generated by mybatis generator.
Of course, you can custom your
BaseDao
according to your demand. For example we have aUserDao
, Then you can defind it like thisFor mapper xml files, I create two packages in mapper(.xml) base folder to separate generated files and hand edited files. For
UserDao
above, I put UserMapper.xml generated by generator in package named 'generated'. I put all hand writing mapper sqls into another UserMapper.xml file in the package namedmanual
. The two mapper files start with the same header<mapper namespace="com.xxx.dao.UserDao" >
. Mybatis can scan the xml mapper files to map sql and corresponding interface method automatically.For generated entities and example objects I overwrite them directly.
I hope the method above can help you!
I had this task in Spring Boot project and was resolved bellow
In mybatis/*.xml files i changed generated like this
<mapper namespace="news.project.demo.mappers.BrandMapper">
to<mapper namespace="news.project.demo.mappers.extended.BrandMapperExtended">
But all sql logic must be written in xml-files. Interfaces are only declarations without @Select or @ResultMap annotations, so you must configure correctly your mybatis-generator.in application.properties i have
mybatis.mapper-locations=classpath*:mybatis/*.xml mybatis.type-aliases-package=news.project.demo.models
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
<javaClientGenerator type="XMLMAPPER" ...>
to generate java interface and xml map configuration on folder mapper.so in my example on mapper folder I have:
in model folder I have
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
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:
<include refid="net.algoritmica.ciaomondo.mapper.AnagraficaMapper.Base_Column_List" />
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
The Larry.Z solution help me to solve the same problem to separate auto generated from hand edited files. I had a custom folder structure in my project and adapted Larry solution to work in my project and add this answer to help other by use Larry solution adapting it.
The best solution is to add feature to Mybatis generator (MBG) to integrate hand modified xml mapper. MBG had to be added parsing features to add corresponding hand node method to client mapper interface but right now this features do not exist so I use and adapted Larry.Z solution.
In my project I use:
My folder structure is:
<base>/dao/
: MBG generated dao class<base>/dao/extended/
: Extended generated class (<DaoGeneratedName>Extended
)<base>/sqlmap/
: MBG generated client Interface and corresponding xml mapper<base>/sqlmap/extended/
:hand xml mapper and hand client Interface
(
<InterfaceGenerated>Extended extends InterfaceGenerated {...
)<base>/sqlmap/generated/
: copy of MBG generated mapper namespace changedI have configured Mybatis - spring
Implement myBeanNameGenerator only if you need to have custom name like me. In this example you can delete row
p:nameGenerator-ref="myBeanNameGenerator"
If all your client Interfaces become extended you can substitute above
p:basePackage="<base>.sqlmap.extended"
(my project configuration is huge so I have extract most important bit )
This is an example of my client interface and mapper hand coded:
}
Where CountriesMapper is the client interface MBG generated
The hand coded correlated xml mapper is:
To make all work I have to integrate in xml mapper all interface method MBG generated and, to do this, I copied MBG generated xml mapper in
<base>/sqlmap/generated/
and change his namespace:The problem raise when db change and I have to use MBG to reflect the new db structure.
So I have create quickly a bash script that watch in
<base>/sqlmap/extended/
and check if there is an hand coded xml mapper. If there is hand coded xml mapper, copy corresponding MBG generated changing his namespace.All this code is not an graceful solution but works.
The bash script overwrite file in
<base>/sqlmap/generated/
so, not put in this folder your code.Make a backup copy of your project and modify the bash script, to custom it and use on your responsibility.
Use of the script
$ ./post-generator.sh "/home/...<base>"
do not put the last/
on pathThis path is the path to the folder that contains sqlmap, sqlmap/extended, sqlmap/generated
You can use a list of path if you, like me, have more then one
To use it by maven, I use this plugin in project pom.xml:
On project folder you can use
$ mvn exec:exec
or$ mvn mybatis-generator:generate exec:exec
If you use Netbeans you can configure a project action to run these goals
mybatis-generator:generate exec:exec
without left Netbeans. You can start it by hand when you have a change in db structure.Now you can work on exended mapper without problem and let MBG do his work if db structure change.
In your bean you can inject extended interface that have
automatic generated MBG methods plus your hand coded methods:
Where countriesMapperExtended bean is generated by mapperScanner above.