MybatisPlus代码自动生成工具类

gitee仓库地址

package com.mybatisplus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * 代码自动生成器
 */
public class GeneratorCode {
    public static void main(String[] args) {
        // 需要构建一个 代码自动生成器 对象
        AutoGenerator mpg = new AutoGenerator();
        // 配置策略
        // 1、全局配置
        GlobalConfig gc = new GlobalConfig();
        //String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("F:/companyprojcet/practice/mybatis-plus/src/main/java");
        gc.setAuthor("lihua");
        gc.setOpen(false);
        gc.setFileOverride(false); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        //3、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("mp");
        pc.setParent("com.mybatisplus");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("user", "tab_order"); // 设置要映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true); // 自动lombok;
        strategy.setLogicDeleteFieldName("deleted");
        // 自动填充配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);
        // 乐观锁
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true); //localhost:8080/hello_id_2
        mpg.setStrategy(strategy);
        mpg.execute(); //执行
    }
}
MyBatisPlus是一个基于MyBatisJava快速开发框架,它提供了一系列便捷的方法,简化了数据持久层的操作,包括实体生成、CRUD操作、数据库迁移等。其中,代码自动生成是MyBatisPlus的一大亮点,它能根据数据库表结构自动生成对应的Java实体(Pojo)、Mapper接口和XML文件。 具体步骤如下: 1. **配置Generator工具**:在MyBatisPlus的配置文件中,通常是在`GlobalConfig`或`BaseMapper`中,你可以配置代码生成器的相关信息,如包名、文件路径、实体命名规则等。 ```java // 在全局配置中 public GlobalConfig global = new GlobalConfig(); global.setDbType("mysql"); // 数据库型 global.setOutputDir("src/main/java"); // 生成的文件目录 global.setAuthor("your_name"); // 作者名称 global.setProjectName("your_project"); // 项目名称 ``` 2. **启用代码生成**:调用`generator.generate()`方法,传入表名列表,MyBatisPlus会扫描这些表并生成相应的代码。 ```java List<String> tableNameList = Arrays.asList("your_table_1", "your_table_2"); MysqlGlobalConfig mysqlConfig = new MysqlGlobalConfig(); // 如果是MySQL数据库 mysqlConfig.setTablePrefix("your_prefix_"); // 表前缀 generator.generate(tableNameList, mysqlConfig); ``` 3. **查看生成的代码**:在指定的目录下,你会看到生成的实体(例如`YourTableName.java`)、Mapper接口(例如`YourTableNameMapper.java`)以及对应的XML映射文件。 MyBatisPlus的代码自动生成能够极大地提高开发效率,但需要注意的是,生成的代码仅为初始版本,可能需要根据实际业务需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

macrohua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值