前提条件

Msql正常连接

#mysql配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

一、依赖

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>

二、配置


@MapperScan("com.mackes.mapper") //*** mapper路径
@Configuration
public class MybatisPlusConfig {
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

三、生成工具类

public class MybatisAutoCode {
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://127.0.0.7:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8", "root", "root");

    public static void main(String[] args) {

        //bof settings
        //包存放路径
        String pkgPath = System.getProperty("user.dir")+ "/mybatis-spring-boot/src/main/java";
        //mapper xml 文件存放路径
        String pkgXml = System.getProperty("user.dir")+ "/mybatis-spring-boot/src/main/resources/mapper";
        //作者
        String author = "mackes";
        //命名空间
        String pkgName = "com.fanslp";
        //实体类存放包名
        String pkgEntity = "model";
        //mapper类存放包名
        String pkgMapper = "mapper";
        //eof settings
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(builder ->builder.outputDir(pkgPath).author(author).fileOverride().disableOpenDir())
                .packageConfig(builder -> builder.parent(pkgName)
                        .entity(pkgEntity)
                        .mapper(pkgMapper)
                        .pathInfo( new HashMap<OutputFile,String>(){{
                                       put(OutputFile.mapperXml,pkgXml);
                                   }}
                        )
                )
                //.templateConfig(builder -> builder.disable(TemplateType.CONTROLLER,TemplateType.XML,TemplateType.SERVICE, TemplateType.SERVICEIMPL))
                .strategyConfig((scanner,builder) -> builder.addInclude(scanner.apply("请输入表名")))
                .execute();

    }
}