说明
分页类:com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect,com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor,com.baomidou.mybatisplus.extension.plugins.pagination.Page。
代码
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.12.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<!-- 这个元素描述了项目相关或测试相关的所有资源路径 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.10.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
application.yml
server:
port: 8080
tomcat:
uri-encoding: utf-8
servlet:
context-path: /demo
#数据源配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://132.232.30.6:3306/health_business_eden?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: root1234
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 20
min-idle: 20
max-active: 50
max-wait: 60000
time-between-eviction-runsMillis: 60000
min-evictable-idle-timeMillis: 300000
validation-query: select 'x' FROM DUAL
test-on-borrow: false
test-on-return: false
max-open-prepared-statements: 20
max-pool-prepared-statement-per-connection-size: 20
filters:
stat:
# 数据库类型
db-type: mysql
# 慢SQL
log-slow-sql: true
# 慢SQL执行时间
slow-sql-millis: 1000
merge-sql: true
initialization-mode: always
connection-properties: druid.stat.mergeSql=true;druid.stat.sloSqlMillis=5000
logging:
level:
com.what21.demo: error
#com.what21.demo.mapper: debug
DemoApplication
package com.what21.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.what21.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
Model
package com.what21.demo.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@TableName("employee")
@Data
public class Employee{
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String domain;
private String name;
private String email;
private String gender;
private Integer age;
private Date createTime;
}
EmployeeMapper
package com.what21.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.what21.demo.model.Employee;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface EmployeeMapper extends BaseMapper<Employee> {
public List<Employee> pageList(Page<Employee> page, Map<String, Object> paramsMap);
}
EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.what21.demo.mapper.EmployeeMapper">
<resultMap id="employeeResultMap" type="com.what21.demo.model.Employee">
<id column="id" property="id"/>
<result column="domain" property="domain"/>
<result column="name" property="name"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="age" property="age"/>
<result column="create_time" property="createTime"/>
</resultMap>
<select id="pageList" parameterType="java.util.Map" resultMap="employeeResultMap">
select * from `employee`
</select>
</mapper>
测试:
package com.what21.demo.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.what21.demo.DemoApplication;
import com.what21.demo.model.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class EmployeeMapperTest {
@Autowired
EmployeeMapper employeeMapper;
@Test
public void testPageList() {
Page<Employee> page = new Page<>(2, 10);
Map<String, Object> paramsMap = new HashMap<>();
List<Employee> dataList = employeeMapper.pageList(page, paramsMap);
page.setRecords(dataList);
System.out.println("当前页=>" + page.getCurrent());
System.out.println("页大小=>" + page.getSize());
System.out.println("总页数=>" + page.getPages());
System.out.println("总记录=>" + page.getTotal());
for (Employee employee : page.getRecords()) {
System.out.println(employee);
}
}
}
今天的文章Java,MyBatisPlus,XML中使用PaginationInnerInterceptor分页分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7915.html