目录
1、设置分页拦截器
2、创建数据库及表
3、pom.xml
4、添加MP的相关配置信息 application.yml
5、根据数据库表创建实体类 User
6、创建 UserDao 接口
7、编写引导类
8、编写测试类
9、Run的运行结果
1、设置分页拦截器
package com.example.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MpConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;} }
2、创建数据库及表
create database if not exists mybatisplus_db character set utf8; use mybatisplus_db; CREATE TABLE user (id bigint(20) primary key auto_increment,name varchar(32) not null,password varchar(32) not null,age int(3) not null ,tel varchar(32) not null ); insert into user values(1,'Tom','tom',3,'18866668888'); insert into user values(2,'Jerry','jerry',4,'16688886666'); insert into user values(3,'Jock','123456',41,'18812345678'); insert into user values(4,'小明','xm',15,'4006184000');
3、pom.xml
- springboot版本是 2.5.0
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version><relativePath/> <!-- lookup parent from repository --></parent><!-- Generated by https://start.springboot.io --><!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn --><groupId>com.example</groupId><artifactId>mp_demo</artifactId><version>0.0.1-SNAPSHOT</version><name>mp_demo</name><description>mp_demo</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.32</version><scope>runtime</scope></dependency>--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.26</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
4、添加MP的相关配置信息 application.yml
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=Asia/Shanghaiusername: rootpassword: 123456 # 开启mp的日志(输出到控制台) mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
5、根据数据库表创建实体类 User
package com.example.model; import lombok.Data;@Data public class User {private Long id;private String name;private String password;private Integer age;private String tel;//setter...getter...toString方法略 }
6、创建 UserDao 接口
package com.example.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.model.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserDao extends BaseMapper<User> { }
7、编写引导类
package com.example;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;// Generated by https://start.springboot.io // 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn @SpringBootApplication //@MapperScan("com.example.mp_demo.mapper") public class Mybatisplus01QuickstartApplication {public static void main(String[] args) {SpringApplication.run(Mybatisplus01QuickstartApplication.class, args);}}
8、编写测试类
package com.example;import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.mapper.UserDao; import com.example.model.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List;@SpringBootTest class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll() {List<User> userList = userDao.selectList(null);userList.forEach(System.out::println);}@Testvoid testGetByPage() {// IPage对象封装了分页操作相关的数据IPage<User> page = new Page<>(1, 2);userDao.selectPage(page, null);System.out.println("当前页码值:" + page.getCurrent());System.out.println("每页显示数:" + page.getSize());System.out.println("总记录数:" + page.getTotal());System.out.println("总页码数:" + page.getPages());System.out.println("当前页数据:");page.getRecords().forEach(System.out::println);}}
9、Run的运行结果
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3b35798] will not be managed by Spring ==> Preparing: SELECT COUNT(*) FROM user ==> Parameters: <== Columns: COUNT(*) <== Row: 4 <== Total: 1 ==> Preparing: SELECT id,name,password,age,tel FROM user LIMIT ? ==> Parameters: 2(Long) <== Columns: id, name, password, age, tel <== Row: 1, Tom, tom, 3, 18866668888 <== Row: 2, Jerry, jerry, 4, 16688886666 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@778a1250] 当前页码值:1 每页显示数:2 总记录数:4 总页码数:2 当前页数据: User(id=1, name=Tom, password=tom, age=3, tel=18866668888) User(id=2, name=Jerry, password=jerry, age=4, tel=16688886666)