天天看点

mybatis list为空判断_MyBatis-Plus系列之代码生成器mybatis-plus-code-generator 工程介绍Config类MySQL8CodeGenerator示例SQLtestMySQLCodeGenerator生成的文件测试工程链接

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

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

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

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

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

mybatis-plus-code-generator 工程介绍

工程地址:mybatis-plus-code-generator:https://github.com/fengwenyi/mybatis-plus-code-generator

目录结构:

mybatis list为空判断_MyBatis-Plus系列之代码生成器mybatis-plus-code-generator 工程介绍Config类MySQL8CodeGenerator示例SQLtestMySQLCodeGenerator生成的文件测试工程链接

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

mybatis list为空判断_MyBatis-Plus系列之代码生成器mybatis-plus-code-generator 工程介绍Config类MySQL8CodeGenerator示例SQLtestMySQLCodeGenerator生成的文件测试工程链接

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

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

示例

我们以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, '[email protected]'),(2, 'Jack', 20, '[email protected]'),(3, 'Tom', 28, '[email protected]'),(4, 'Sandy', 21, '[email protected]'),(5, 'Billie', 24, '[email protected]');
           

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);
           

生成的文件

mybatis list为空判断_MyBatis-Plus系列之代码生成器mybatis-plus-code-generator 工程介绍Config类MySQL8CodeGenerator示例SQLtestMySQLCodeGenerator生成的文件测试工程链接

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

mybatis list为空判断_MyBatis-Plus系列之代码生成器mybatis-plus-code-generator 工程介绍Config类MySQL8CodeGenerator示例SQLtestMySQLCodeGenerator生成的文件测试工程链接

测试

@Autowiredprivate MPUserRepository mpUserRepository;@Testpublic void testSelectList() {    List list = mpUserRepository.list();    list.forEach(userEntity -> log.info(userEntity.toString()));}
           

运行:

mybatis list为空判断_MyBatis-Plus系列之代码生成器mybatis-plus-code-generator 工程介绍Config类MySQL8CodeGenerator示例SQLtestMySQLCodeGenerator生成的文件测试工程链接

工程链接

代码生成器:mybatis-plus-code-generator:https://github.com/fengwenyi/mybatis-plus-code-generator

诗 例 工 程:study-spring-boot-mybatis-plus:https://github.com/fengwenyi/study-spring-boot/tree/master/spring-boot-mybatis-plus

原文:https://developer.aliyun.com/article/778405

继续阅读