/**
* get total count
* @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<ParameterMapping> 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());
}
}
}
}
protected Map toParameterMap(Object parameter) {
if (parameter == null) {
return new HashMap();
}
if (parameter instanceof Map) {
return (Map<?,?>) parameter;
} else {
try {
return PropertyUtils.describe(parameter);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
分享到:
相关推荐
MyBatis通用Dao,包含分页方法
springboot+mybatis 多数据源 basedao +分页 basedao做的不好,你们可以改进
myeclipse 2013 插件MyBatis Generator 1.3.2生成的代码缺少分页,使用麻烦。修改了源码org.mybatis.generator.core_1.3.2.201207161521.jar包,增加了PaginationPlugin和SelectOneByExamplePlugin两个扩展插件。...
9. Mybatis 映射文件中,如果 A 标签通过 include 引用了 B 标 签的内容,请问,B 标签能否定义在 A 标签的后面,还是说 必须定义在 A 标签的前面? 10. 简述 Mybatis 的 Xml 映射文件和 Mybatis 内部数据结构之间的...
本文介绍使用Mybatis拦截器,实现分页;并且在dao层,直接返回自定义的分页对象。具有很好的参考价值,下面跟着小编一起来看下吧
自动读取数据库表结构,生成bean和mybatis dao文件,dao含基本的增删改查和分页方法
MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个...
此框架可自动生成SpringMvc+mybatis框架结构数据库采用的是mysql用户可以自行更改成自己需要的数据库,使用时将本项目导入myeclipse或eclipse中,更改项目名称为 你自定义的项目名称后在Test包中有2个java类...
分了dao service的ssm整合还附带分页功能,合适新手,对于这些知识还不是很熟悉,想进一步了解的,可以以这个作为基础,在往深层次的发展
2. MyBatis采用目前流行的通用Mapper方式进行DAO开发,避免单表在xml写sql语句 3、采用PageHelper分页插件 4、spring boot包含 dev、prod两个环境配置文件 5、集成 swagger-ui,方便Controller层接口展示和调试 6、...
zebra-dao是在mybatis基础上进一步封装的异步DAO,同时它也支持分页功能。它的产生背景是公司要求服务完全异步化,一个服务可能包括RPC调用请求、MemCached请求、KV存储请求以及MySQL数据库调用,目前其它三种请求的...
##### dao.template 数据库接口Dao(mybatis接口方式,在方法上写sql,复杂的使用sqlProvider) ##### daoSqlProvider.template 复杂sql提供者 ##### service.template 对外开放的接口 ##### serviceImpl....
只需简单配置后台数据库和包名,即可快速生成springBoot+mybatis所需要的全部后台代码(controller、service、serviceIml、dao、entity、sqlxml文件),实现对每个表的新增、修改、删除、查询(分页查询+列表查询)...
工具默认集成了以MyBatis作为持久化层的模板,如果需要JPA相关等模板可以自己参考工具的文档进行编写 生成实体类 生成WEB API相关接口 Controller 生成业务逻辑相关的Service/ServiceImpl 生成操作数据库的Dao接口...
实现Mybatis的分页查询模块,支持MySQL、PostgreSQL、SQLServer等数据库分页查询 通用的权限管理模块,基于Apache Shiro的 用户-角色-权限(RBAC)的细粒度权限控制 大量配置示例,根据需求,自由优化、调整,达到最佳...
1.mapper文件:在原先生成的mapper文件中加入分页查询,条件查询,条件统计.并且自动生成oracle/mysql分页sql(仅支持) 2.dao: 自使用生成接口方法,若继承超类,则有超类抽象实现. 3.service:生成spring标准的service源码...
程序的结构分为表现层(Controller)、业务层(Service)、数据层(Dao),包括权限授权、分页查询、日志记录,订单及产品查询,授权管理等功能 入门级DEMO 博客链接: 技术点: Spring IOC & DI(控制反转和依赖注入) ...
仅包含增删改查和分页,用户登录功能。谨慎下载。分页采用list截取实现。
+ **MySQL分页**:此脚手架未采用基于MyBatis拦截器的第三方分页插件,而是直接在生成的dao层添加`setLimit*`,使用它们即可在生成的sql中直接添加`limit ?, ?`,这样做即简洁又易于理解。 + **MySQL注释**:将...
主要包含web项目经常使用的一些组件,包含: mybatis的dao封装和mybatis配置文件扫描器使用redis缓存的基本配置工具类druid数据源模型函数(加密数据库密码)基本实体封装常用的枚举类型日志收集配置类分页信息配置...