一、Mybatis-PlusGenerator是什么
Mybatis-PlusGenerator是Mybatis-Plus框架中的一个代码生成器工具,可以根据指定的表结构生成对应的Java DAO层代码。作为Mybatis-Plus框架的一部分,它继承了Mybatis-Plus框架的优点,包括强大的CRUD功能、方便的分页查询、支持复杂条件查询等。同时,Mybatis-PlusGenerator可以免去手写DAO层代码的繁琐过程,提高开发效率。
二、Mybatis-PlusGenerator的应用场景
Mybatis-PlusGenerator适用于以下场景:
1、需要对数据库进行 CURD 操作的 Java Web 开发。
2、需要快速生成 DAO 层代码的 Java 开发。
3、需要规范团队开发的 Java Web 开发。
三、Mybatis-PlusGenerator的使用方法
以下将以MySQL数据库为例,介绍Mybatis-PlusGenerator的使用方法。
1、在 pom.xml 中添加依赖:
“`
com.baomidou
mybatis-plus-generator
3.4.2
“`
2、添加 generatorConfig.xml 配置文件
在src/main/resources目录下新建generatorConfig.xml文件,文件内容如下:
“`
“`
3、运行MybatisPlusGenerator
在 IDEA 中执行如下命令:
“`
public static void main(String[] args) throws FileNotFoundException {
// 执行代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
//生成文件的输出目录
gc.setOutputDir(“D://zhaorui//test”);
// 是否覆盖已有文件
gc.setFileOverride(true);
// 是否打开输出目录
gc.setOpen(false);
// 开启 activeRecord 模式
gc.setActiveRecord(true);
// 是否在xml中添加二级缓存配置
gc.setEnableCache(false);
// 开启 BaseResultMap
gc.setBaseResultMap(true);
// XML columList
gc.setBaseColumnList(false);
//排除生成的表
gc.setServiceImplName(“%sService”);
gc.setControllerName(“%sController”);
//自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName(“%sMapper”);
gc.setXmlName(“%sMapper”);
gc.setServiceName(“%sService”);
gc.setEntityName(“%s”);
gc.setAuthor(“zhaorui”);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(“jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8”);
// dsc.setSchemaName(“public”);
dsc.setDriverName(“com.mysql.jdbc.Driver”);
dsc.setUsername(“root”);
dsc.setPassword(“root123456”);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(“com.example.demo”);
// pc.setModuleName(“test”);
pc.setController(“controller”);
pc.setService(“service”);
pc.setServiceImpl(“service.impl”);
pc.setMapper(“mapper”);
pc.setEntity(“model”);
mpg.setPackageInfo(pc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController(null);
templateConfig.setServiceImpl(null);
templateConfig.setService(null);
templateConfig.setXml(null);
templateConfig.setMapper(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//配置全局大写命名
strategy.setCapitalMode(true);
// 所有表名前缀
strategy.setTablePrefix(new String[] {“t_”});
// 表名生成策略
strategy.setNaming(NamingStrategy.underline_to_camel);
// 需要生成的表
strategy.setInclude(new String[] {“user”});
//strategy.setExclude(new String[]{“test”}); // 排除生成的表
// 自定义实体Father类
strategy.setSuperMapperClass(null);
strategy.setSuperServiceClass(null);
strategy.setSuperServiceImplClass(null);
strategy.setSuperControllerClass(null);
strategy.setSuperEntityClass(null);
//自定义基础的Entity类,公共字段
strategy.setSuperEntityColumns(new String[]{“id”});
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setEntityBuilderModel(true);
mpg.setStrategy(strategy);
mpg.execute();
}
“`
四、Mybatis-PlusGenerator的实现原理
Mybatis-PlusGenerator的实现原理可以总结为两部分,分别是根据数据库 schema 信息生成 Java 实体,以及根据 Java 实体生成 Mybatis XML 文件。
在根据数据库 schema 信息生成 Java 实体过程中,主要分为以下几个步骤:
1、连接数据库:通过 JDBC 连接驱动连接数据库,获取数据库元数据。
2、解析元数据:通过元数据获取数据库的各种信息,如库名、表名、列名、表结构等。
3、代码生成:根据元数据生成 Java 实体源代码,并将其保存至指定路径。
在根据 Java 实体生成 Mybatis XML 文件过程中,主要分为以下几个步骤:
1、读取 Java 实体:从指定路径读取 Java 实体源代码。
2、解析 Java 实体:解析 Java 实体源代码,获取其字段名称、数据类型等信息。
3、代码生成:根据 Java 实体信息生成 Mybatis 相关的 XML 文件内容,并将其保存至指定路径。
五、小结
Mybatis-PlusGenerator是一款开源的Java代码生成器工具,可以根据数据库表自动生成Java DAO层代码。它操作简单、易上手,大大提高了开发效率。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/157780.html