概覽:使用MybatisPlus和它的代碼生成整合SpringBoot能夠實現快速的業務功能開發,具體步驟以下前端
1、添加依賴java
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- Mybait-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> <!-- Mybait-Plus代碼生成 begin --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- Mybait-Plus代碼生成 end -->
2、配置Mybaits-Plusmysql
## MyBatis-Plus(若是用MyBatis只需去掉plus後綴) mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=cn.bounter.mybatisplus.entity mybatis-plus.configuration.map-underscore-to-camel-case=true mybatis-plus.configuration.use-column-label=true
3、配置代碼生成器 MysqlGeneratorgit
package cn.bounter.mybatisplus; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * <p> * mysql 代碼生成器演示例子 * </p> * */ public class MysqlGenerator { /** * <p> * 讀取控制檯內容 * </p> */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("請輸入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("請輸入正確的" + tip + "!"); } /** * RUN THIS */ public static void main(String[] args) { // 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); // String modelName = ""; gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("simon"); gc.setOpen(false); gc.setFileOverride(true);// 是否覆蓋同名文件,默認是false gc.setActiveRecord(true);// 不須要ActiveRecord特性的請改成false gc.setEnableCache(false);// XML 二級緩存 gc.setBaseResultMap(false);// XML ResultMap gc.setBaseColumnList(false);// XML columList mpg.setGlobalConfig(gc); // 數據源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://47.98.151.249:3306/bounter?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("bounter"); dsc.setPassword("111111@Bounter"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName(scanner("模塊名")); pc.setParent("cn.bounter.mybatisplus"); pc.setController("controller"); pc.setEntity("entity"); pc.setMapper("dao"); pc.setService("service"); pc.setServiceImpl("service.impl" ); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 若是模板引擎是 freemarker String templatePath = "/templates/mapper.xml.ftl"; // 自定義輸出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定義配置會被優先輸出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸出文件名 , 若是你 Entity 設置了先後綴、此處注意 xml 的名稱會跟着發生變化!! return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setInclude(scanner("表名,多個英文逗號分割").split(",")); // strategy.setTablePrefix("t_"); strategy.setRestControllerStyle(true); mpg.setStrategy(strategy); // 選擇 freemarker 引擎須要指定以下加,注意 pom 依賴必須有! mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
4、生成代碼,實現相應的Controllergithub
package cn.bounter.mybatisplus.controller; import cn.bounter.mybatisplus.common.PageReq; import cn.bounter.mybatisplus.entity.Bounter; import cn.bounter.mybatisplus.service.IBounterService; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.api.R; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * <p> * 前端控制器 * </p> * * @author simon * @since 2019-05-09 */ @RestController @RequestMapping("/api/bounter") public class BounterController { @Autowired private IBounterService bounterService; /** * 列表&搜索 * @param pageReq * @return */ @PostMapping("/list") public R<?> list(@RequestBody PageReq<Bounter> pageReq) { Bounter bounter = pageReq.getReqObj() == null ? new Bounter() : pageReq.getReqObj(); return R.ok(bounterService.page(new Page<>(pageReq.getPageNum(), pageReq.getPageSize()), Wrappers.<Bounter>lambdaQuery().eq(Bounter::getId, bounter.getId()))); } /** * 詳情 * @param id * @return */ @GetMapping("/{id}") public R<?> detail(@PathVariable Long id) { return R.ok(bounterService.getById(id)); } /** * 新增 * @param bounter * @return */ @PostMapping public R<?> save(@RequestBody Bounter bounter) { return R.ok(bounterService.save(bounter)); } /** * 修改 * @param bounter * @return */ @PutMapping public R<?> update(@RequestBody Bounter bounter){ return R.ok(bounterService.updateById(bounter)); } /** * 刪除 * @param id * @return */ @DeleteMapping("/{id}") public R<?> delete(@PathVariable Long id) { return R.ok(bounterService.removeById(id)); } }