When using Mybatis for development, in order to reduce program writing time and submit development efficiency, reverse generation tools are usually used to generate part of the code, such as Mapper file, mapper.xml file and entity class.

Tk-mybatis – Generator is used in this paper. Cut the crap and get to work!

  1. Create a test table
  2. Create a New SpringBoot project
  3. Use the reverse generation tool to generate the Mapper interface, mapper.xml file, and entity objects.

Project Structure:

--src
----main
--------com.common
------------my.mapper MyMApper.interface
------------mybatis.utils GeneratorDisplay.class
----generatorConfig.xml
----pom.xml
Copy the code
  • So what MyMapper interface does is it inherits this structure and uses the methods in Mapper
  • GeneratorDisplay is a utility class that reads configuration files (Generatorconfig.xml)
  • Generatorconfig. XML is a reverse generation file configuration file
  • See the link at the end of this article for the code.

Create table structure

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'primary key',
  `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'name',
  `age` int(11) NULL DEFAULT NULL COMMENT 'age',
  `gender` tinyint(3) NULL DEFAULT NULL COMMENT 'Gender (0-female 1-male)'.PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;
Copy the code

After the table structure is created, you only need to modify GeneratorConfig.xml as required. Clicking the main function that runs GeneratorDisplay will generate the corresponding file in the configured folder.


Example code -GitHub

Example code -Gitee

Personal blog – Ifknow