<dd id="t699n"></dd>

  • <em id="t699n"></em>

    1. <dd id="t699n"></dd>

      mybatis-plus-generator實現代碼自動生成器

      時間:2022-10-07 18:50:51 類型:JAVA
      字號:    

      mybatis-plus-generator實現代碼自動生成器

      第一步:引入依賴

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
      
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.5.1</version>
      </dependency>
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-generator</artifactId>
          <version>3.5.3</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <scope>runtime</scope>
      </dependency>

      第二步:applicatio.yml配置

      mybatis-plus:
        configuration:
          log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
          #開啟sql日志
          map-underscore-to-camel-case: true
          # 該配置就是將帶有下劃線的表字段映射為駝峰格式的實體類屬性
      spring:
        datasource:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/data0917?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
          username: root
          password: root

      第三步:創建測試類并執行, 然后在設置的目錄下自動 生成 xml,service,mapper,controller文件

      import com.baomidou.mybatisplus.generator.FastAutoGenerator;
      import com.baomidou.mybatisplus.generator.config.OutputFile;
      import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
      import org.junit.jupiter.api.Test;
      import org.springframework.boot.test.context.SpringBootTest;
      
      import java.util.Collections;
      
      @SpringBootTest
      public class MyBatisPlusGenerator {
          @Test
          public  void fastAutoGeneratorTest(){
              String url="jdbc:mysql://localhost:3306/data0917?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
              FastAutoGenerator.create(url, "root", "root")
                      .globalConfig(builder -> {
                          builder.author("carl") // 設置作者
                                 // .enableSwagger() // 開啟 swagger 模式
                                  .outputDir("F://java//javaProject//batis-genteraor//src//main//java//"); // 指定輸出目錄
                      })
                      .packageConfig(builder -> {
                          builder.parent("com.zhuangzi") // 設置父包名
                                  .moduleName("batisgenteraor") // 設置父包模塊名
                                  .pathInfo(Collections.singletonMap(OutputFile.xml, "F://java//javaProject//batis-genteraor//src//main//resources//mapper//")); // 設置mapperXml生成路徑
                      })
                      .strategyConfig(builder -> {
                          builder.addInclude("users"); // 設置需要生成的表名
                          // .addTablePrefix("t_", "c_"); // 設置過濾表前綴
                      })
                      .templateEngine(new FreemarkerTemplateEngine())
                      // 使用Freemarker引擎模板,默認的是Velocity引擎模板
                      .execute();
      
          }
      }

      1.jpg

      黄网站免费 <