package com.mybatisplus;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatisplus.mapper.UserMapper;
import com.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
class MybatisPlusApplicationTests {
//继承自BaseMapper,所有方法都来自父类,也可以编写自己的扩展方法
@Resource
private UserMapper userMapper;
//快速使用
@Test
void contextLoads() {
//通过id查询用户
// System.out.println(userMapper.selectById(4L));
//查询全部用户,其参数是一个条件构造器
userMapper.selectList(null).forEach(System.out::println);
}
//测试插入数据
@Test
void testInsert() {
User user = new User()
.setName("小黎")
.setAge(72)
.setEmail("1933@qq.com");
userMapper.insert(user);
}
//测试更新数据
@Test
void testUpdate() {
User user = new User()
.setId(1698244309027500042L)
.setName("王五")
.setAge(8)
.setEmail("456@gmail.com");
int update = userMapper.updateById(user);
System.out.println(update);
}
//测试乐观锁成功案例
@Test
void testOptimisticLockSuccess() {
//查询用户信息
User user = userMapper.selectById(1L);
//修改用户信息
user.setAge(34).setName("丙丁");
userMapper.updateById(user);
}
//测试乐观锁失败案例
@Test
void testOptimisticLockFail() {
//线程1
User user = userMapper.selectById(1L);
user.setAge(34).setName("丙丁11111");
//模拟另外一个线程执行插队操作
User user2 = userMapper.selectById(1L);
user2.setAge(34).setName("丙丁22222");
userMapper.updateById(user2);
//自旋锁多次提交
userMapper.updateById(user);//如果没有乐观锁,就会覆盖插队线程的值
}
//测试通过id查询
@Test
void testSelectById() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
//批量查询多个用户
@Test
void testSelectBatch() {
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
users.forEach(System.out::println);
}
//条件查询map
@Test
void testSelectByMaps() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "丙丁22222");
map.put("age", 28);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
//测试分页查询
@Test
void testPage() {
//第一个参数:当前页
//第二个参数:页面的大小
Page<User> page = new Page<>(2, 3);
Page<User> userPage = userMapper.selectPage(page, null);
userPage.getRecords().forEach(System.out::println);
}
//测试删除
@Test
void testDelete() {
userMapper.deleteById(1698244309027500042L);
}
//测试批量删除
@Test
void testDeleteBatch() {
userMapper.deleteBatchIds(Arrays.asList(1L, 2L, 3L));
}
//测试条件删除
@Test
void testDeleteByMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "小王");
userMapper.deleteByMap(map);
//再查询逻辑删除的数据
userMapper.selectByMap(map).forEach(System.out::println);
}
@Test
void testLogicDelete() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "小王");
userMapper.deleteByMap(map);
//再查询逻辑删除的数据
userMapper.selectByMap(map).forEach(System.out::println);
}
}
![avatar](https://profile-avatar.csdnimg.cn/3332171d6ed34d6a81b820f4461229fa_m0_50281408.jpg!1)
笑的像个child
- 粉丝: 2398
- 资源: 6
最新资源
- 基于断裂力学理论及COMSOL模拟的横观各向同性介质水力压裂扩展:考虑裂纹形态变化、单边拉伸剪切及地应力影响的相场法模拟研究,基于COMSOL软件的水力压裂模拟研究:从多裂纹扩展到断裂力学的破坏分析
- 基于断裂力学理论与Comsol软件模拟横观各向同性介质水力压裂裂纹扩展模型及多工况下裂纹扩展的相场法数值模拟研究,基于断裂力学理论与comsol软件的横观各向同性介质水力压裂裂纹扩展模型研究及相场法数
- Springboot+vue的敬老院管理系统。Javaee项目,springboot vue前后端分离项目。
- 《COMSOL模拟多裂纹水力压裂扩展:拉伸与压缩破坏下的应用研究》,探究comsol多裂纹水力压裂扩展机制:拉伸与压缩下的破坏行为分析,comsol多裂纹水力压裂扩展,可以实现拉伸和压缩下的破坏
- Springboot+vue的新冠病毒密接者跟踪系统(有报告)。Javaee项目,springboot vue前后端分离项目
- 经典激光技术:COMSOL模拟下的激光烧蚀、融覆与选区激光融化-探究熔池内复杂输运现象及热行为对凝固组织与性能的影响,深度解析激光直接沉积过程中的复杂输运现象与热行为影响-以COMSOL激光烧
- 智慧社区解决方案PPT(45页).pptx
- 智慧社区示范区智能化系统详细规划设计方案PPT(43页).pptx
- 智慧社区解决方案-8Word(152页).docx
- 智慧社区物联网解决方案PPT(31页).pptx
- 智慧社区解决方案-5PPT(26页).pptx
- 智慧社区解决方案-7PPT(36页).pptx
- 35页-智慧社区解决方案.pdf
- 智慧社区项目方案PPT(38页).pptx
- 基于Springboot的智慧社区居家养老健康管理系统(有报告)。Javaee项目,springboot项目。
- 38页-智能智慧社区解决方案.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)