Code generators, also known as reverse engineering, automatically generate entity classes, mapping files, and interfaces based on the table structure of the database.
This is the opposite of Hibernate’s automatic table building.
Long ago, I wrote a code generator project, and I have been using it myself. It is very convenient, well verified, and supports many databases.
See a lot of friends in the database to generate entity class worry, now share with you, improve the efficiency of development.
Introduction to mybatis- plus-code-Generator project
Engineering Address: Mybatis -plus-code-generator: github.com/fengwenyi/m…
Directory structure:
The Config class
Configuration can be done in this class for each person’s taste
/** package name: service */
public static final String PACKAGE_NAME_SERVICE = "repository";
/** Service. Impl */
public static final String PACKAGE_NAME_SERVICE_IMPL = "repository.impl";
/** package name: model */
public static final String PACKAGE_NAME_MODEL = "entity";
/** package name: dao */
public static final String PACKAGE_NAME_DAO = "mapper";
/** Package name: XML */
public static final String PACKAGE_NAME_XML = "xml";
/** File name suffix: Model */
public static final String FILE_NAME_MODEL = "%sEntity";
/** File name suffix: Dao */
public static final String FILE_NAME_DAO = "I%sMapper";
/** File name suffix: Mapper */
public static final String FILE_NAME_XML = "%sMapper";
/** Service */
public static final String FILE_NAME_SERVICE = "MP%sRepository";
/** File name suffix: ServiceImpl */
public static final String FILE_NAME_SERVICE_IMPL = "%sRepositoryImpl";
/** Logically delete field */
public static final String FIELD_LOGIC_DELETE_NAME = "delete_status";
/ * * * / author
public static final String AUTHOR = "Erwin Feng";
/** Indicates whether Swagger is supported. */ is not supported by default
public static final Boolean SWAGGER_SUPPORT = false;
Copy the code
MySQL8CodeGenerator
The circle has to be changed.
After modification, execution generates the corresponding code file, as shown in the following example.
The sample
We take the example of MyBatis-Plus official website as an example to demonstrate
SQL
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT 'primary key ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT 'name',
age INT(11) NULL DEFAULT NULL COMMENT 'age',
email VARCHAR(50) NULL DEFAULT NULL COMMENT 'email'.PRIMARY KEY (id)
);
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1.'Jone'.18.'[email protected]'),
(2.'Jack'.20.'[email protected]'),
(3.'Tom'.28.'[email protected]'),
(4.'Sandy'.21.'[email protected]'),
(5.'Billie'.24.'[email protected]');
Copy the code
testMySQLCodeGenerator
DbType dbType = DbType.MYSQL;
String dbUrl = "JDBC: mysql: / / 192.168.16.128:3306 / study - spring - the boot - mybatis - plus";
String username = "root";
String password = "123456";
String driver = "com.mysql.cj.jdbc.Driver";
// Table prefix, generated entity class, no prefix
String [] tablePrefixes = {};
// Table name, empty, generate all tables
String [] tableNames = {};
// Field prefix
String [] fieldPrefixes = {};
// Base package name
String packageName = "com.fengwenyi.studyspringbootmybatisplus.db";
CommonUtils.execute(dbType, dbUrl, username, password, driver, tablePrefixes, tableNames, packageName, fieldPrefixes);
Copy the code
Generated file
We copy the generated files into the project
test
@Autowired
private MPUserRepository mpUserRepository;
@Test
public void testSelectList(a) {
List<UserEntity> list = mpUserRepository.list();
list.forEach(userEntity -> log.info(userEntity.toString()));
}
Copy the code
Run:
Project link
Code generator: mybatis-plus-code-generator: github.com/fengwenyi/m…
Example: study-spring-boot-mybatis-plus: github.com/fengwenyi/s…