AutoGenerator自动生成代码

1.添加以下依赖(如果不需要swagger的话, 依赖只需要导入前面3个即可):

  (1) mybatis-plus-generator;
  (2) mybatis-plus-boot-starter;
  (3) velocity-engine;
  (4) springfox-swagger2;
  (5) springfox-swagger-ui;

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

 

2.创建以下自动生成代码类(如果不需要swagger的话,代码里面修改gc.setSwagger2(true);为gc.setSwagger2(false);) 

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     代码生成器:
 *          需要更改地方( tableName(表名) | absolutePath(生成代码放置路径) | 数据库配置 | author(作者名称))
 * </p>
 */
public class MpGenerator {

    private static String absolutePath = "D:\\test\\src\\main\\java"; // 绝对路径(生成代码放置位置 - 你自己的)
    private static String packageName= "com.example.test"; // 包名
    private static String tableName = "tab_user"; // 生成的表名称|  生成多张表代码,输入格式为"t1,t2,t3"(逗号分隔); 需要生成所有表代码,这里设置为空; 单表,输入指定表名 "

    // 注: tinyInt1isBit=false这个不加的话, 如果你的数据库字段类型为tinyint(1)转成java实体类字段会变成boolean,而不是int
    private static String jdbcUrl = "jdbc:mysql://xxxxxxxx:3306/xxx?useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false&useSSL=false";
    private static String jdbcDriverName = "com.mysql.jdbc.Driver";
    private static String jdbcRoot = "xxxxxxxx";
    private static String jdbcPassWord = "xxxxxxxx";

    // other
    private static String author = "xxxxxxxx"; // 作者名称
    private static String modelName= "model"; // 实体类所在文件夹名称(根据个人习惯)
    private static String daoName= "dao"; // mapper接口所在文件夹名称(根据个人习惯)

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(absolutePath);
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setBaseResultMap(true); // mapper.xml生成resultMap
        gc.setBaseColumnList(true); // mapper.xml 生成 ColumnList
        gc.setSwagger2(true); // 实体属性 Swagger2 注解
        gc.setFileOverride(false); // 新生成文件是否覆盖已有文件
        gc.setEnableCache(false); // 是否开启二级缓存
        gc.setServiceName("%sService");  // 设置生成的service接口的名字的首字母是否为I
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(jdbcUrl);
        dsc.setDriverName(jdbcDriverName);
        dsc.setUsername(jdbcRoot);
        dsc.setPassword(jdbcPassWord);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageName);
        pc.setMapper(daoName);
        pc.setEntity(modelName);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };

//        String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.vm"; // 如果模板引擎是 velocity

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String replace = packageName.replace(".", "\\");
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return absolutePath + "/"+ replace+  "/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(tableName.split(","));
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}

3.补充, 使用mybatis-plus的一键生成, 则表示使用mybatis-plus-boot-starter,官方文档说的是使用它们这个对mybatis框架不会产生影响,只是起到增加作用,说是打辅助的......由于我上面把mapper.xml放到了src/main/java文件夹里面.为了springboot项目能正常运行,记得做一下添加

配置文件:application.yml
#mybatis:
mybatis-plus:
  mapperLocations: classpath*:/com/test/mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
	
	
项目springboot启动类: xxxApplication
@MapperScan("com.test.dao")
	
	
项目pom.xml:
<resources>
	<resource>
		<directory>src/main/java</directory>
		<includes>
			<include>**/*.xml</include>
		</includes>
	</resource>
</resources>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值