Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91629
  • 博文数量: 40
  • 博客积分: 651
  • 博客等级: 上士
  • 技术积分: 356
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-08 22:31
文章分类

全部博文(40)

文章存档

2013年(6)

2012年(3)

2011年(31)

我的朋友

分类: Java

2013-04-01 17:08:45

目采用了拦截器,就是mybaits自带的plus功能。将每次select操作都进行拦截。
cotroller层
 
  1. @RequestMapping(params = ("method=list"))
  2.     public ModelAndView listAll(HttpServletRequest request,HttpServletResponse response) {
  3.         
  4.         //可以将分页参数获取封装,已达到更好的复用效果。
  5.         //page=2&pageSize=10&totalPages=19&totalRows=188
  6.         String pagec = request.getParameter("page");
  7.         String pageSize = request.getParameter("pageSize");
  8.         String totalPages = request.getParameter("totalPages");
  9.         String totalRows = request.getParameter("totalRows");
  10.         //这里其实有些条条件,例如totalRows 前台是没有办法提供的,还有 totalPages 或其它些参数通过 计算可以获得
  11.         String price = request.getParameter("price");//书的价格
  12.         String name = request.getParameter("name");//书的名称
  13.         Map map = new HashMap();
  14.         if(StringUtils.isNotEmpty(price))
  15.          map.put("price", new Integer(price));
  16.         if(StringUtils.isNotEmpty(price))
  17.         map.put("name", name);
  18.          
  19.         //方法1:将分页参数直接放到mapper接口函数参数中,也可在对象中定义名字为page的属性,反射一样可以得到
  20.         //后台连接直接获取
  21.         //Page page = new Page();
  22.         
  23.         //方法2:不用进行map传参,用ThreadLocal进行传参,方便没有侵入性
  24.         PageContext page = PageContext.getContext();
  25.         
  26.         //请自行验证
  27.         if(null == pagec)
  28.         {
  29.             page.setCurrentPage(1);
  30.             page.setPageSize(10);
  31.         }
  32.         else{
  33.             page.setCurrentPage(Integer.parseInt(pagec));
  34.             page.setPageSize(Integer.parseInt(pageSize));
  35.             page.setTotalPages(Integer.parseInt(totalPages));
  36.             page.setTotalRows(Integer.parseInt(totalRows));
  37.         }
  38.         page.setPagination(true);

  39. //        方法1用
  40. //        Map map = new HashMap();
  41. //        map.put("page", page);
  42. //        HandlerResult rs = userService.list(map);
  43.         
  44.         //方法2用
  45.         HandlerResult rs = userService.list(map);
  46.         
  47.         ModelAndView mv = new ModelAndView("/views/show.jsp");
  48.         mv.addObject("userList", rs.getResultObj());
  49.         mv.addObject("page",page);
  50.         return mv;
  51.     }
简要说明:本文采用两种方式将page对象传入到拦截器中。第一种方式是采用参数传值,不管是用map还是在统一参数对象中名称为page的属性都可以在分页拦截器中得到page的值。第二种方式是用ThreadLocal,对service层没有侵入性。比较方便。

Service层代码。
 
  1. public class UserService {
  2.     
  3.     @Autowired
  4.     private UsersMapper usersMappser; 接口中的方法要和myabatis配置文件中配置的查询id要一致


  5.     /**
  6.      * 统一Service出口,方便管理
  7.      * @param map
  8.      * @return
  9.      */
  10.     public HandlerResult list(Map map){
  11.         
  12.         HandlerResult rs = new HandlerResult();
  13.         
  14.         rs.setResultObj(usersMappser.list(map));
  15.         
  16.         return rs;
  17.     }
  18.     
  19.     /**
  20.      * 采用本地线程的方式分页
  21.      * @return
  22.      */
  23.     public HandlerResult list(){
  24.         
  25.         HandlerResult rs = new HandlerResult();
  26.         
  27.         rs.setResultObj(usersMappser.list2());
  28.         
  29.         return rs;
  30.     }
  31.     

  32.     public UsersMapper getUsersMappser() {
  33.         return usersMappser;
  34.     }

  35.     public void setUsersMappser(UsersMapper usersMappser) {
  36.         this.usersMappser = usersMappser;
  37.     }
  38. }
