Teach you to quickly generate a key code behind, so little sister and test the addition of chat time

EDITORIAL : I was sailing to the sea , the nickname comes from the name of my girlfriend's name as well. I love technology, open source love, love programming.Technology is open source, knowledge is shared.

This blog is a little summary and record their own learning, if you have the Java , algorithms interested, you can focus on my dynamic, we learn together.

With the knowledge to change the fate, let our family live a better life

Common development, has been knocking yourself entity, mapper, mapper.xml, service, controller, not only troublesome, but also seriously affect the development efficiency.This has got all day, you might as well look at little sister hooked up testing. O (∩_∩) O ~ ha

Here Insert Picture Description

They gnaw As a developer, you should put time and effort on achieving business logic above.

Therefore MybatisPlus provides a code generator, mvc three easily generate the code. After all, these business logic has nothing to do, if you have a database table, there is a ready CRUD code is just fine!

Is not it exciting?
Here Insert Picture Description

AutoGenerator MyBatis-Plus is a code generator, you can quickly generate code Entity, Mapper, Mapper XML, Service, Controller and other modules through AutoGenerator, greatly enhance the development efficiency.

1 Create a Maven project

Note: I created the project is Springboot

pom.xml introduced related dependent jar package

		<!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- velocity 模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>

2. Create a class code generator

public class CodeGenerator {

    @Test
    public void run() {

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("扬帆向海");  // 开发人员
        gc.setOpen(false);  // 生成后是否打开资源管理器
        gc.setFileOverride(false);  // 重新生成时文件是否覆盖
        gc.setServiceName("%sService"); // 去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); // 主键策略
        gc.setDateType(DateType.ONLY_DATE);// 定义生成的实体类中日期类型
        gc.setSwagger2(true); // 开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/库名?serverTimezone=GMT%2B8"); // 驱动连接的url
        dsc.setDriverName("com.mysql.cj.jdbc.Driver"); // 驱动名称
        dsc.setUsername("root"); // 用户名
        dsc.setPassword("root"); // 密码
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("xxx"); // 模块名
        pc.setParent("com.zxy.xxx");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("表名称"); // 表名称
        strategy.setNaming(NamingStrategy.underline_to_camel);// 数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); // 生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); // restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); // url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }
}

3. Run the class code generator

After the completion of the implementation will generate code

This has been generated those tedious code, you just like to focus on the business logic

Eat potato chips and test a small sister chat. . .
Here Insert Picture Description

Published 73 original articles · won praise 794 · views 50000 +

Guess you like

Origin blog.csdn.net/weixin_43570367/article/details/103896958