MyBatisPlus是基于MyBatis框架基础上开发的增强型工具,目的: 简化开发,提高开发效率
1.MP环境搭建
SpringBoot+MP整合
1.创建jar项目,引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.创建User和Mapper
3.修改application.yml文件
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db10?serverTimezone=UTC
username: root
password: root
main:
banner-mode: off #隐藏启动控制台logo
# 显示sql语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印sql
global-config:
banner: false # 隐藏启动控制台logo
4.写测试类
package com.doyens.test;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.dao.UserMapper;
import com.doyens.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class MPTestApplication {
@Autowired
private UserMapper userMapper;
@Test
public void testSave(){
User user = new User();
user.setName("chtjava");
user.setAge(12);
user.setPassword("123123");
user.setTel("4009998888");
userMapper.insert(user);
}
@Test
public void testDel(){
userMapper.deleteById(1746708438365769730L);
}
@Test
public void updateUser(){
User user = new User();
user.setId(1L);
user.setName("cctv");
user.setAge(12);
user.setPassword("123123");
user.setTel("4009998888");
userMapper.updateById(user);
}
@Test
public void testGetAll(){
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
}
//查询单条
@Test
public void testById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
//分页
@Test
public void testQueryPage(){
//第一参数是 当前页 第二个参数 每页显示的条数
IPage page = new Page(2,5);
// 分页sql limit (当前页码-1)*pagesize ,5
// 第一个参数是page接口 第二个参数是查询条件
userMapper.selectPage(page,null);
System.out.println("当前页:"+page.getCurrent());
System.out.println("每页显示的条数"+page.getSize());
System.out.println("一共多少页: "+page.getPages());
System.out.println("一共多少条数据" + page.getTotal());
System.out.println("展示的数据"+page.getRecords());
}
//添加查询条件
@Test
public void testQueryBy(){
//第一种写法
/* QueryWrapper qw = new QueryWrapper();
qw.lt("age",18);
List<User> list = userMapper.selectList(qw);
System.out.println(list);*/
//第二种查询条件 使用 lambda方式
/* QueryWrapper<User> qw = new QueryWrapper<>();
qw.lambda().lt(User::getAge,18);
List<User> list = userMapper.selectList(qw);
System.out.println(list);*/
//第三种查询条件 使用 lambda方式
/*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,18);
lqw.gt(User::getAge,13);
List<User> list = userMapper.selectList(lqw);
System.out.println(list);*/
//多条件链式写法
/*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,18).gt(User::getAge,13);
List<User> list = userMapper.selectList(lqw);
System.out.println(list);*/
//条件变成or关系
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,18).or().gt(User::getAge,13);
List<User> list = userMapper.selectList(lqw);
System.out.println(list);
}
}
5.实现分页查询的时候需要写拦截器
6.条件查询
1.可以使用: QueryWrapper
2.可以使用lambda查询
//第三种查询条件 使用 lambda方式
/*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,18);
lqw.gt(User::getAge,13);
List<User> list = userMapper.selectList(lqw);
System.out.println(list);*/
//多条件链式写法
/*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,18).gt(User::getAge,13);
List<User> list = userMapper.selectList(lqw);
System.out.println(list);*/
7.组合查询
//条件变成or关系
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,18).or().gt(User::getAge,13);
List<User> list = userMapper.selectList(lqw);
System.out.println(list);
8.对null值处理
//如果是null的话不要添加查询条件
if (null !=uq.getAge()){
lqw.gt(User::getAge,uq.getAge());
}*//*
//MP自带的判断是否为null
lqw.lt(null !=uq.getAge2(),User::getAge,uq.getAge2());
lqw.gt(null !=uq.getAge(),User::getAge,uq.getAge());
List<User> list = userMapper.selectList(lqw);
System.out.println(list);
9.查询投影
规定查询的字段
第一种:
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.select(User::getId,User::getName);
第二种:
QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("id","name","age");
第三种: 可以给添加count(*)
lqw.select("count(*) as count,tel");
//做分组
lqw.groupBy("tel");
//查询返回的数据
List<Map<String, Object>> maps = userMapper.selectMaps(lqw);
System.out.println(maps);
10.查询条件设置
//范围查询:
// lt 小于 le 小于等于 gt大于 ge 大于等于 between and
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
// lqw.eq(User::getName,"cctv").eq(User::getPassword,"123123");
lqw.between(User::getAge,10,20);
List<User> userList = userMapper.selectList(lqw);
System.out.println(userList);
//eq like likeRight likeLeft
//模糊匹配
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
//lqw.like(User::getName,"j");
// lqw.likeRight(User::getName,"j");
lqw.likeLeft(User::getName,"j");
List<User> userList = userMapper.selectList(lqw);
System.out.println(userList);