本章导学:
我们之前调用MP的方法时,参数里往往会传一个null,Wrapper就是接收null的对象
我们点进去BaseMapper里看看
这里面是MP为我们生成的一些方法,有些方法里有queryWrapper和updateWrapper参数。我们点进去Wrapper看看
可以看到,QueryWrapper和UpdateWrapper是AbstractWrapper的子类,而AbstractWrapper是Wrapper的子类。
//查询brand_name为白白白白不的数据
//等同于where brand_name = "白白白白不"'
@Test
void test01(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.eq("brand_name","白白白白不");
Brand brand = mqDao.selectOne(brandQueryWrapper);
System.out.println(brand);
}
生成的SQL如下
//查询ordered字段在10-50之间的数据
//等同于where ordered between 10 and 50;
@Test
void test02(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.between("ordered",10,50);
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
生成的SQL如下
//模糊查询
//等同于where brand_name like concat('%',"松鼠",'%');
@Test
void test03(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.like("brand_name","松鼠");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
生成的SQL如下
QueryWrapper也支持链式调用,我们可以接多个条件
//Wrapper可以链式调用
//查询名字包含松鼠,ordered字段在10-50之间,且description不为空的数据
@Test
void test04(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.like("brand_name","小米")
.between("ordered",10,50)
.isNotNull("description");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
生成的SQL如下
//查询指定字段
@Test
void test08() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
//用select方法把需要查询的字段添加进去
brandQueryWrapper.select("brand_name","ordered");
List
生成的SQL如下
//子查询,查询ID小于10的数据
@Test
void test09() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
//字段名,子查询语句
brandQueryWrapper.inSql("id","select id from tb_brand where id <= 10");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
生成的SQL如下
//查询全部信息,按名称降序排序,若相同,按id升序排序
@Test
void test05() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.orderByDesc("brand_name")
.orderByAsc("id");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
生成的SQL如下
//删除description为空的数据
@Test
void test06() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.isNull("description");
int delete = mqDao.delete(brandQueryWrapper);
System.out.println("result:"+delete);
}
生成的SQL如下
//修改名字包含松鼠,ordered字段在为5
@Test
void test07() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.like("brand_name","松鼠")
.eq("ordered",5);
//模拟前台传来的数据封装到实体类
Brand brand = new Brand();
brand.setBrandName("老虎");
brand.setOrdered(666);
int result = mqDao.update(brand, brandQueryWrapper);
System.out.println("result:"+result);
}
这里的修改是动态SQL语句,不会把未设置的字段值修改为空
SQL如下
使用UpdateWrapper进行修改
//updateWrapper进行修改,修改brand_name为老虎的信息
@Test
void test10() {
UpdateWrapper updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("brand_name","老虎");
//设置修改的内容
updateWrapper.set("brand_name","猎豹");
//这里我们没有实体类对象了,填null就好
int update = mqDao.update(null, updateWrapper);
System.out.println(update);
}
生成的SQL如下
测试代码如下
package com.brrbaii;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.brrbaii.dao.MqDao;
import com.brrbaii.pojo.Brand;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class WrapperTest {
@Autowired
private MqDao mqDao;
//查询brand_name为白白白白不的数据
//等同于where brand_name = "白白白白不"'
@Test
void test01(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.eq("brand_name","白白白白不");
Brand brand = mqDao.selectOne(brandQueryWrapper);
System.out.println(brand);
}
//查询ordered字段在10-50之间的数据
//等同于where ordered between 10 and 50;
@Test
void test02(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.between("ordered",10,50);
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
//模糊查询
//等同于where brand_name like concat('%',"松鼠",'%');
@Test
void test03(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.like("brand_name","松鼠");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
//Wrapper可以链式调用
//查询名字包含松鼠,ordered字段在10-50之间,且description不为空的数据
@Test
void test04(){
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.like("brand_name","小米")
.between("ordered",10,50)
.isNotNull("description");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
//查询全部信息,按名称降序排序,若相同,按id升序排序
@Test
void test05() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.orderByDesc("brand_name")
.orderByAsc("id");
mqDao.selectList(brandQueryWrapper).forEach(System.out::println);
}
//删除description为空的数据
@Test
void test06() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.isNull("description");
int delete = mqDao.delete(brandQueryWrapper);
System.out.println("result:"+delete);
}
//修改名字包含松鼠,ordered字段在为5
@Test
void test07() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
brandQueryWrapper.like("brand_name","松鼠")
.eq("ordered",5);
//模拟前台传来的数据封装到实体类
Brand brand = new Brand();
brand.setBrandName("老虎");
brand.setOrdered(666);
int result = mqDao.update(brand, brandQueryWrapper);
System.out.println("result:"+result);
}
//查询指定字段
@Test
void test08() {
QueryWrapper brandQueryWrapper = new QueryWrapper();
//用select方法把需要查询的字段添加进去
brandQueryWrapper.select("brand_name","ordered");
List