MyBatis-Plus系列之代码生成器

1,216 阅读2分钟

代码生成器,也叫逆向工程,是根据数据库里的表结构,自动生成对应的实体类、映射文件和接口。

这和hibernate的自动建表恰好相反。

很早之前,写了一个代码生成器的工程,自己也一直在用,很方便,也经过大量验证,也支持很多数据库。

看到很多小伙伴在为数据库生成实体类发愁,现分享给大家,提高开发效率。

mybatis-plus-code-generator 工程介绍

工程地址:mybatis-plus-code-generatorgithub.com/fengwenyi/m…

目录结构:

image-20201116221745190

Config类

针对每个人的习惯,可以在这个类里进行配置

    /** 包名:service */
    public static final String PACKAGE_NAME_SERVICE = "repository";

    /** 包名:service.impl */
    public static final String PACKAGE_NAME_SERVICE_IMPL = "repository.impl";

    /** 包名:model */
    public static final String PACKAGE_NAME_MODEL = "entity";

    /** 包名:dao */
    public static final String PACKAGE_NAME_DAO = "mapper";

    /** 包名:xml */
    public static final String PACKAGE_NAME_XML = "xml";

    /** 文件名后缀:Model */
    public static final String FILE_NAME_MODEL = "%sEntity";

    /** 文件名后缀:Dao */
    public static final String FILE_NAME_DAO = "I%sMapper";

    /** 文件名后缀:Mapper */
    public static final String FILE_NAME_XML = "%sMapper";

    /** MP开头,Service结尾 */
    public static final String FILE_NAME_SERVICE = "MP%sRepository";

    /** 文件名后缀:ServiceImpl */
    public static final String FILE_NAME_SERVICE_IMPL = "%sRepositoryImpl";

    /** 逻辑删除字段 */
    public static final String FIELD_LOGIC_DELETE_NAME = "delete_status";

    /** 作者 */
    public static final String AUTHOR = "Erwin Feng";

    /** 是否支持Swagger,默认不支持 */
    public static final Boolean SWAGGER_SUPPORT = false;

MySQL8CodeGenerator

image-20201116222257951

圈中的地方是必须要改的。

修改之后,执行就可以生成相应的代码文件了,下面我们举个例子。

示例

我们以MyBatis-Plus官网的例子为例,拿来演示

SQL

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

testMySQLCodeGenerator

DbType dbType = DbType.MYSQL;
String dbUrl = "jdbc:mysql://192.168.16.128:3306/study-spring-boot-mybatis-plus";
String username = "root";
String password = "123456";
String driver = "com.mysql.cj.jdbc.Driver";
// 表前缀,生成的实体类,不含前缀
String [] tablePrefixes = {};
// 表名,为空,生成所有的表
String [] tableNames = {};
// 字段前缀
String [] fieldPrefixes = {};
// 基础包名
String packageName = "com.fengwenyi.studyspringbootmybatisplus.db";
CommonUtils.execute(dbType, dbUrl, username, password, driver, tablePrefixes, tableNames, packageName, fieldPrefixes);

生成的文件

image-20201116222805795

我们将生成的文件拷贝到工程里

image-20201116222957495

测试

@Autowired
private MPUserRepository mpUserRepository;

@Test
public void testSelectList() {
    List<UserEntity> list = mpUserRepository.list();
    list.forEach(userEntity -> log.info(userEntity.toString()));
}

运行:

image-20201116223227966

工程链接

代码生成器:mybatis-plus-code-generatorgithub.com/fengwenyi/m…

示 例 工 程:study-spring-boot-mybatis-plusgithub.com/fengwenyi/s…