mybatisplus教案:https://www.kuangstudy.com/bbs/1366329082232467457
mybatisplus文档:https://mp.baomidou.com/guide/
1.快速体验
原来是先pojo,再写mapper接口,在写xml
现在是,先写pojo,在写mapper接口,接口继承BaseMapper
@Repository//代表持久层
public interface UserMapper extends BaseMapper<User> {
//所有CRUD操作都编写完成了,不用像以前一样配置一大堆文件
}
并且在主启动类添加@MapperScan注解
//扫描mapper包下的所有接口
@MapperScan("com.wsk.mapper")
2.日志输出
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3.插入测试和雪花算法.主键策略
pojo层,注解@TableName("um05")标注表名,注解@TableId(value = "ume001", type = IdType.AUTO)标注主键。IdType可选值如下
public enum IdType {
AUTO, //数据库id自增
INPUT, //手动输入
ID_WORKER, //默认的全局唯一id
UUID, //全局唯一id uuid
NONE;//未设置主键
}
默认 : ID_WORKER 全局唯一Id
分布式系统唯一Id生成:https://www.cnblogs.com/haoxinyue/p/5208136.html
5.更新操作
增:Mapper.insert(user);
改:Mapper.updateById(user);
查:Mapper.selectList(null);
6.自动填充
1、在表中增加字段:create_time,update_time,类型datetime
2、pojo中增加
private Date createTime;//驼峰命名
private Date updateTime;
3、增加注解
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
4、编写处理器来处理这个注解!
【
@Slf4j//日志
@Component//丢到springboot里 一定不要忘记把处理器加到Ioc容器中!
public class MyMetaObjectHandler extends MetaObjectHandler {//extends??
@Override//插入时的填充策略
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTIme",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
@Override//更新时的填充策略
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
】
7.乐观锁:https://mp.baomidou.com/guide/interceptor-optimistic-locker.html
1.配置插件
2.在实体类的字段上加上@Version注解
8.查询操作
Mapper.selectById();
Mapper.selectBatchIds(ArrayList)
Mapper.selectByMap(hashmap)
Mapper.selectOne()
9.分页https://mp.baomidou.com/guide/page.html
1.配置拦截器
2.直接使用
Page<> page = new Page(1,5);
userMapper.selectPage(page,wrapper)
page.getRecords().forEach()
10.删除
Mapper.deleteById();
Mapper.deleteBatchIds(ArrayList)
Mapper.deleteByMap(hashmap)
逻辑删除
步骤1: 配置com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig
步骤2: 实体类字段上加上@TableLogic注解
11.wrapper 条件构造器 https://mp.baomidou.com/guide/wrapper.html
wrapper = new QueryWrapper<>();
12.代码生成器
1.代码生成器对象
new AutoGenerator().setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig);
https://blog.csdn.net/qq_37279783/article/details/86690115
在xml使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
配置类
config下的类,加上@configuation注解后,会被当成配置项,可以为启动类分担一些注解。
比如,@MapperScan("com.kuang.mapper")可以移动过来