文章目錄
前言
一、引入依賴
二、使用步驟
1.創(chuàng)建一個類(例如CodeGenerator)
2.編輯生成模板
三、一鍵生成代碼
?結(jié)尾
前言
在SpringBoot中,通過引入MyBatis-Plus 實現(xiàn)數(shù)據(jù)庫代碼生成器,我還寫好了一些模板方法,可一鍵生成。
注意
適用版本:mybatis-plus-generator 3.5.1 及其以上版本
一、引入依賴
在pom.xml中引入代碼生成器、以及代碼生成器中默認使用的velocity引擎
<!-- 代碼生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
二、使用步驟
1.創(chuàng)建一個類(例如CodeGenerator)
把以下代碼放到一個方法中。
FastAutoGenerator.create("url", "username", "password")
.globalConfig(builder -> {
builder.author("baomidou") // 設(shè)置作者
.enableSwagger() // 開啟 swagger 模式
.fileOverride() // 覆蓋已生成文件
.outputDir("D://"); // 指定輸出目錄
})
.packageConfig(builder -> {
builder.parent("com.baomidou.mybatisplus.samples.generator") // 設(shè)置父包名
.moduleName("system") // 設(shè)置父包模塊名
.pathInfo(Collections.singletonMap(OutputFile.xml, "D://")); // 設(shè)置mapperXml生成路徑
})
.strategyConfig(builder -> {
builder.addInclude("t_simple") // 設(shè)置需要生成的表名
.addTablePrefix("t_", "c_"); // 設(shè)置過濾表前綴
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默認的是Velocity引擎模板
.execute();
我的代碼如下(示例):
?
默認使用的Velocity引擎模板,所以就把使用Freemarker代碼注釋掉了
2.編輯生成模板
官方的模板位置如下:
將controller.java.vm復(fù)制到resources/templates下
?可直接使用默認沒有功能的模板,也可復(fù)制我的,編寫了一些增刪改查、分頁的模板
package ${package.Controller};
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
/**
* <p>
* $!{table.comment} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
@Resource
private ${table.serviceName} ${table.entityPath}Service;
// 新增或者更新
@PostMapping
public boolean save(@RequestBody ${entity} ${table.entityPath}) {
return ${table.entityPath}Service.saveOrUpdate(${table.entityPath});
}
// 刪除
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
return ${table.entityPath}Service.removeById(id);
}
// 查詢所有數(shù)據(jù)
@GetMapping
public List<${entity}> findAll() {
return ${table.entityPath}Service.list();
}
// 根據(jù)id查詢
@GetMapping("/{id}")
public ${entity} findOne(@PathVariable Integer id) {
return ${table.entityPath}Service.getById(id);
}
// 分頁查詢
@GetMapping("/page")
public Page<${entity}> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize));
}
}
#end
此處的增刪改查調(diào)用了mybatis-plus的功能,不懂的小伙伴可以配置學(xué)習(xí)一下
三、一鍵生成代碼
只需在CodeGenerator.java中運行一下代碼
?
前端控制器UserController、數(shù)據(jù)庫實體類User、service、UserMapper就一件生成好啦。
?我們自定義的功能也在UserController中啦。
文章來源:http://www.zghlxwxcb.cn/news/detail-433937.html
?結(jié)尾
代碼生成器可以一鍵省去我們一半花在創(chuàng)建和寫SQL代碼的時間,不過平時小伙伴不急著做項目,也可以手動寫寫,提高SQL語言能力。文章來源地址http://www.zghlxwxcb.cn/news/detail-433937.html
到了這里,關(guān)于在SpringBoot使用MyBatis-Plus代碼生成器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!