查询
一般的查询
test
@SpringBootTest
class MybatisPlusFastStartApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void selectMethod(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 3L, 5L));
System.out.println(Arrays.toString(users.toArray()));
}
@Test
void selectMethod2(){
HashMap<String, Object> map = new HashMap<>();//方式二
map.put("name","mmgg");
map.put("age",77);
List<User> users = userMapper.selectByMap(map);
System.out.println(Arrays.toString(users.toArray()));
}
}
复制代码
分页查询
- 需要注入插件
- 创建Configuration类,注入bean
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInnerInterceptor(){//分页插件
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
复制代码
test
@SpringBootTest
class MybatisPlusFastStartApplicationTests {
@Test
void pagination(){
/*
分页构造函数
参数:
current – 当前页
size – 每页显示条数
*/
Page<User> page = new Page<>(2,5);
//返回Page //返回List
List<User> userList = userMapper.selectPage(page, null).getRecords();
System.out.println(Arrays.toString(userList.toArray()));
}
}
复制代码
删除
物理删除
- test
@SpringBootTest
class MybatisPlusFastStartApplicationTests {
@Test
void deleteTest(){
userMapper.deleteById(1383720015207952386L);
//批量删除
userMapper.deleteBatchIds(Arrays.asList(1383394648605704193L,1383394648605704194L, 1383680218527526914L));
}
@Test
void deleteWithMapTest(){
Map<String, Object> map = new HashMap<>();
map.put("name","mmgg");//删除name=mmgg的选手
userMapper.deleteByMap(map);
}
}
复制代码
逻辑删除
- 数据库创建删除标记位字段
- pojo创建相应字段并在字段上使用注解
@TableLogic
- 配置application。yaml
pojo
配置文件
mybatis-plus:
global-config:
db-config:
logic-delete-field: isDeleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
复制代码
test
@SpringBootTest
class MybatisPlusFastStartApplicationTests {
@Test
void logicDelete(){
userMapper.deleteById(1L);
}
}
复制代码
查询是查不出来的
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END