概览:使用MybatisPlus和它的代码生成整合SpringBoot可以实现快速的业务功能开发,具体步骤如下
一、添加依赖
<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 -->
二、配置Mybaits-Plus
## 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
三、配置代码生成器 MysqlGenerator
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();
} }
四、生成代码,实现相应的Controller
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));
}
}
到此为止就实现了一个完整业务的增删改查了,是不是觉得挺简单哉,那就赶快自己动手试试吧!
需要完整源码的见下面GIthub传送门: