Recently, a friend asked me if mybatis has the function of automatically creating table structures, because they have been used to using hibernate before. Naturally, they believe that by configuring betting or writing mapping files on entity classes, the table can be automatically created after the system starts.
I can only tell him with regret that mybatis does not have this function. Seeing his indifference, I can only comfort him that even if there is no such function, we can develop it ourselves~~
So there is the following system, which has been opened and you can come down and take a look~~
Mybatis_BuildTable_V0.2
https://git.oschina.net/sunchenbin/Mybatis_BuildTable_V0.2.git
The project architecture uses SpringMvc+Mybatis+Maven. The feature is to create tables and modify table structures by configuring model annotations. Currently, only Mysql is supported. Because the focus is to highlight the function of mybatis automatic table creation, so there is no need to think too much about the framework.
Usage specification:
The core code is in model-store-repo
1. The SysMysqlColumns.java object is configured with the mysql data type. The more types are configured here, the more types you can use when creating the table.
2.LengthCount.java is a custom annotation used to mark the data type configured in SysMysqlColumns.java. To mark this type requires several lengths, such as datetime/varchar(1)/decimal(5,2), respectively, 0, 1, 2, respectively.
3.Column.java is also a custom annotation, used to mark fields in the model. As the basis for creating tables, if not marked, it will not be scanned. There are several attributes used to set the settings of properties such as field name, field type, length, etc. For details, please refer to the comments on the code.
4.Table.java is also a custom annotation, used to mark the model object, and has an attribute name, which is used to set the table name after the model generates the table. If the annotation is not set, the model will not be scanned.
OK, after the system starts, it will automatically call the createMysqlTable() method of SysMysqlCreateTableManagerImpl.java. That's right, this is the core method, which is responsible for creating, deleting, and modifying tables.
model-store-frontend/resources/config/autoCreateTable.properties
You will find that there is such a configuration file, which has two configurations
1.mybatis.table.auto=update
2.mybatis.model.pack=com.sunchenbin.store.model
This system provides two modes:
1. When mybatis.table.auto=create, after the system starts, all tables will be deleted, and then the table will be rebuilt according to the structure configured in the model. This operation will destroy the original data.
2. When mybatis.table.auto=update, the system will automatically determine which tables are newly created, which fields need to be modified in type, which fields need to be deleted, and which fields need to be added. This 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 using maven. The web depends on repo, frontend and mobile depend on web. Therefore, to run frontend and mobile, you must first install the web and repo
As for how to start the project with maven... I won't say more.
The above is the full description of Mybatis automatic creation of tables and updating table structure introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!