MybatisPlus 代码生成器模板

依赖jar包


    com.baomidou
    mybatis-plus-generator
    3.2.0



    org.freemarker
    freemarker
    2.3.29

代码


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

/**
 * -mp代码生成工具类,使用freemarker引擎
 * -使用前可以进行的修改
 *      gc.setAuthor  设置作者,会出现在类前
 *      gc.setOutputDir 设置输出路径,默认为 D:\
 *      pc.setParent 设置输出的包名(也即文件夹名)
 *      DataSourceConfig 设置数据源
 * -实体类使用lombok,swagger2,驼峰命名,去除is_前缀
 * -controller 使用rest风格
 * -mapper.xml生成resultMap和baseColumnList
 * @author gcl
 * @create 2019-09-17 14:51
 */
public class CodeGenerator {

    /**
     * 

* 读取控制台内容 *

*/ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); // 选择 freemarker 引擎,默认 Veloctiy mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 全局配置 GlobalConfig gc = new GlobalConfig(); //作者 gc.setAuthor("gcl"); //路径,默认为 D:\ //String projectPath = System.getProperty("user.dir"); //gc.setOutputDir(projectPath + "java"); // XML ResultMap gc.setBaseResultMap(true); // XML column List gc.setBaseColumnList(true); //使用swagger2 gc.setSwagger2(true); //设置主键生成策略 gc.setIdType(IdType.ID_WORKER_STR); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("密码"); dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意 //实体类使用lombok strategy.setEntityLombokModel(true); //rest风格controller strategy.setRestControllerStyle(true); //is_xxx 去除is_ 前缀 strategy.setEntityBooleanColumnRemoveIsPrefix(true); //字段注释 strategy.setEntityTableFieldAnnotationEnable(true); //数据表命名策略 strategy.setNaming(NamingStrategy.underline_to_camel); //数据表列生成策略 strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 需要生成的表 strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("CodeGeneratorFolder"); mpg.setPackageInfo(pc); // 执行生成 mpg.execute(); } }

测试结果

MybatisPlus 代码生成器模板_第1张图片
MybatisPlus 代码生成器模板_第2张图片
实体类


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 

* *

* * @author gcl * @since 2019-09-17 */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @ApiModel(value="User对象", description="") public class User implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "主键") @TableId(value = "id", type = IdType.ID_WORKER_STR) private Long id; @ApiModelProperty(value = "姓名") @TableField("name") private String name; @ApiModelProperty(value = "年龄") @TableField("age") private Integer age; @ApiModelProperty(value = "邮箱") @TableField("email") private String email; @ApiModelProperty(value = "直属上级id") @TableField("manager_id") private Long managerId; @ApiModelProperty(value = "创建时间") @TableField("create_time") private LocalDateTime createTime; @ApiModelProperty(value = "修改时间") @TableField("update_time") private LocalDateTime updateTime; @ApiModelProperty(value = "版本") @TableField("version") private Integer version; @ApiModelProperty(value = "逻辑删除标识(0.未删除,1.已删除)") @TableField("deleted") private Integer deleted; }

controller

/**
 * 

* 前端控制器 *

* * @author gcl * @since 2019-09-17 */ @RestController @RequestMapping("/user") public class UserController { }

service

/**
 * 

* 服务类 *

* * @author gcl * @since 2019-09-17 */ public interface IUserService extends IService { }

service实现类

/**
 * 

* 服务实现类 *

* * @author gcl * @since 2019-09-17 */ @Service public class UserServiceImpl extends ServiceImpl implements IUserService { }

mapper接口

/**
 * 

* Mapper 接口 *

* * @author gcl * @since 2019-09-17 */ public interface UserMapper extends BaseMapper { }

mapper.xml文件





    
    
        
        
        
        
        
        
        
        
        
    

    
    
        id, name, age, email, manager_id, create_time, update_time, version, deleted
    



你可能感兴趣的:(Mybatis)