mybatis-plus代码生成器,xml文件生成在resource下

更多关于mybatis-plus,可以到mybatis-plus的官网上查看:https://mp.baomidou.com/

​ 通过Mybatis-Plus强大的自动代码生成功能我们可以让项目自动生成一些通用的类。如controller,service,dao,mapper,entity等。

进行代码自动生成之前,我们需要提前创建好一个表。自动代码生成会根据关联的这张表去生成代码。

1. 引入依赖

<!--这两个主要的依赖要加上-->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.2</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.0</version>
</dependency>

2. 代码:

public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
    	//获取工程的根目录
        String projectPath = System.getProperty("user.dir");//得到当前项目的路径
        gc.setOutputDir(projectPath + "/src/main/java");//生成文件输出根目录
        gc.setAuthor("hongcheng");//作者
        gc.setOpen(false);//是否生成代码后打开本地目录
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");//我的mysql是5.7版本的
    	//dsc.setDriverName("com.mysql.cj.jdbc/Driver");//这是mysql8.0以上版本的
        dsc.setUsername("root");
        dsc.setPassword("654123");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("cheng");
        pc.setParent("com.hong");//设置父包  com.hong.cheng.controller/service/dao/entity
        mpg.setPackageInfo(pc);

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

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

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/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.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);//controller是否使用restful风格
        mpg.setStrategy(strategy);
        mpg.execute();
    }

3. 自动生成之后的目录结构

在这里插入图片描述

可以看到mapper下的UserMapper.xml文件放置resources下。

代码生成之后,可以看到代码中有的注解报红,这是因为没有加入相关依赖导致的,只需要在pom.xml中加入相关依赖就可以使用。

Mybatis-Plus Generator是基于Mybatis-Generator的扩展,支持自定义代码生成器模板。 自定义模板的参数设置步骤如下: 1. 新建自定义模板文件Mybatis-Plus Generator的classpath:templates目录下新建自定义模板文件,例如在该目录下新建一个MyMapper.xml.vm文件。 2. 在代码生成器配置文件中配置自定义模板路径 在代码生成器的配置文件中配置自定义模板路径,在该文件中找到templateEngine节点下的velocityTemplatePath节点,并将其值设置为自定义模板文件所在目录的绝对路径。 例如: ``` <templateEngine> <type>velocity</type> <velocity> <properties> <property> <name>file.resource.loader.path</name> <value>/Users/username/mybatis-plus-generator/templates</value> </property> </properties> </velocity> </templateEngine> ``` 3. 设置自定义模板参数 在自定义模板文件中,使用Velocity模板语言来设置自定义参数。例如,在MyMapper.xml.vm文件中添加以下内容: ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="$packageName.$moduleName.mapper.$entityNameMapper"> <resultMap id="BaseResultMap" type="$packageName.$moduleName.entity.$entityName"> #foreach($column in $table.columns) <result column="$column.columnName" property="$column.propertyName" jdbcType="$column.jdbcType" /> #end </resultMap> </mapper> ``` 在该模板中,使用了$packageName、$moduleName、$entityName等参数,这些参数需要在代码生成器的配置文件中进行设置。 在代码生成器的配置文件中找到strategyConfig节点下的superEntityClass、entityLombokModel、entityColumnConstant、entityBuilderModel、controllerMappingHyphenStyle、versionFieldName、logicDeleteFieldName、tablePrefix、fieldPrefix、include、exclude、entityTableFieldAnnotationEnable等节点,将其值设置为需要的参数值。 例如: ``` <strategyConfig> <superEntityClass>com.baomidou.mybatisplus.extension.activerecord.Model</superEntityClass> <entityLombokModel>true</entityLombokModel> <entityColumnConstant>true</entityColumnConstant> <entityBuilderModel>true</entityBuilderModel> <controllerMappingHyphenStyle>true</controllerMappingHyphenStyle> <versionFieldName>version</versionFieldName> <logicDeleteFieldName>deleted</logicDeleteFieldName> <tablePrefix>sys_</tablePrefix> <fieldPrefix>sys_</fieldPrefix> <include>sys_user,sys_role,sys_permission</include> <exclude>sys_log</exclude> <entityTableFieldAnnotationEnable>true</entityTableFieldAnnotationEnable> </strategyConfig> ``` 以上是自定义模板的参数设置步骤,按照这些步骤进行设置即可。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值