SpringBoot整合MyBatis-Plus实现快速业务功能开发
概览:使用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));
}
}
SpringBoot整合MyBatis-Plus实现快速业务功能开发的更多相关文章
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis之进门篇
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- 001 SringBoot基础知识及SpringBoot整合Mybatis
1.原有Spring优缺点分析 (1)优点 Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品.无需开发重量级的Enterprise J ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置
接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...
- 【SpringBoot系列1】SpringBoot整合MyBatis
前言: 一直看网上说SpringBoot是解锁你的配置烦恼,一种超级快速开发的框架.一直挺想学的,正好最近也有时间,就学了下 这个是SpringBoot整合MyBatis的一个教程,用了阿里的drui ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- SpringBoot 整合 Mybatis + Mysql——XML配置方式
一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...
随机推荐
- spark 学习网站和资料
spark 官网首页 https://spark.apache.org/ spark 官网文档 spark scala API 文档 https://spark.apache.org/docs/lat ...
- Phaser 源码分析
Phaser 一个可重用的同步屏障,功能上与 CyclicBarrier 和 CountDownLatch 类似,但是支持更灵活的使用. 把多个线程协作执行的任务划分为多个阶段,编程时需要明确各个阶段 ...
- Linux基础—saltstack运维工具学习
一.saltstack简介 1.saltstack是什么 系统管理员日常会进行大量的重复性操作,例如安装软件,修改配置文件,创建用户,批量执行命令等,如果主机数量庞大,单靠人工维护实在让人难以忍受. ...
- Oracle数据备份与恢复
为了保证数据库的高可用性,Oracle数据库提供了备份和恢复机制,以便在数据库发生故障时完成对数据库的恢复操作,避免损失重要的数据资源 丢失数据分为:物理丢失:操作系统的数据库主键(数据文件.控机文件 ...
- javaScript 递归 闭包 私有变量
递归 递归的概念 在程序中函数直接或者间接调用自己. 跳出结构,有了跳出才有结果. 递归的思想 递归的调用,最终还是要转换为自己这个函数. 应用 function sum(n){ if(n == ...
- 32 kill不掉的语句
32 kill不掉的语句 在mysql中有两个kill命令:一个是kill query+线程id,表示终止这个线程正在执行的语句:一个是kill connection+线程id,缺省connectio ...
- 将查询列表内容保存到excel表格中,并保存到相应的盘中
1.先导入相应的jar包 2.一个小的Demo测试[实体类+测试类:保存excel的方法] Student实体类 public class Student{ private int id; priva ...
- 浅谈格雷码(Grey Code)在信息学竞赛中的应用
1.格雷码的概念 1.性质 格雷码(Grey Code),又叫循环二进制码或反射二进制码,是一种编码方式,它的基本特点是任意两个相邻的格雷码只有一位二进制数不同. 常用的二进制数与格雷码间的转换关系如 ...
- Java使用POI读取和写入Excel指南(转)
做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃,这里基于最新的Apache POI 4.0 ...
- vue 跳转页面返回时tab状态有误的解决办法
一.前言 最近在做新vue项目的时候遇到了一个问题,就是tab间的切换没有问题,当跳转到其他页面时,且这个页面并非子路由,再用浏览器的返回按钮返回首页时,tab的active始终指向默认的第一个选项. ...