Recently, a small partner asked me MyBatis have the ability to automatically create table structure, because they have been used to hibernate, it is assumed that in the entity class configuration under the annotation or write the mapping file, the system can automatically create a table after startup.
I can only regret to tell him that MyBatis does not have this function, look at his mood, I can only comfort him, even without this function, we can develop their own AH ~ ~
So there is the following system, has open source everyone can come down to see ~ ~
mybatis_buildtable_v0.2
Https://git.oschina.net/sunchenbin/Mybatis_BuildTable_V0.2.git
The project architecture uses Springmvc+mybatis+maven, which features the ability to create tables by configuring the model annotations, modifying the table structure, and currently only supports MySQL, because the focus is on highlighting the Mybatis auto-creation feature, so there is no more thought on the frame.
Usage Specification:
The core code is in Model-store-repo.
1.sysmysqlcolumns.java This object is configured with MySQL data types, the more types you configure here, the more types you can use to create tables
2.lengthcount.java is a custom annotation used to mark the data type configured in the Sysmysqlcolumns.java, marking the type with several lengths to set, such as Datetime/varchar (1)/decimal ( 5,2), respectively, is required to set 0 one of 2
3.column.java is also a custom annotation used to mark the fields in the model as the basis for creating a table, which is not marked, is not scanned, there are several properties to set the field name, field type, length and other properties, see the comments on the code for details
4.table.java is also a custom annotation that marks the model object with a property name that sets the table name after the model is generated, and if the annotation is not set, the model is not scanned to
OK, when the system starts, it will automatically call Sysmysqlcreatetablemanagerimpl.java's Createmysqltable () method, yes, this is the core method, responsible for creating, deleting, modifying the table.
Model-store-frontend/resources/config/autocreatetable.properties
You will find that there is a configuration file with two configurations
1.mybatis.table.auto=update
2.mybatis.model.pack=com.sunchenbin.store.model
The system provides two modes:
1. When mybatis.table.auto=create, when the system starts, all tables are deleted, and the table is re-built according to the structure configured in model, which destroys the original data.
2. When mybatis.table.auto=update, the system will automatically determine which tables are new, which fields to modify the type, and which fields to delete, and which fields to add, the operation will not destroy the original data.
3.mybatis.model.pack This configuration is used to configure the package name of the object to be scanned for creating the table
The system is configured to start with MAVEN, the Web relies on repo,frontend and mobile relies on the web, so to run frontend and mobile, you must first instal the web and repo
As for how to start a project with Maven .... No more talking.
Ah ~ ~ Some friends said that the actual significance of the system, a matter of opinion, for some people is meaningless, and for the other part of the person may have a lot of help ... All right, just the sauce.
Later I'm going to refactor it into a jar package for everyone to use.
MyBatis automatically create TABLE/Update table structure