database table structure changes, can be said to be a headache in the development process. Headache to a lot of programmers in order not to change the table structure, preferring to write a bunch of "compromise" code in the code is reluctant to move the table structure, resulting in more problems, when the problem accumulates in the table structure has to be changed, found that the table structure of the problem has been spread to many other system modules, is not by modifying a module can be simple to solve the problem, so my understanding is that the table structure necessary to make changes to do early, do not worry about trouble, because the moment to avoid problems, will in the future caused by the number of times the problem of growth.
Once the table structure changes, it is inevitable that the code to make a lot of changes, it is unavoidable, we can only make the code more flexible to deal with the changes in the database. The inheritance of this piece of mybatis can be used.
Often use Mybatis-generator to generate the corresponding database Model,dao and mapping, and then modify on the basis above, so that once the database structure changes, it is more difficult to re-generate the code with generator. This problem can be solved using the MyBatis inheritance feature. The scenario is as follows.
First, use generator to generate the model,dao,mapping:attachsendtask of the base class Attachsendtaskmapper
MyBatis How to use inheritance
Model:public class Attachsendtaskextend extends Attachsendtask
Dao:public interface Attachsendtaskextendmapper extends Attachsendtaskmapper
Public list<attachsendtaskextend> doselect (); Using subclasses as query result objects
Mapping: <mapper namespace= "Com.aaa.bbb.ccc.ddd.AttachSendTaskExtendMapper" >
<resultmap id= "Baseresultmap" type= "com.aaa.bbb.ccc.ddd. Attachsendtaskextend "extends=" Com.aaa.bbb.ccc.ddd.AttachSendTaskMapper.BaseResultMap ">
extends= "${namespace}.${resultmap}"
This table structure changes, re-use generator to generate a set of code for the base class, custom virtual properties and extended attributes, placed in the subclass, change the base class to deal with the database table structure changes.
Using the MyBatis inheritance feature to respond to database changes