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
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.
public interface MybatisBaseDao {
int countByExample(E example);
int deleteByExample(E example);
int deleteByPrimaryKey(PK id);
int insert(T record);
int insertSelective(T record);
List selectByExample(E example);
T selectByPrimaryKey(PK id);
int updateByExampleSelective(@Param("record") T record, @Param("example") E example);
int updateByExample(@Param("record") T record, @Param("example") E example);
int updateByPrimaryKeySelective(T record);
int updateByPrimaryKey(T record);
}
Of course, you can custom your BaseDao according to your demand. For example we have a UserDao, Then you can defind it like this
public interface UserDao extends MybatisBaseDao{
List selectUserByAddress(String address); // hand edited query method
}
For 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 named manual. The two mapper files start with the same header . 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!