mybatis plus 新增分頁外掛

flyComeOn發表於2024-03-26

一、後端配置支援分頁控制元件

    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>


相關文章