1、创建springboot项目
2、引入依赖
<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>3.5.1</version> </dependency> <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope> </dependency>
3、引入插件
File——Settings——Plugins——lombok(设置过就不用设置了)
4.设置application.yml文件
spring:# 配置数据源信息datasource:# 配置数据源类型type: com.zaxxer.hikari.HikariDataSource# 配置连接数据库信息driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=falseusername: rootpassword: 123456# 加入日志功能 mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 配置类型别名所对应的包type-aliases-package: com.example.demos.entity
本案例测试类使用的是user
如果测试需要加注解
@SpringBootTest public class MybatisPlusTest {@Resourceprivate UserMapper userMapper;//报错,mapper层加@Repository注解@Testpublic void testSelectList(){List<User> userList = userMapper.selectList(null);for (User user : userList) {System.out.println(user);}} }
此时准备工作已经做完
使用分页插件查询
首先建一个配置类
@Configuration @MapperScan("com.example.demos.mapper") public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return mybatisPlusInterceptor;} }
第二步测试类中调用selectPage
@SpringBootTest public class MybatisPlusPageTest {@Autowiredprivate UserMapper userMapper;@Testpublic void testPage(){Page<User> page = new Page<>(1,5);//limit (起始数,几条)1指第1页Page<User> userPage = userMapper.selectPage(page, null);//null查询全部就用nullSystem.out.println(page);System.out.println(page.getRecords());//获取当前页的数据System.out.println(page.getTotal());//查询总条数System.out.println(page.hasNext());//是否有下一页System.out.println(page.hasPrevious());//是否有上一页} }
自定义分页功能
配置类和上面一样
首先在mapper层,写方法
@Repository public interface UserMapper extends BaseMapper<User> {//Page必须位于第一个参数Page<User> selectPageVo(@Param("page")Page<User> page,@Param("age") Integer age); }
resource下的mapper中的xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demos.mapper.UserMapper"> <!-- Page<User> selectPageVo(@Param("page")Page<User> page,@Param("age") Integer age);--><select id="selectPageVo" resultType="User">select * from user where age > #{age}</select> </mapper>
测试类中
@Test public void testByPage(){Page<User> page = new Page<>(1,2);userMapper.selectPageVo(page,20);//第二个参数是拼接在SQL里的参数,也是前端传来的System.out.println(page.getRecords());//获取当前页的数据System.out.println(page.getTotal());//查询总条数System.out.println(page.hasNext());//是否有下一页System.out.println(page.hasPrevious());//是否有上一页 }