美文网首页初见
SpringBoot使用AutoGenerator类自动生成代码

SpringBoot使用AutoGenerator类自动生成代码

作者: 不服高人_有罪 | 来源:发表于2020-06-18 16:22 被阅读0次

1、新建spring boot项目;
2、在pom.xml文件中添加对mybatis和mysql的依赖:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>

3、在application.properties文件中配置数据库连接:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT&2b8&useSSL=false
spring.datasource.username=登录名
spring.datasource.password=登录密码

4、新建AutoGenerator类,内容如下:

public class MyBatisPlusGenerator {

    //生成文件所在项目路径
    private static String baseProjectPath = "D:\\demo";
    //基本包名
    private static String basePackage = "com.example.demo";
    //作者
    private static String authorName = "authorName";
    //要生成的表名, 为空时默认指定库的所有表
    private static String[] tables = {};
    //table前缀
    private static String prefix = "t_";

    //数据库配置四要素
    private static String driverName = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT&2b8&useSSL=false;
    private static String username = "登录名";
    private static String password = "登录密码";


    public static void main(String[] args) {

        AutoGenerator gen = new AutoGenerator();

        // 数据库配置
        gen.setDataSource(new DataSourceConfig()
                .setDbType(DbType.MYSQL)
                .setDriverName(driverName)
                .setUrl(url)
                .setUsername(username)
                .setPassword(password));

        // 全局配置
        gen.setGlobalConfig(new GlobalConfig()
                .setOutputDir(baseProjectPath + "/src/main/java")//输出目录
                .setFileOverride(true)// 是否覆盖文件
                .setBaseResultMap(true)// XML ResultMap
                .setBaseColumnList(true)// XML columnList
                .setOpen(false)//生成后打开文件夹
                .setAuthor(authorName)
                // 自定义文件命名,注意 %s 会自动填充表实体属性!
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController")
        );

        // 策略配置
        gen.setStrategy(new StrategyConfig()
                .setTablePrefix(prefix)// 表前缀
                .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
                .setInclude(tables) // 需要生成的表
                .setRestControllerStyle(true)
                .setEntityLombokModel(true)
        );

        // 包配置
        gen.setPackageInfo(new PackageConfig()
                .setParent(basePackage)
                .setController("controller")
                .setEntity("entity")
                .setMapper("dao")
                .setService("service")
                .setServiceImpl("service.impl")
                .setXml("mapper")
        );

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

6、运行 MyBatisPlusGenerator.main(),完成。

相关文章

网友评论

    本文标题:SpringBoot使用AutoGenerator类自动生成代码

    本文链接:https://www.haomeiwen.com/subject/ggnyxktx.html