zl程序教程

您现在的位置是:首页 >  后端

当前栏目

MyBatis-Plus代码生成器模板(SpringBoot版本)

SpringBoot模板mybatis 版本 Plus 代码生成
2023-06-13 09:16:06 时间

获取生成后的代码存放的绝对路径方法如上图

代码:

  • 适用于SpringBoot全注解
  • 无Xml文件
  • Service层有接口和impl实现类
  • ActiveRecord特性开启
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.io.File;

/*
代码生成器:
    适用于SpringBoot全注解
    无Xml文件
    Service层有接口和impl实现类
  	ActiveRecord特性开启
    @author: 常家壮
    @since 2020-10-02
 */
public class Generator {

    //获取 项目绝对路径
    private static String canonicalPath = "";

    public static void main(String[] args) {

        //获取项目路径
        try {
            canonicalPath = new File("").getCanonicalPath();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //代码生成后存放的路径,获取路径方法: 在项目中的main文件夹右键->>找到 Copy Path选项->>粘贴到下面即可,我已经在下面添加了'\\'
        String filePath = "请看博客截图,或者上面的注释填写";

        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor("常家壮");//作者名称
        gc.setOutputDir(filePath+"\\"+"java");//代码生成路径
        gc.setFileOverride(true);// 是否覆盖同名文件,默认是false
        gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 是否有二级缓存
        gc.setBaseResultMap(false);// XML 是否创建ResultMap
        gc.setBaseColumnList(false);// XML 是否创建columList
        gc.setOpen(false);//生成后打开文件夹
        /* 自定义文件命名,注意 %s 会自动填充表实体属性! */
        gc.setMapperName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
            //如果需要这个层注掉即可
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            // 自定义数据库表字段类型转换【可选】
            @Override
            public IColumnType processTypeConvert(GlobalConfig gc, String fieldType) {
                System.out.println("转换类型:" + fieldType);
                // 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。
                //默认会把日期类型 转为LocalDateTime ,在查询的时候会报错,这里改为Date
                String t = fieldType.toLowerCase();
                if(t.contains("date") || t.contains("time") || t.contains("year")){
                    return DbColumnType.DATE;
                }else{
                    return super.processTypeConvert(gc,fieldType);
                }

            }
        });
        dsc.setDriverName("com.mysql.jdbc.Driver");//数据库驱动
        dsc.setUsername("root");//数据库用户
        dsc.setPassword("123456");//数据库密码
        dsc.setUrl("jdbc:mysql://数据库ip地址/数据库名称?useUnicode=true&characterEncoding=utf8");//数据库连接地址
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);//【实体】是否为lombok模型(默认 false)
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.chang.csdn");// 自定义包路径,根据自己的来
        pc.setController("controller");// 这里是控制器包名,默认 web
        pc.setMapper("mapper");// 设置Mapper包名,默认mapper
        pc.setService("service");// 设置Service包名,默认service
        pc.setEntity("pojo");// 设置实体类包名,默认entity,继承的父类,已序列化
        mpg.setPackageInfo(pc);

        // 执行生成
        mpg.execute();
    }
}

依赖缺一不可

就是为了防止爆红,或者无法生成代码

    <dependencies>
       <!--mybatisplus 代码生成器下面的依赖必须有-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <!--就不需要手动引入mybatis -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <!--mybatisplus 代码生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.51</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <!--slf4j-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.2.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

希望这篇博客对你有作用!