title
mybatis-plus add and modify
Query single value, multiple primary keys, conditions
@Test
void queryOne() {
// 查询单个user
User user = userMapper.selectById(1);
System.out.println(user);
}
@Test
void queryBatchUser() {
// 根据id查询多个user
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
}
@Test
void queryByMap() {
// 多条件查询
Map<String, Object> conditionParamMap = new HashMap<>();
conditionParamMap.put("name", "Jack");
conditionParamMap.put("id", "2");
List<User> users = userMapper.selectByMap(conditionParamMap);
users.forEach(System.out::println);
}
Paging query
package cn.bitqian.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* mybatis plus配置类
* @author echo lovely
* @date 2020/11/15 09:48
*/
@EnableTransactionManagement // 开启事务
@MapperScan("cn.bitqian.mapper")
@Configuration
public class MyBatisPlusConfig {
// mybatis插件注册
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
// 分页查询
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 添加分页查询到插件中
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
Paging test
@Test
void queryByPaging() {
int pageNum = 1;
int pageSize = 5;
Page<User> page = new Page<>(pageNum, pageSize);
Page<User> userPage = userMapper.selectPage(page, null);
// 遍历 user
userPage.getRecords().forEach(System.out::println);
}
Physical deletion, logical deletion
Physical deletion: is to actually delete the database from the data.
Multiple condition delete
@Test
void deleteById() {
// ctrl shift z
userMapper.deleteById(1327447426226786310l);
}
@Test
void deleteBatchByIds() {
userMapper.deleteBatchIds(
Arrays.asList(1327447426226786306l,
1327447426226786307l));
}
@Test
void deleteBatchByCondition() {
Map<String, Object> conditionParamMap = new HashMap<>();
// 删除name为abc的
conditionParamMap.put("name", "abc");
userMapper.deleteByMap(conditionParamMap);
}
Logical deletion: There is a column specifically corresponding to the column that represents the deletion status. The deletion performed is the modification.
When querying, query the status that has not been changed. Modification is the same as query.
Add fields to the user table
// 逻辑删除字段
@TableLogic
private int deleted;
Configure yaml, the value that will be modified by logical deletion
# log4j std out
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# global 逻辑删除配置
global-config:
db-config:
logic-delete-field: deleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置@TableLogic注解)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
Test delete
// 逻辑删除 实际上是修改
@Test
void deleteByLogic() {
// 被逻辑删除后的值不会被查询,修改到
// SELECT id,name,age,email,version,deleted,gmt_create,gmt_modify FROM user WHERE id=? AND deleted=0
userMapper.deleteById(1l);
}
delete ⇒ update