MyBatis-Plus使用(4)-集成SpringBoot
我这里使用的MyBatis-Plus是当前最新的3.2.0版本,
1. 引入需要的jar,基础jar包括:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- MySQL数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
</dependencies>
2. 配置application.yml文件
spring:
# 数据库连接池
datasource:
druid:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/****?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: ****
password: ****
initialSize: 5
minIdle: 10
maxActive: 20
maxWait: 60000 # 配置MyBatis-Plus打印SQL日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3. 根据数据库表生成mapper.xml,dao,service,controller,大家可以参照代码生成器文章:MyBatis-Plus使用(1)-概述+代码生成器
4. 编写启动类
@MapperScan("com.zh.dao")
@ComponentScan(value = "com.zh")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println(" ====== Application Start ======");
}
/**
* MyBatisPlus的分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
以上三个注解简单讲下作用:
@MapperScan是mybatis.spring的注解,作用是指定扫描的dao路径,如果你的每个dao类上都有@Mapper注解,则@MapperScan就可以忽略不写了。
@ComponentScan 是spring注解,用于扫描需要Spring自动装配的文件。
@SpringBootApplication是启动Spring自动装配。
5. 测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class ServiceTest { @Autowired
private UserInfoService userInfoService; @Test
public void pageTest() {
Page<UserInfo> page = new Page<>();
page.setCurrent(1);
page.setSize(10);
page.setOrders(Lists.newArrayList(OrderItem.asc("username"))); QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
wrapper.eq("gender", 0); IPage<UserInfo> result = userInfoService.page(page, wrapper);
System.out.println(JSON.toJSONString(result));
} }
上面的作用是分页查询,根据username排序,查询gender=0的用户信息,生成的SQL如下:
==> Preparing: SELECT COUNT(1) FROM user_info WHERE (gender = ?)
==> Parameters: 0(Integer)
<== Columns: COUNT(1)
<== Row: 2
==> Preparing: SELECT id, address, gender, username FROM user_info WHERE (gender = ?) ORDER BY username LIMIT ?,?
==> Parameters: 0(Integer), 0(Long), 10(Long)
<== Columns: id, address, gender, username
<== Row: 1, 花果山, 0, 孙悟空
<== Row: 2, 高老庄, 0, 猪八戒
<== Total: 2
注意:我用的MyBatis-Plus是3.2.0版本,已经对分页做了优化,大家如果用以前的版本最好升级到3.2.0版本。
因为之前的3.1.2版本生成的SQL语句效率是有问题的,主要问题在count语句,他是把用子查询把所有符合条件的数据都查询出来,再count,真要优化还需要重写page方法,SQL如下:
SELECT COUNT(1) FROM (SELECT id, address, gender, username FROM user_info WHERE (gender = 0)) AS D
@SpringBootApplication
MyBatis-Plus使用(4)-集成SpringBoot的更多相关文章
- 集成Springboot+MyBatis+JPA
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...
- MyBatis初级实战之三:springboot集成druid
OpenWrite版: 欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kuber ...
- Spring Maven项目集成Springboot
Maven管理的Spring项目,准备集成Springboot做接口 1.Springboot对Spring有版本要求 我用的Springboot版本:1.4.5.RELEASE,对应Spring的版 ...
- MyBatis原理,Spring、SpringBoot整合MyBatis
1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- 因此mybatis最好与spring集成起来使用
单独使用mybatis是有很多限制的(比如无法实现跨越多个session的事务),而且很多业务系统本来就是使用spring来管理的事务,因此mybatis最好与spring集成起来使用. spring ...
- (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...
- 实战:docker搭建FastDFS文件系统并集成SpringBoot
实战:docker搭建FastDFS文件系统并集成SpringBoot 前言 15年的时候,那时候云存储还远远没有现在使用的这么广泛,归根结底就是成本和安全问题,记得那时候我待的公司是做建站开发的,前 ...
- 记一次springboot+mybatis+phoenix在代码集成中的坑
场景: 希望使用phoenix做查询服务,给服务端提供接口 设计: 通过springboot做restful的接口发布,通过mybatis做phoenix的sql处理,因此是springboot+my ...
随机推荐
- 内存节省到极致!!!Redis中的压缩表,值得了解...
redis源码分析系列文章 [Redis源码系列]在Liunx安装和常见API 为什么要从Redis源码分析 String底层实现——动态字符串SDS 双向链表都不懂,还说懂Redis? 面试官:说说 ...
- Halcon一维测量1D Measuring解析
一维测量(也叫一维计量或卡尺)的概念非常直观.沿着一个预定的区域(主要是垂直于RIO感兴趣区域的方向) 边缘的位置.这里的边缘为从暗到亮或从亮到暗的过渡. 基于提取的边缘,可以测量零件的尺寸.例如,可 ...
- 数据可视化之powerBI入门 (一)认识PowerBI
来自 https://zhuanlan.zhihu.com/p/64144024 Power BI是什么? Power BI是微软推出的数据分析和可视化工具,我们先来看看微软官方是怎么介绍的: Po ...
- redis(十五):Redis 有序集合(sorted set)(python)
#coding:utf8 import redis r =redis.Redis(host="23.226.74.190",port=63279,password="66 ...
- np.nan is an invalid document, expected byte or unicode string.
ValueError Traceback (most recent call last) <ipython-input-12-1dc462ae8893> in <module> ...
- Angular 懒加载找不到模块问题解决方法
问题: 懒加载无法找到模块 解决办法: 在app-routing.module.ts中引入该模块
- unity-热更-InjectFix(一)
1 C#热更新预备知识 1.1 mono.cecil注入 使用Mono.Cecil实现IL代码注入 注入之后修改dll,新增mdb文件: 注意,待了解参数注释打开会报错: 1.2 InjectFix ...
- 1731: [Usaco2005 dec]Layout 排队布局*
1731: [Usaco2005 dec]Layout 排队布局 题意: n头奶牛在数轴上,不同奶牛可以在同个位置处,编号小的奶牛必须在前面.m条关系,一种是两头奶牛距离必须超过d,一种是两头奶牛距离 ...
- python利用difflib判断两个字符串的相似度
我们再工作中可能会遇到需要判断两个字符串有多少相似度的情况(比如抓取页面内容存入数据库,如果相似度大于70%则判定为同一片文章,则不录入数据库) 那这个时候,我们应该怎么判断呢? 不要着急,pytho ...
- springboot --- 之SSM框架整合
1.pom依赖: 即:spring-boot的基本jar ---- 内置springmvc和spring Thymeleaf jar 热部署 jar ---方便二次加载 ctrl+f9再次编译 Myb ...