MybatisPlus多中Lambda查询更新
package com.lpinfo.shop.module;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpinfo.shop.LpinfoShopApplicationTests;
import com.lpinfo.shop.plateform.module.dto.ModuleTestDto;
import com.lpinfo.shop.plateform.module.entity.ModuleTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lpinfo.shop.plateform.module.service.ModuleTestService;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
//@Transactional
//@Rollback(true)
@Component
public class ModuleTestServiceSuit extends LpinfoShopApplicationTests {
@Autowired
private ModuleTestService service;
@Test
public void testSuitCase() {
LambdaQueryWrapper<ModuleTest> wrapper1 = new LambdaQueryWrapper<>();
wrapper1.eq(ModuleTest::getId, 1);
List<ModuleTest> list = service.list(wrapper1);
log.info("1.Wrapper查询,{}", JSON.toJSON(list));
LambdaQueryWrapper<ModuleTest> wrapper2 = new LambdaQueryWrapper<ModuleTest>();
wrapper2.inSql(ModuleTest::getId,"select id from module_test");
List<ModuleTest> list2 = service.list(wrapper2);
log.info("嵌套子查询,{}", JSON.toJSON(list2));
/**
* 基础数据
* id name phone mstime rztime sort creater ctime
* 1 张一 188111 2020-08-03 22:26:46 2020-08-03 1 admin 2020-08-03 21:58:35
* 2 张二 188222 2020-08-07 22:26:51 2020-08-14 2 admin 2020-08-03 21:58:54
* 3 张三 188333 2020-08-06 22:26:57 2020-08-20 3 admin 2020-08-19 22:25:33
* 4 张四 188444 2020-08-21 22:27:00 2020-08-17 4 test 2020-08-20 22:25:30
* 5 张五 188555 2020-08-13 22:27:03 2020-08-20 5 test 2020-08-21 22:25:38
* 6 张六 188666 2020-08-09 22:27:06 2020-08-14 6 test 2020-08-27 22:25:27
* 7 张七 188777 2020-08-14 22:27:09 2020-08-08 7 admin 2020-08-26 22:25:24
* 8 张八 188888 2020-08-14 22:27:14 2020-08-29 8 admin 2020-08-20 22:25:41
* 9 张九 188999 2020-08-15 22:27:19 2020-08-06 9 admin 2020-08-14 22:25:44
* 10 张十 188000 2020-08-19 22:27:22 2020-08-08 10 admin 2020-08-21 22:25:48
*/
Page<ModuleTest> page = new Page<>(1, 5);
LambdaQueryWrapper<ModuleTest> wrapper3 = new LambdaQueryWrapper<ModuleTest>();
String nameAndphone="张";
if (!StringUtils.isEmpty(nameAndphone)){
wrapper3.and(wrapper -> wrapper.like(ModuleTest::getName, nameAndphone).or().like(ModuleTest::getPhone, nameAndphone));
}
String createUser="test";
if (!StringUtils.isEmpty(createUser)){
wrapper3.and(wrapper -> wrapper.eq(ModuleTest::getCreater, createUser));
}
String startTime ="2020-09-01";
String endTime ="2020-09-09";
if(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime)){
wrapper3.between(ModuleTest::getRztime,startTime,endTime);
}
根据不同的排序条件进行排序
String createtime="sdasd";
if(!StringUtils.isEmpty(createtime)){
//wrapper3.orderByDesc(ModuleTest::getCtime);
wrapper3.orderByAsc(ModuleTest::getCtime);
// wrapper3.orderByAsc(ModuleTest::getSort);
// wrapper3.orderByDesc(ModuleTest::getSort);
}
///执行的SQL
//SELECT id,name,phone,mstime,rztime,sort,creater,ctime FROM module_test WHERE (( (name LIKE ? OR phone LIKE ?) ) AND ( (creater = ?) ) AND rztime BETWEEN ? AND ?) ORDER BY ctime ASC LIMIT ?,?
IPage<ModuleTest> userIPage = service.page(page, wrapper3);
log.info("\n分页查询,{}", JSON.toJSON(userIPage));
///查询到的结果
/**
* 分页查询,{"current":1,"total":1,"pages":1,"size":5,"records":[{"rztime":"2020-09-05","phone":"188111","creater":"test","mstime":"2020-08-03T22:26:46","name":"张一","ctime":"2020-08-03T21:58:35","id":1,"sort":1}],"searchCount":true,"orders":[]}
*/
更新
LambdaUpdateWrapper<ModuleTest> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(ModuleTest::getName, "2324");
updateWrapper.eq(ModuleTest::getId, 1);
service.update(updateWrapper);
}
}