MyBatis-Plus功能学习
- MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
1,安装配置
-
拥有 Java 开发环境以及相应 IDE 熟悉 Spring Boot 熟悉 Maven
- 创建springboot项目,在pom.xml中引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
- 在application.properties中添加配置
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
- 创建Mybatis-Plus配置类以及注解**@MapperScan**
@Configuration
@MapperScan("com.example.springboot.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
2,CRUD接口
- 通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用
get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆, - 泛型
T 为任意实体对象 - 建议如果存在自定义通用 Service 方法的可能,请创建自己的
IBaseService 继承 Mybatis-Plus 提供的基类 - 对象
Wrapper 为 条件构造器
Save方法(插入记录)
boolean save(T entity);
boolean saveBatch(Collection<T> entityList);
boolean saveBatch(Collection<T> entityList, int batchSize);
SavaOrUpdate(插入或更新,查询目标数据是否存在,若存在则更新,否则插入)
boolean saveOrUpdate(T entity);
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
boolean saveOrUpdateBatch(Collection<T> entityList);
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
Remove(删除数据)
boolean remove(Wrapper<T> queryWrapper);
boolean removeById(Serializable id);
boolean removeByMap(Map<String, Object> columnMap);
boolean removeByIds(Collection<? extends Serializable> idList);
Update(更新数据)
boolean update(Wrapper<T> updateWrapper);
boolean update(T updateEntity, Wrapper<T> whereWrapper);
boolean updateById(T entity);
boolean updateBatchById(Collection<T> entityList);
boolean updateBatchById(Collection<T> entityList, int batchSize);
Get(查询数据)
T getById(Serializable id);
T getOne(Wrapper<T> queryWrapper);
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
Map<String, Object> getMap(Wrapper<T> queryWrapper);
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
List(批量查询)
List<T> list();
List<T> list(Wrapper<T> queryWrapper);
Collection<T> listByIds(Collection<? extends Serializable> idList);
Collection<T> listByMap(Map<String, Object> columnMap);
List<Map<String, Object>> listMaps();
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
List<Object> listObjs();
<V> List<V> listObjs(Function<? super Object, V> mapper);
List<Object> listObjs(Wrapper<T> queryWrapper);
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
Page(分页查询)
IPage<T> page(IPage<T> page);
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
IPage<Map<String, Object>> pageMaps(IPage<T> page);
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
Count(查询数据量)
int count();
int count(Wrapper<T> queryWrapper);
3,分页插件
- 当需要将数据根据一定数量分成指定页数时,需要对数据进行分页
使用sql语句实现分页
controller.xml
@GetMapping("/page")
public Map<String , Object> findPage(@RequestParam Integer pageNumber,
@RequestParam Integer pageSize,
@RequestParam String name){
pageNumber=(pageNumber-1)*pageSize;
List<Sign> data=signService.selectPage(pageNumber,pageSize,name);
Integer total = signService.selectTotal(name);
Map<String , Object> res=new HashMap<>();
res.put("data",data);
res.put("total",total);
return res;
}
mapper.xml
<select id="selectPage" resultType="com.example.springboot.entity.User">
SELECT * FROM user WHERE username like concat('%',#{username},'%') limit #{pageNumber} , #{pageSize}
</select>
<select id="selectTotal" resultType="java.lang.Integer">
SELECT count(*) from user WHERE username like concat('%',#{username},'%')
</select>
从前端传入每页的数据数量以及页数,通过运算搜索该页所需显示的数据号
- 利用MyBatis-Plus插件可直接返回分页数据
@GetMapping("/page/front")
public Result findPage(
@RequestParam Integer pageNumber,
@RequestParam Integer pageSize,
@RequestParam(defaultValue = "") String name
){
QueryWrapper<Exam> queryWrapper=new QueryWrapper<>();
queryWrapper.orderByDesc("id");
if (!"".equals(name)){
queryWrapper.like("name",name);
}
Page<Exam>page=examService.page(new Page<>(pageNumber,pageSize),queryWrapper);
return Result.success(page);
}
返回的数据类型为
[{"id":2,"name":"大学物理考试","time":"2022-10-16T15:38:37.000Z","teacher":"某某","state":"未开始"}],
"total":1,"size":10,"current":1,"orders":[],"optimizeCountSql":true,"searchCount":true,"countId":null,"maxLimit":null,"pages":1},
利用Mybatis-Plus提供的方法可省去很多数据库语句增删改查的编写
|