3:mybatis接口 配置文件
  
  1. public interface UsersMapper extends SqlMapper{
  2.     
  3.     public List list(Map para);
  4.     
  5.     public List list2();
  6. }
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.   "">
  5. <mapper namespace="com.flydreamer.orm.mapper.UsersMapper">
  6.     
  7.     <resultMap type="com.flydreamer.orm.pojo.Book" id="userResult">
  8.         <result property="id" column="ID" />
  9.         <result property="name" column="NAME" />
  10.         <result property="price" column="PRICE" />
  11.     </resultMap>
  12.     
  13.     <select id="list" parameterType="map" resultMap="userResult">
  14.             SELECT id,name,price FROM book
  15.         <where>
  16.             <if test="name !=null">
  17.              and b.name LIKE CONCAT(CONCAT('%',
  18.                         #{name}),'%')
  19.             </if>
  20.             
  21.             <if test="price !=null">
  22.              and b.price >= #{price}
  23.             </if>
  24.         </where>
  25.     </select>
  26.     
  27.     <select id="list2" resultMap="userResult">
  28.     SELECT id,name,price FROM book
  29.     </select>
  30.  
  31. </mapper>

4:page的拦截器
 
  1. package com.flydreamer.interceptor;

  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.List;
  7. import java.util.Properties;

  8. import org.apache.ibatis.builder.xml.dynamic.ForEachSqlNode;
  9. import org.apache.ibatis.executor.ErrorContext;
  10. import org.apache.ibatis.executor.Executor;
  11. import org.apache.ibatis.executor.ExecutorException;
  12. import org.apache.ibatis.logging.Log;
  13. import org.apache.ibatis.logging.LogFactory;
  14. import org.apache.ibatis.mapping.BoundSql;
  15. import org.apache.ibatis.mapping.MappedStatement;
  16. import org.apache.ibatis.mapping.ParameterMapping;
  17. import org.apache.ibatis.mapping.ParameterMode;
  18. import org.apache.ibatis.mapping.SqlSource;
  19. import org.apache.ibatis.mapping.MappedStatement.Builder;
  20. import org.apache.ibatis.plugin.Interceptor;
  21. import org.apache.ibatis.plugin.Intercepts;
  22. import org.apache.ibatis.plugin.Invocation;
  23. import org.apache.ibatis.plugin.Plugin;
  24. import org.apache.ibatis.plugin.Signature;
  25. import org.apache.ibatis.reflection.MetaObject;
  26. import org.apache.ibatis.reflection.property.PropertyTokenizer;
  27. import org.apache.ibatis.session.Configuration;
  28. import org.apache.ibatis.session.ResultHandler;
  29. import org.apache.ibatis.session.RowBounds;
  30. import org.apache.ibatis.type.TypeHandler;
  31. import org.apache.ibatis.type.TypeHandlerRegistry;

  32. import com.flydreamer.page.Db2Dialect;
  33. import com.flydreamer.page.Dialect;
  34. import com.flydreamer.page.MySql5Dialect;
  35. import com.flydreamer.page.OracleDialect;
  36. import com.flydreamer.page.Page;
  37. import com.flydreamer.page.PageContext;
  38. import com.flydreamer.page.ReflectHelper;

  39. //只拦截select部分
  40. @Intercepts({@Signature(type=Executor.class,method="query",args={ MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class })})
  41. public class PaginationInterceptor implements Interceptor{
  42.     
  43.     private final static Log log = LogFactory.getLog(PaginationInterceptor.class);
  44.     
  45.     Dialect dialect = new Db2Dialect();
  46.     
  47.     public Object intercept(Invocation invocation) throws Throwable {
  48.         
  49.         MappedStatement mappedStatement=(MappedStatement)invocation.getArgs()[0];        
  50.         Object parameter = invocation.getArgs()[1];
  51.         BoundSql boundSql = mappedStatement.getBoundSql(parameter);
  52.         String originalSql = boundSql.getSql().trim();
  53.         RowBounds rowBounds = (RowBounds)invocation.getArgs()[2];

  54.         Object parameterObject = boundSql.getParameterObject();
  55.         if(boundSql==null || boundSql.getSql()==null || "".equals(boundSql.getSql()))
  56.             return null;
  57.         //分页参数--上下文传参
  58.         Page page = null;
  59.         PageContext context=PageContext.getContext();
  60.         
  61.         //map传参每次都将currentPage重置,先判读map再判断context
  62.         if(parameterObject!=null)
  63.             page = (Page)ReflectHelper.isPage(parameterObject,"page");
  64.         
  65.         //分页参数--context参数里的Page传参
  66.         if(page==null && context.isPagination()==true)
  67.         {
  68.             page = context;
  69.         }
  70.         
  71.         //后面用到了context的东东
  72.         if(page!=null && page.isPagination()==true)             
  73.         {
  74.          int totpage=page.getTotalRows();    
  75.          //得到总记录数
  76.          if (totpage==0)
  77.             {
  78.                 StringBuffer countSql = new StringBuffer(originalSql.length()+100 );
  79.                 countSql.append("select count(1) from (").append(originalSql).append(") t");
  80.                      Connection connection=mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection() ;
  81.                  PreparedStatement countStmt = connection.prepareStatement(countSql.toString());
  82.                  BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(),countSql.toString(),boundSql.getParameterMappings(),parameterObject);
  83.                  setParameters(countStmt,mappedStatement,countBS,parameterObject);
  84.                  ResultSet rs = countStmt.executeQuery();
  85.                  if (rs.next()) {
  86.                      totpage = rs.getInt(1);
  87.                  }
  88.                  rs.close();
  89.                  countStmt.close();
  90.                  connection.close();
  91.             }
  92.         
  93.          //分页计算
  94.      page.init(totpage,page.getPageSize(),page.getCurrentPage());
  95.         
  96.             if(rowBounds == null || rowBounds == RowBounds.DEFAULT){
  97.                 rowBounds= new RowBounds(page.getPageSize()*(page.getCurrentPage()-1),page.getPageSize());
  98.                 
  99.             }    

  100.             //分页查询 本地化对象 修改数据库注意修改实现
  101.          String pagesql=dialect.getLimitString(originalSql, rowBounds.getOffset(), rowBounds.getLimit());
  102.          invocation.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
  103.          BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pagesql,boundSql.getParameterMappings(),boundSql.getParameterObject());
  104.          MappedStatement newMs = copyFromMappedStatement(mappedStatement,new BoundSqlSqlSource(newBoundSql));
  105.         
  106.          invocation.getArgs()[0]= newMs;
  107.         }
  108.             
  109.             
  110.          return invocation.proceed();
  111.         
  112.     }
  113.     public static class BoundSqlSqlSource implements SqlSource {
  114.         BoundSql boundSql;
  115.   
  116.         public BoundSqlSqlSource(BoundSql boundSql) {
  117.             this.boundSql = boundSql;
  118.         }
  119.   
  120.         public BoundSql getBoundSql(Object parameterObject) {
  121.             return boundSql;
  122.         }
  123.     }
  124.     public Object plugin(Object arg0) {
  125.          return Plugin.wrap(arg0, this);
  126.     }
  127.     public void setProperties(Properties arg0) {
  128.     
  129.     }
  130.     
  131.     /**
  132.      * 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler
  133.      * @param ps
  134.      * @param mappedStatement
  135.      * @param boundSql
  136.      * @param parameterObject
  137.      * @throws SQLException
  138.      */
  139.     private void setParameters(PreparedStatement ps,MappedStatement mappedStatement,BoundSql boundSql,Object parameterObject) throws SQLException {
  140.         ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
  141.         List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  142.         if (parameterMappings != null) {
  143.             Configuration configuration = mappedStatement.getConfiguration();
  144.             TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
  145.             MetaObject metaObject = parameterObject == null ? null: configuration.newMetaObject(parameterObject);
  146.             for (int i = 0; i < parameterMappings.size(); i++) {
  147.                 ParameterMapping parameterMapping = parameterMappings.get(i);
  148.                 if (parameterMapping.getMode() != ParameterMode.OUT) {
  149.                     Object value;
  150.                     String propertyName = parameterMapping.getProperty();
  151.                     PropertyTokenizer prop = new PropertyTokenizer(propertyName);
  152.                     if (parameterObject == null) {
  153.                         value = null;
  154.                     } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
  155.                         value = parameterObject;
  156.                     } else if (boundSql.hasAdditionalParameter(propertyName)) {
  157.                         value = boundSql.getAdditionalParameter(propertyName);
  158.                     } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)&& boundSql.hasAdditionalParameter(prop.getName())) {
  159.                         value = boundSql.getAdditionalParameter(prop.getName());
  160.                         if (value != null) {
  161.                             value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
  162.                         }
  163.                     } else {
  164.                         value = metaObject == null ? null : metaObject.getValue(propertyName);
  165.                     }
  166.                     TypeHandler typeHandler = parameterMapping.getTypeHandler();
  167.                     if (typeHandler == null) {
  168.                         throw new ExecutorException("There was no TypeHandler found for parameter "+ propertyName + " of statement "+ mappedStatement.getId());
  169.                     }
  170.                     typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
  171.                 }
  172.             }
  173.         }
  174.     }
  175.     
  176.     private MappedStatement copyFromMappedStatement(MappedStatement ms,
  177.              SqlSource newSqlSource) {
  178.             Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
  179.             ms.getId(), newSqlSource, ms.getSqlCommandType());
  180.             builder.resource(ms.getResource());
  181.             builder.fetchSize(ms.getFetchSize());
  182.             builder.statementType(ms.getStatementType());
  183.             builder.keyGenerator(ms.getKeyGenerator());
  184.             //builder.keyProperty(ms.getKeyProperties().toString());
  185.             builder.timeout(ms.getTimeout());
  186.              builder.parameterMap(ms.getParameterMap());
  187.             builder.resultMaps(ms.getResultMaps());
  188.             builder.cache(ms.getCache());
  189.             MappedStatement newMs = builder.build();
  190.             return newMs;
  191.             }
  192.         

  193. }

5:spring 配置

   


?    

   
       
       
       
   
 

6、拦截器配置
      


 
     
   
   
   
   
   
         
         
         
     





阅读(5922) | 评论(1) | 转发(0) |
3

上一篇:【三】文本分析器Analyzer

下一篇:没有了

给主人留下些什么吧!~~

hntuddn2014-09-16 13:34:30

不需要分页的也被拦截下来分页了。