Using the MyBatis inheritance feature to respond to database changes

Source: Internet
Author: User
Tags generator

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.