@Test
void contextLoads() {
//参数是一个条件构造器wrapper,先暂时填null
//查询全部用户
List<User> users = userMapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
//批量查询
@Test
public void testById(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
}
//按条件查询
@Test
public void testByMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("name","dai");
map.put("version","2");
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
分页查询:
1.注册组件
//分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
2.使用mybatis-plus自带的page对象
//分页查询
@Test
public void testByLimit(){
Page<User> page = new Page<>(1,4);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
System.out.println(page.getTotal());
}
删除操作:
@Test
public void testDeleteById(){
userMapper.deleteById(11L);
}
//批量删除
@Test
public void testDeleteByMoreId(){
userMapper.deleteBatchIds(Arrays.asList(4,5));
}
//map删除
@Test
public void testDeleteMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("name","Java");
userMapper.deleteByMap(map);
}
逻辑删除:
物理删除:从数据库中直接删除
逻辑删除:数据库中没有删除,而是通过一个变量使它失效 deleted=0 —>deleted=1
1.数据库中增加deleted字段:
2.yml中配置:
#逻辑删除
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
3.实体类中增加字段和注解
//逻辑删除
@TableLogic
private Integer deleted;
4.配置类中添加组件:
//逻辑删除组件
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}
5.测试删除,发现走的是update而不是deleted,并没有真正删除,而是将字段deleted从0变成了1