溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot中如何使用tk.mybatis代碼生成器

發布時間:2021-07-16 10:39:12 來源:億速云 閱讀:523 作者:chen 欄目:大數據
# SpringBoot中如何使用tk.mybatis代碼生成器

## 一、tk.mybatis簡介

tk.mybatis是一個基于MyBatis的輕量級通用Mapper框架,它通過提供通用Mapper接口和代碼生成器,可以大幅減少MyBatis開發中的重復代碼。其中代碼生成器模塊能夠根據數據庫表結構自動生成:
- 實體類(Entity)
- Mapper接口
- XML映射文件

## 二、環境準備

### 1. 添加依賴
在SpringBoot項目的`pom.xml`中添加以下依賴:

```xml
<!-- tk.mybatis核心包 -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>

<!-- 代碼生成器 -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-generator</artifactId>
    <version>1.1.5</version>
    <scope>provided</scope>
</dependency>

<!-- 模板引擎 -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>

2. 數據庫配置

確保application.yml中已配置數據源:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_db?useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

三、代碼生成器配置

1. 創建Generator配置類

test目錄下新建GeneratorRunner.java

public class GeneratorRunner {
    public static void main(String[] args) {
        // 代碼生成器配置
        AutoGenerator generator = new AutoGenerator();
        
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        gc.setAuthor("YourName");
        gc.setOpen(false);
        generator.setGlobalConfig(gc);

        // 數據源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/your_db");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        generator.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.example.demo");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        generator.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setInclude("user", "order"); // 需要生成的表名
        generator.setStrategy(strategy);

        generator.execute();
    }
}

2. 關鍵配置說明

配置項 說明
setOutputDir 指定生成文件的輸出目錄
setInclude 指定需要生成的表(支持多表,用逗號分隔)
setEntityLombokModel 是否使用Lombok注解(推薦true)
setNaming 數據庫字段到實體屬性的命名策略(推薦下劃線轉駝峰)

四、運行生成器

  1. 右鍵運行GeneratorRunner.main()方法
  2. 控制臺出現生成成功提示后,檢查目標目錄:
    
    src/main/java
    └── com
       └── example
           └── demo
               ├── entity  # 實體類
               ├── mapper # Mapper接口
               └── xml    # XML映射文件
    

五、生成后使用

  1. 在啟動類添加Mapper掃描注解:
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. 在Service中注入Mapper:
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    
    public User getById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }
}

六、注意事項

  1. 表必須有主鍵:tk.mybatis要求每個表必須定義主鍵
  2. 字段命名規范:建議使用下劃線命名法(如:user_name)
  3. 生成前備份:重復生成會覆蓋已有文件
  4. 自定義模板:可通過修改src/main/resources/generator下的.ftl文件定制生成模板

七、總結

通過tk.mybatis代碼生成器,我們可以在SpringBoot項目中快速生成基礎CRUD代碼,將開發效率提升50%以上。相比傳統MyBatis開發方式,減少了大量重復工作,讓開發者更專注于業務邏輯實現。 “`

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女