创建SpringBoot工程:

选择辅助三件套:

再导入MP相关依赖坐标:

<!-- jdbc -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency> <!-- spring-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <!-- spring-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

创建MP的分页配置类:

package cn.echo42.configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author DaiZhiZhou
* @file MP-SpringBoot
* @create 2020-08-06 4:38
*/ @Configuration
@ConditionalOnClass({PaginationInterceptor.class})
public class MybatisPlusConfiguration { @Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
} }

application.yml配置:

#数据源
spring:
datasource:
# 数据源类型
type: org.springframework.jdbc.datasource.DriverManagerDataSource
# 驱动目录
driver-class-name: com.mysql.cj.jdbc.Driver
# 数据库访问url
url: jdbc:mysql://127.0.0.1:3306/oa?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
# 访问账号
username: root
# 访问密码
password: 123456
#配置mybatis-plus
mybatis-plus:
# mapper映射器位置
mapper-locations:
- classpath:mapper/*Mapper.xml
# 全局配置
global-config:
# 数据库配置
db-config:
# 主键类型 自动设置
id-type: auto
banner: true

复制之前Spring整合MP的配置:

User.java、UserMapper.java、UserMapper.xml

注意在启动函数这里注解上@MapperScan

package cn.echo42;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("cn.echo42.mapper")
public class MpSpringbootApplication { public static void main(String[] args) {
SpringApplication.run(MpSpringbootApplication.class, args);
} }

测试类则是SpringBoot现成提供好的:

我们需要自动装配下userMapper,测试方法CV过来就行

package cn.echo42;

import cn.echo42.mapper.UserMapper;
import cn.echo42.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; @SpringBootTest
class MpSpringbootApplicationTests { @Autowired
UserMapper userMapper;
@Test
void contextLoads() {
} @Test
public void mpInsert() {
int insert = userMapper.insert(new User(
null,
"阿伟001",
"133778",
1,
0
));
} @Test
public void mpUpdate() { UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>(); updateWrapper.eq("user_id", 6); // WHERE user_id = 6 // updateWrapper.between("id", 10, 20); WHERE id BETWEEN 10 AND 20 int update = userMapper.update(new User(
null,
"杰哥001",
"133778",
1,
0
), updateWrapper); } @Test
public void mpDelete() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>(); updateWrapper.eq("user_id", 6); // WHERE user_id = 6 // updateWrapper.between("id", 10, 20); WHERE id BETWEEN 10 AND 20 int delete = userMapper.delete(updateWrapper);
} @Test
public void mpQuery() {
User user = userMapper.selectById(3);
System.out.println(user);
} @Test
public void mpQuery2() {
List<Serializable> list = new ArrayList<Serializable>();
list.add(2);
list.add(4); List<User> userList = userMapper.selectBatchIds(list); for (User user : userList) {
System.out.println(user);
}
} @Test
public void mpQuery3() {
Map<String, Object> columnMapping = new HashMap<String, Object>(); columnMapping.put("user_is_del", 0);
columnMapping.put("user_password", "123456"); List<User> userList = userMapper.selectByMap(columnMapping); for (User user : userList) {
System.out.println(user);
}
} @Test
public void mpQuery4() { QueryWrapper<User> userWrapper = new QueryWrapper<User>(); // 假装这是从控制器传递过来的字符串参数
String fromUrlParam = "user"; userWrapper.like(fromUrlParam != null , "user_name", fromUrlParam); // 用来查询符合筛选条件的记录数量
Integer integer = userMapper.selectCount(userWrapper); System.out.println("return rows : " + integer );
} @Test
public void mpQuery5() { IPage<User> page = new Page<User>(1, 5); userMapper.selectPage(page, null); long total = page.getTotal(); System.out.println("TotalRecords : " + total); List<User> list = page.getRecords(); for
(User user : list) {
System.out.println(user);
}
}
}

没打印日志,看不到SQL输出结果,但是测试通过就算有效果了

【Mybatis-Plus】03 SpringBoot整合的更多相关文章

  1. 【SpringBoot】03.SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...

  2. 03.springboot 整合RabbitMQ

    1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. SpringBoot整合mybatis使用pageHelper插件进行分页操作

    SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看 ...

  4. 浅谈Mybatis持久化框架在Spring、SSM、SpringBoot整合的演进及简化过程

    前言 最近开始了SpringBoot相关知识的学习,作为为目前比较流行.用的比较广的Spring框架,是每一个Java学习者及从业者都会接触到一个知识点.作为Spring框架项目,肯定少不了与数据库持 ...

  5. SpringBoot整合系列-PageHelper分页插件

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分页插件PageHelper ...

  6. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  7. mybatis源码学习(四)--springboot整合mybatis原理

    我们接下来说:springboot是如何和mybatis进行整合的 1.首先,springboot中使用mybatis需要用到mybatis-spring-boot-start,可以理解为mybati ...

  8. springboot整合druid、mybatis

    目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...

  9. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  10. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

随机推荐

  1. 使用Wesky.Net.Opentools库,一行代码实现自动解析实体类summary注释信息(可用于数据实体文档的快速实现)

    使用前,需要对你的项目勾选输出api文档文件. 引用Wesky.Net.OpenTools包,保持1.0.11版本或以上.   为了方便,我直接在昨天的演示基础上,继续给实体类添加注释. 昨天的演示文 ...

  2. Lru在Rust中的实现, 源码解析

    LRU(Least Recently Used)是一种常用的页面置换算法,其核心思想是选择最近最久未使用的页面予以淘汰. LRU算法原理 基本思想:LRU算法基于一个假设,即如果一个数据在最近一段时间 ...

  3. VictoriaLogs 要凭什么革了各家日志存储的命

    如果大家对时序指标的存储方案有些了解,那大概率会听过 VictoriaMetrics,VictoriaMetrics 号称 Prometheus 的升级版,在性能和成本方面也确实做得很好,如果是夜莺新 ...

  4. 增补博客 第二篇 python 谢宾斯基三角型字符分形图形输出

    SIZE = int(input())# 输入分割次数 SIZE = SIZE<<3 # 将分割次数转为次数 y = SIZE - 1 # 用来控制列数 while y>=0: fo ...

  5. SOP页面跳转设计 RAS AES加密算法应用跨服务免登陆接口设计

    SOP页面跳转设计 RAS AES加密算法应用跨服务免登陆接口设计 SOP,是 Standard Operating Procedure三个单词中首字母的大写 ,即标准作业程序,指将某一事件的标准操作 ...

  6. Linux chmod -bash: ./xx.sh: Permission denied的解决方案

    Linux -bash: ./xx.sh: Permission denied的解决方案启动tomcat命令:./startup.sh之后提示-bash: ./startup.sh: Permissi ...

  7. python 发起PUT请求,报"Method not Allowed" 和 取返回的报文的内容

    发起请求的时候,默认使用的POST请求方式,导致发起请求,返回[405 Method not Allowed ],检查此更新接口的请求方式为PUT,更改请求方式为PUT PUT接口返回的内容,不能通过 ...

  8. 【iOS】Class对构造简洁代码很有帮助

    (这到底取的是什么标题啊) 首先先看这段代码(有删减) @property (nonatomic, copy)NSMutableArray <NSMutableArray *>*datas ...

  9. python安装pywifi

    1.Windows安装: 在Dos窗口中输入以下命令: pip install pywifi 如果找不到pip命令,那么需要将Python安装文件夹下Scripts文件夹的绝对路径加入环境变量中. 2 ...

  10. .NET Core MVC基础之返回文件类型

    .NET Core MVC基础之返回文件类型 前言 上一篇文章讲了基础的返回类型,这篇文章讲解如何返回文件类型给浏览器下载. 系列文章 .NET MVC基础之页面传值方式 通过图片流来返回图片 返回类 ...