mybatis+springMvc
来源:互联网 发布:淘宝店铺刷流量有用吗 编辑:程序博客网 时间:2024/05/22 13:05
1.在xml配置文件中配置插件 来拦截分页请求
<plugins>
<plugin interceptor="com.CQMarketSupervise.common.PaginationInterceptor" />
</plugins>
2.拦截
MetaObject简介
在我的实现里大量使用了MetaObject这个对象,因此有必要先介绍下它。MetaObject是Mybatis提供的一个的工具类,通过它包装一个对象后可以获取或设置该对象的原本不可访问的属性(比如那些私有属性)。它有个三个重要方法经常用到:
1) MetaObject forObject(Object object,ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory)
2) Object getValue(String name)
3) void setValue(String name, Object value)
方法1)用于包装对象;方法2)用于获取属性的值(支持OGNL的方法);方法3)用于设置属性的值(支持OGNL的方法);
@Intercepts( { @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class PaginationInterceptor implements Interceptor {
private final static Log log = LogFactory
.getLog(PaginationInterceptor.class);
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation
.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
MetaObject metaStatementHandler = MetaObject.forObject(
statementHandler, new DefaultObjectFactory(),
new DefaultObjectWrapperFactory());
RowBounds rowBounds = (RowBounds) metaStatementHandler
.getValue("delegate.rowBounds");
if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
return invocation.proceed();
}
Configuration configuration = (Configuration) metaStatementHandler
.getValue("delegate.configuration");
Dialect.Type databaseType = null;
try {
databaseType = Dialect.Type.valueOf(configuration.getVariables()
.getProperty("dialect").toUpperCase());
} catch (Exception e) {
// ignore
}
if (databaseType == null) {
throw new RuntimeException(
"the value of the dialect property in configuration.xml is not defined : "
+ configuration.getVariables().getProperty(
"dialect"));
}
Dialect dialect = null;
switch (databaseType) {
case MYSQL:
dialect = new MysqlDialect();
break;
case ORACLE:
dialect = new OracleDialect();
break;
case SQLSERVER:
dialect = new SQLServerDialect();
break;
}
String originalSql = (String) metaStatementHandler
.getValue("delegate.boundSql.sql");
metaStatementHandler.setValue("delegate.boundSql.sql", dialect
.getLimitString(originalSql, rowBounds.getOffset(), rowBounds
.getLimit()));
metaStatementHandler.setValue("delegate.rowBounds.offset",
RowBounds.NO_ROW_OFFSET);
metaStatementHandler.setValue("delegate.rowBounds.limit",
RowBounds.NO_ROW_LIMIT);
if (log.isDebugEnabled()) {
log.debug("生成分页SQL : " + boundSql.getSql());
}
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
------------------------------------------------------------------------------------
public abstract class Dialect {
public static enum Type {
MYSQL, ORACLE,SQLSERVER
}
public abstract String getLimitString(String sql, int offset, int limit);
}
------------------------------------------------------------------------------------
public class OracleDialect extends Dialect {
@Override
public String getLimitString(String sql, int offset, int limit) {
// TODO Auto-generated method stub
sql = sql.trim();
StringBuffer pagingSelect = new StringBuffer(sql.length() + 100);
pagingSelect
.append("select * from ( select row_.*, rownum rownum_ from ( ");
pagingSelect.append(sql);
pagingSelect.append(" ) row_ ) where rownum_ > ").append(offset)
.append(" and rownum_ <= ").append(offset + limit);
return pagingSelect.toString();
}
}
/**
* 通用分页模型
* @author wt
*
*/
public class PageInfo {
/**
* 总条数
*/
private int total;
/**
* 总页数
*/
private int pageTotal;
/**
* 当前页
*/
private int currPageNo;
/**
* 每页条数
*/
private int pageSize;
/**
* 当前页详细数据
*/
private List rows = new ArrayList();
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageTotal() {
return pageTotal;
}
public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}
public int getCurrPageNo() {
return currPageNo;
}
public void setCurrPageNo(int currPageNo) {
this.currPageNo = currPageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
-------------------------------------------------------------------------------------
private static final Logger log = Logger.getLogger(CommonServiceImpl.class);
@Resource(name = "sqlSessionFactory")
private DefaultSqlSessionFactory fb;
public PageInfo getPage(String detailSqlId, String countSqlId, Object obj,
int pageNo, int pageSize) {
return getPage(detailSqlId, countSqlId, obj, pageNo, pageSize, 1);
}
public PageInfo getPage(String detailSqlId, String countSqlId, Object obj,
int pageNo, int pageSize, int fetchNum) {
PageInfo page = new PageInfo();
page.setCurrPageNo(pageNo);
page.setPageSize(pageSize);
SqlSession ss = null;
int cruuPageNo = page.getCurrPageNo();
try {
ss = fb.openSession();
if (cruuPageNo < 1) {
cruuPageNo = 1;
}
Object oo = ss.selectOne(countSqlId, obj);
int cnt = new Integer(oo.toString());
int pageTotal = cnt % page.getPageSize() == 0 ? cnt
/ page.getPageSize() : (cnt / page.getPageSize() + 1);
page.setTotal(cnt);
page.setPageTotal(pageTotal);
int offset = (cruuPageNo - 1) * page.getPageSize();
RowBounds bounds = new RowBounds(offset, page.getPageSize()
* fetchNum);
page.setCurrPageNo(cruuPageNo);
page.setRows(ss.selectList(detailSqlId, obj, bounds));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ss.close();
}
log.debug(JsonUtil.object2json(page));
return page;
}
- mybatis,springmvc
- springMVC +MyBatis
- springmvc+mybatis
- Springmvc+mybatis
- mybatis+springMvc
- springMVC+Mybatis
- SpringMVC+MyBatis
- springmvc+mybatis
- springMVC+mybatis
- springmvc+mybatis
- SpringMVC+MyBatis
- springmvc+mybatis
- springmvc+mybatis整合springmvc
- SpringMVC: 重拾springMVC+mybatis
- SpringMVC Mybatis学习资料
- spring+springmvc+mybatis+maven
- SpringMVC+mybatis+DWR3注解
- springMVC+mybatis配置详解
- java小游戏-坦克大战,事后总结
- 如何改变Javascript在页面的执行顺序
- 正则表达式
- 升级ZC451 codebase遇到的问题以及解决方法
- Eclipse编译调试Cocos2d问题汇总
- mybatis+springMvc
- DECLARE_DYNAMIC和IMPLEMENT_DYNAMIC宏
- poj-1840 Eqs 暴力+哈希
- DataGirdView嵌套mdi中样式影响后的解决方案
- Android实战简易教程-第三十八枪(模仿腾讯QQ的网络状态提示和设置功能实现)
- 101Symmetric Tree
- windows下 python 2.7x 安装pip
- php面向对象要点[转]
- HDU4678Mine(博弈+sg)