mybatis plus 添加分页插件

一、后端配置支持分页控件

    1、 在pom.xml添加上依赖 <!-- Mybatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.2</version>
        </dependency>
2、添加设置类
package com.hengan.common.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;

/**
* @author: hengan
* @create: 2022-01-17 16:18
**/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件,如果不配置,分页插件将不生效
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 指定数据库方言为 MYSQL
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
3、在sqlSession中添加分页
package com.hengan.citicPlatGunNew.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.hengan.common.config.MybatisPlusConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
public class MainDataSourceConfig {

@Autowired
private MybatisPlusConfig config;

@Bean(name = "MainDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "MainSqlSessionFactory")
public SqlSessionFactory mainSqlSessionFactory(@Qualifier("MainDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
// 添加分页
bean.setPlugins(config.paginationInterceptor());
return bean.getObject();
}

@Bean(name = "MainTransactionManager")
public DataSourceTransactionManager mainTransactionManager(@Qualifier("MainDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "MainSqlSessionTemplate")
public SqlSessionTemplate mainSqlSessionTemplate(@Qualifier("MainSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}


 

二、写法(左右结构)

     <el-container>
<el-aside width="400px"> <div class="treediv" :style="{ height: $publicjs.divheight }"> <ContTree @treeNodeClick="treeNodeClick" /> </div> </el-aside> <el-container> <el-main> <div class="bgDiv" :style="{ height: $publicjs.divheight }"> <!-- 查询界面,查询参数、列表展示、导航分页 --> <el-card class="table-box"> <el-table v-loading="loading" :data="tableData" stripe height="95%" :row-style="$publicjs.tableRowStyle" :cell-style="{ padding: '0px' }" > <el-table-column type="index" width="45" label="序号"/> </el-table> <!--分页 --> <el-pagination background :page-sizes="$publicjs.pageSizes" :page-size="pageSize" :total="totalCount" :current-page="currentPage" :layout="$publicjs.elTableLayout" @size-change="handleSizeChange" @current-change="handleCurrentChange" /> </el-card> </div> </el-main> </el-container> </el-container>
二、拼接参数写法
// 列表查询
 getDataList() {
      this.loading = true;
      this.tableData = [];
      let params = new FormData();
      params.append("pageIndex", this.currentPage)
      params.append("pageCount", this.pageSize)
      params.append("orgId", this.selectForm.orgId);
      getGunAlarmInfo(params).then((res) => {
        this.loading = false;
        if (res.code == 200) {
           let result =res.data
           this.tableData= result.records;
           this.totalCount = result.total;
        } else {
          this.loading = false;
          this.$publicjs.showMessage(res.message, this.$publicjs.ErrorType);
        }
      })
    },
   handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
三、调用后端接口
export function getGunAlarmInfo(data) {
  return request({
    url: 'gms/gun-use-freq/getGunAlarmInfo',
    method: 'post',
    data
  })
}
四、controller
@ApiOperation(value = "查询每个分子公司的报警信息")
@PostMapping("/getGunAlarmInfo")
public ResponseData getGunAlarmInfo(String orgId, Integer pageIndex, Integer pageCount ){
return gunUseCountService.getGunAlarmInfo(orgId,pageIndex, pageCount);
}
五、services 层
// 分页
Page<EquiRealAlarmEx> page = new Page<>(pageIndex, pageCount);
// 获得分子公司的报警信息,自定义的查询语句,page参数需要放在第一个参数位置,这是硬性规定
IPage<EquiRealAlarmEx> pageResult = gunAlarmDataMapper.getAlarmData(page,map);
List<EquiRealAlarmEx> list = pageResult.getRecords();
long total = pageResult.getTotal();
六、mapper层
// 查询每个分子公司枪支使用记录
IPage<EquiRealAlarmEx> getAlarmData( Page<EquiRealAlarmEx> page, @Param("map") Map<String, Object> map);
七、xml
<select id="getAlarmData" resultType="com.hengan.citicPlatGunNew.entity.ex.EquiRealAlarmEx">
SELECT
s.dept_name,
c.cabinet_name,
b.gun_code,
e.alarm_type,
e.alarm_start_time,
e.alarm_end_time
FROM `${map.dbName}`.equi_real_alarm e
LEFT JOIN `${map.dbName}`.base_cabinet_info c ON e.cabinet_id = c.id
left join `${map.dbName}`.base_gun_info b on b.id = e.gun_id
LEFT JOIN `${map.dbName}`.sys_dept_info s on s.id = c.dept_id
where b.is_delete=#{map.isDelete} and b.is_enabled =#{map.isEnabled} and e.cabinet_id is not null
order by e.alarm_start_time desc
</select>


posted @ 2024-03-26 13:50  flyComeOn  阅读(680)  评论(0编辑  收藏  举报