博客
关于我
long mode 分页_Mybatis 在dao中获取分页的总记录数
阅读量:792 次
发布时间:2023-02-06

本文共 15327 字,大约阅读时间需要 51 分钟。

/**

* 获取总计数量

* @param sqlSession

* @param statementName

* @param values

* @return

*/

private long getTotalCount(SqlSession sqlSession, String statementName, Object values) {

Map parameterMap = toParameterMap(values);

long count = 0L;

try {

MappedStatement mst = sqlSession.getConfiguration().getMappedStatement(statementName);

BoundSql boundSql = mst.getBoundSql(parameterMap);

String sql = "select count(*) total_count from (" + boundSql.getSql() + ")";

PreparedStatement pstmt = sqlSession.getConnection().prepareStatement(sql);

BoundSql countBS = new BoundSql(mst.getConfiguration(), sql, boundSql.getParameterMappings(), parameterMap);

setParameters(pstmt, mst, boundSql, parameterMap);

ResultSet rs = pstmt.executeQuery();

if (rs.next()) {

count = rs.getLong("total_count");

}

rs.close();

pstmt.close();

} catch (Exception e) {

count = 0L;

e.printStackTrace();

throw new RuntimeException(e);

}

return count;

}

/**

* 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler

* @param ps

* @param mappedStatement

* @param boundSql

* @param parameterObject

* @throws SQLException

*/

private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException {

ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());

List parameterMappings = boundSql.getParameterMappings();

if (parameterMappings != null) {

Configuration configuration = mappedStatement.getConfiguration();

TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();

MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);

for (int i = 0; i < parameterMappings.size(); i++) {

ParameterMapping parameterMapping = parameterMappings.get(i);

if (parameterMapping.getMode() != ParameterMode.OUT) {

Object value;

String propertyName = parameterMapping.getProperty();

PropertyTokenizer prop = new PropertyTokenizer(propertyName);

if (parameterObject == null) {

value = null;

} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {

value = parameterObject;

} else if (boundSql.hasAdditionalParameter(propertyName)) {

value = boundSql.getAdditionalParameter(propertyName);

} else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) {

value = boundSql.getAdditionalParameter(prop.getName());

if (value != null) {

value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));

}

} else {

value = metaObject == null ? null : metaObject.getValue(propertyName);

}

TypeHandler typeHandler = parameterMapping.getTypeHandler();

if (typeHandler == null) {

throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());

}

typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

">

">

转载地址:http://bnufk.baihongyu.com/

你可能感兴趣的文章
logrotate的日志轮转
查看>>
Logstash input jdbc连接数据库
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>
logstash-6.2.3从kafka读取日志输出到elasticsearch-6.2.3中
查看>>
logstash增量读取mysql中的数据到es中
查看>>
Logstash安装
查看>>
Logstash是什么,干什么用的?带你详细认识
查看>>
Logstash简介和部署---ElasticStack(ELK)工作笔记019
查看>>
logstash设置开机自启动
查看>>
logstash详解
查看>>
Logstash语法入门
查看>>
Logstash读取自定义日志以及解析处理_并把处理后数据上传至Elasticsearch---ElasticStack(ELK)工作笔记021
查看>>
Logstash配置详解---ElasticStack(ELK)工作笔记020
查看>>
loguru日志模块:简化Python自动化测试的日志管理!
查看>>
loj #6485. LJJ 学二项式定理 (模板qwq)
查看>>
Loj 6285. 数列分块入门 9
查看>>
LOJ#10064. 「一本通 3.1 例 1」黑暗城堡
查看>>
LOJ2542. 「PKUWC2018」随机游走【概率期望DP+Min-Max容斥(最值反演)】
查看>>
Loki 学习总结(1)—— Loki 中小项目日志系统的不二之选
查看>>