视频地址:mybatis-plus
- mybatis-plus基础知识
- 特性
- 快速入门
- mybatis与mybatis-plus的区别
- 配置日志的输出
- 插入测试及雪花算法
- 主键生成策略
- 主键自增
- 更新操作
- 自动填充
- 数据库级别
- 代码级别
- 乐观锁处理
- 查询操作
- 批量查询
- 多条件查询
- 分页查询
- 删除操作
- 逻辑删除
- 性能分析插件
- 条件查询器Wrapper
- 代码自动生成器
- 注意点
1.连接数据库
2.pojo类
3.新建mapper包并新建一个mapper接口,如下:
package com.example.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.example.mybatisplus.pojo.User; // 继承baseMapper接口即可 public interface UserMapper extends baseMapper{ }
4.在主启动类上开启注解扫描
4.测试
package com.example.mybatisplus;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
// 查询所有数据
public void user_Test() {
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
}
mybatis与mybatis-plus的区别
配置日志的输出
插入测试及雪花算法
主键生成策略
默认ID_WORKER (全局唯一id)
这里mybatis-plus使用的是雪花算法
雪花算法
1.需要将数据库的id字段设为自增
2.在实体类id字段上配置注解@TableId(type = IdType.AUTO)
几种不同的主键自增策略
AUTO(0), // id自增
NONE(1), // 未设置主键自增策略
INPUT(2), // 手动输入
ASSIGN_ID(3), //
ASSIGN_UUID(4), //
@Deprecated
ID_WORKER(3), // 默认的全局唯一id
@Deprecated
ID_WORKER_STR(3), // 全局唯一表示法 截取字符串
@Deprecated
UUID(4); // 全局唯一id
更新操作
@Test
public void updateData(){
User user=new User();
user.setUser("yang yang");
user.setAge(23);
user.setPass("123");
user.setSex("man");
user.setId(6);
// 注意:这里的updateById方法的参数是一个对象
int count=userMapper.updateById(user);
System.out.println(count);
}
自动填充
数据库级别
代码级别
编写处理器来处理注解
这样的话,在执行插入和更新操作时,会将数据的updateTime和inserTime字段的值进行自动插入和更新
@Slf4j
@Component
public class MymetaHandler implements metaObjectHandler {
// 插入时的策略
@Override
public void insertFill(metaObject metaObject) {
log.info("start inserfill...");
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
//更新时的策略
@Override
public void updateFill(metaObject metaObject) {
log.info("start updatefill...");
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
乐观锁处理
编写Configuration配置类
@Configuration
public class mybatisplusConfig {
// 注册乐观锁插件
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
}
模拟线程插队的更新操作
User user=userMapper.selectById(8); //version=2
user.setUser("kuangsheng");
// 模拟插队线程的操作
User user1=userMapper.selectById(8);
user1.setUser("kuangsheng111");
userMapper.updateById(user1); //version=3
int count=userMapper.updateById(user); //如果没有乐观锁,则改端代码的结果会 覆盖 插队线程的操作
查询操作
批量查询
List多条件查询userList=userMapper.selectBatchIds(Arrays.asList(1,2,3)); userList.forEach(System.out::println);
Map分页查询map=new TreeMap<>(); map.put("user","李福"); List userList= userMapper.selectByMap(map); System.out.println(userList);
1.配置拦截器组件
// 配置分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
测试
//curren:当前页 size:每页数据条数 Page删除操作page=new Page<>(1,5); userMapper.selectPage(page,null); List userList=page.getRecords(); userList.forEach(System.out::println); }
单个删除
int count=userMapper.deleteById(7);
if (count>0){
System.out.println("删除成功!!!");
}
批量删除
int count=userMapper.deleteBatchIds(Arrays.asList(5,6));
if (count>0){
System.out.println("批量删除成功!!!");
}
通过map删除
Map逻辑删除map=new HashMap<>(); map.put("user","liifu"); map.put("user","cyh"); int count=userMapper.deleteByMap(map); if(count>0){ System.out.println("删除成功!!"); }
1.数据库增加一个deleted字段
2.pojo类增加对应deleted字段
@TableLogic private int deleted; // 0:未删除 1:删除
3.编写Config配置
注意:3.3.1开始不在需要这一步了
在这里插入代码片
4.配置文件(application.yml)
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#逻辑删除
global-config:
db-config:
logic-delete-value: 1 #即删除为1
logic-not-delete-value: 0 #未删除为0
测试
int count=userMapper.deleteById(4);
if (count>0){
System.out.println("删除成功!!!");
}
删除后再次查询(看能否查出该数据)
条件查询器Wrapper平时查询sql语句时可能会遇见一些查询比较耗时的操作。而mybatis-plus提供了性能分析插件(当超过某个时间,他就会停止执行)
适用于写一些执行复杂查询的sql语句
QueryWrapperqueryWrapper=new QueryWrapper<>(); // 查询 age>=21 sex="man" version=1 的信息 queryWrapper.ge("age",21).eq("sex","man").eq("version",1); List userList =userMapper.selectList(queryWrapper); userList.forEach(System.out::println);
区间查询
QueryWrapperwrapper=new QueryWrapper<>(); // 区间查询 wrapper.between("age",21,23); int count=userMapper.selectCount(wrapper); System.out.println(count);
查询结果数(即符合条件的条数)
QueryWrapperwrapper=new QueryWrapper(); wrapper.ge("age",21).eq("version",1); int count=userMapper.selectCount(wrapper); System.out.println("查询的结果数="+count);
模糊查询
QueryWrapperwrapper=new QueryWrapper(); // 查询age中不包含 e 且 user字段中以kuang开头的名字的数据 wrapper.notLike("age","y").likeRight("user","kuang"); List
升序查询数据
// 升序 查询所有数据 QueryWrapper代码自动生成器 注意点wrapper=new QueryWrapper<>(); wrapper.orderByAsc("age"); List userList=userMapper.selectList(wrapper); userList.forEach(System.out::println);
1.要在主启动类上使用@MapperScan注解扫描mapper下的包



