前面咱们将了Spring Boot集成Mybatis相关的操做,而在Mybatis-plus则是基于Mybatis进行了更加丰富的基础功能提供和封装,好比预置了大量的默认方法以及分页组件。java
好比其中提供的BaseMapper,用于其余业务Mapper的集成接口变定义了以下常见的功能的接口:mysql
public interface BaseMapper<T> extends Mapper<T> {
/** * 插入一条记录 * * @param entity 实体对象 */ int insert(T entity);
/** * 根据 ID 删除 * * @param id 主键ID */ int deleteById(Serializable id);
/** * 根据 columnMap 条件,删除记录 * * @param columnMap 表字段 map 对象 */ int deleteByMap( Map<String, Object> columnMap);
/** * 根据 entity 条件,删除记录 * * @param wrapper 实体对象封装操做类(能够为 null) */ int delete( Wrapper<T> wrapper);
/** * 删除(根据ID 批量删除) * * @param idList 主键ID列表(不能为 null 以及 empty) */ int deleteBatchIds( Collection<? extends Serializable> idList);
/** * 根据 ID 修改 * * @param entity 实体对象 */ int updateById( T entity);
/** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,能够为 null) * @param updateWrapper 实体对象封装操做类(能够为 null,里面的 entity 用于生成 where 语句) */ int update( T entity, Wrapper<T> updateWrapper);
/** * 根据 ID 查询 * * @param id 主键ID */ T selectById(Serializable id);
/** * 查询(根据ID 批量查询) * * @param idList 主键ID列表(不能为 null 以及 empty) */ List<T> selectBatchIds( Collection<? extends Serializable> idList);
/** * 查询(根据 columnMap 条件) * * @param columnMap 表字段 map 对象 */ List<T> selectByMap( Map<String, Object> columnMap);
/** * 根据 entity 条件,查询一条记录 * * @param queryWrapper 实体对象封装操做类(能够为 null) */ T selectOne( Wrapper<T> queryWrapper);
/** * 根据 Wrapper 条件,查询总记录数 * * @param queryWrapper 实体对象封装操做类(能够为 null) */ Integer selectCount( Wrapper<T> queryWrapper);
/** * 根据 entity 条件,查询所有记录 * * @param queryWrapper 实体对象封装操做类(能够为 null) */ List<T> selectList( Wrapper<T> queryWrapper);
/** * 根据 Wrapper 条件,查询所有记录 * * @param queryWrapper 实体对象封装操做类(能够为 null) */ List<Map<String, Object>> selectMaps( Wrapper<T> queryWrapper);
/** * 根据 Wrapper 条件,查询所有记录 * <p>注意: 只返回第一个字段的值</p> * * @param queryWrapper 实体对象封装操做类(能够为 null) */ List<Object> selectObjs( Wrapper<T> queryWrapper);
/** * 根据 entity 条件,查询所有记录(并翻页) * * @param page 分页查询条件(能够为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操做类(能够为 null) */ <E extends IPage<T>> E selectPage(E page, Wrapper<T> queryWrapper);
/** * 根据 Wrapper 条件,查询所有记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操做类 */ <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, Wrapper<T> queryWrapper);}
经过上述预置的接口及实现,咱们能够节省大量的时间,减小大量重复代码的编写,本篇文章就带你们了解一下如何在spring boot中集成mybatis-plus。web
mybatis-plus自己提供了spring boot的starter,注意这里依旧不是springboot官方提供的。spring
相关核心依赖引入以下:sql
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!-- 引入mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency></dependencies>
这里重点是mybatis-plus-boot-starter,其余的引入依赖与mybatis类似,但没有引入mybatis对应的starter。数据库
在application配置文件中进行数据库的配置:springboot
spring.datasource.url=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=genesis_123spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
依旧采用spring的datasource配置。mybatis
在启动类上添加@MapperScan来指定Mapper接口对应的包路径。app
"com.secbro.mapper") (public class SpringBootMainApplication {
public static void main(String[] args) { SpringApplication.run(SpringBootMainApplication.class, args); }
}
这里主要演示分页功能使用,所以须要经过单独的配置类来配置分页组件:ide
(value = {PaginationInterceptor.class})public class MybatisPlusConfig { public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; }}
实体类以下,注意若是类名与数据库表名不一导致用@TableName指定数据库表名。
"tb_order") (public class Order {
private int id;
private String orderNo;
private int amount;}
Mapper接口定义以下,集成BaseMapper,这样便拥有了上面展现的BaseMapper的对应方法功能。
public interface OrderMapper extends BaseMapper<Order> {
}
此时,不用再写任何代码即可以注入OrderMapper,调用一系列默认的增产改查及分页功能了。
这里以分页功能的使用为例来进行单元测试的演示:
4jclass OrderMapperTest {
private OrderMapper orderMapper;
void queryByPage() { //参数一是当前页,参数二是每页个数 IPage<Order> orderPage = new Page<>(1, 2); orderPage = orderMapper.selectPage(orderPage, null); List<Order> list = orderPage.getRecords(); for (Order order : list) { System.out.println(order); } }
}
固然,在实践中OrderMapper是要注入到对应service中进行使用的。
执行单元测试,发现成功查询数据库中记录并打印,分页功能正常使用。这里Page有多个构造参数,也会返回各种数据,好比分页总数,总条数等信息。你们可按照该思路调用对应方法尝试使用。