iBatis分頁(基於Struts2和Freemarker)

來源:互聯網
上載者:User

iBATIS freemarker Spring ITeye Struts      之前介紹過基於Hibernate分頁的原理和設計,這裡我們所用的分頁都是物理分頁技術,不是JS實現的頁面分頁技術,是在SQL語句上執行的分頁,可以擷取結果集數量固定的列表,執行效率很高。下面來看看iBatis中如何設計分頁,本文基於Struts2,Spring3來整合,因為暫時Spring不支援MyBatis3(可以選用MyBatis官方的MyBatis-Spring外掛程式來實現,配有中文文檔,很好理解),我們這裡仍然以iBatis2作為載體來介紹。
    首先就是搭建開發環境,這裡可以說也是對Struts2,Spring3和iBatis2進行了簡單的整合,大家也可以來參考。項目的結構如下,使用Maven建立的web項目:

    添加必要的依賴,因為整合了Struts2和Spring,依賴就比較多了,如下:

    首先來配置一下Struts2,這個就比較簡單了,相信大家都不陌生。在web.xml中:
Xml代碼   <!-- Struts2的過濾器 -->   <filter>       <filter-name>struts2</filter-name>   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   </filter>   <filter-mapping>       <filter-name>struts2</filter-name>       <url-pattern>*.action</url-pattern>   </filter-mapping>  
    然後是struts.xml,配置Struts相關的內容,這裡我們配置freemarker為預設的結果類型,然後配置一個測試的Action,因為和Spring進行了整合,所以Action具體的配置放到Spring中來進行,如下即可:
Xml代碼   <?xml version="1.0" encoding="UTF-8" ?>   <!DOCTYPE struts PUBLIC       "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"       "http://struts.apache.org/dtds/struts-2.1.7.dtd">   <struts>       <package name="ibatis-paging" extends="struts-default"           namespace="/">           <result-types>               <result-type name="freemarker"           class="org.apache.struts2.views.freemarker.FreemarkerResult"                   default="true" />           </result-types>           <action name="user_*" class="userAction" method="{1}">               <result name="list">user_list.ftl</result>           </action>       </package>   </struts>  
    對Freemarker做一個簡單的設定,卸載freeemarer.properties檔案中即可,這裡我們主要是引用了一個宏檔案,就是分頁宏的配置,如下:
Properties代碼   template_update_delay=5   default_encoding=UTF-8   url_escaping_charset=UTF-8   number_format=0.#   date_format=yyyy-MM-dd   time_format=HH:mm:ss   datetime_format=yyyy-MM-dd HH:mm:ss   boolean_format=true,false   whitespace_stripping=true   tag_syntax=auto_detect   auto_import=/Freemarker/page_macro.ftl as p  
    Log4J的配置這裡不再貼出代碼,大家可以去下載源碼,一看就明白了,之後我們配置Spring,在resources檔案夾下建立spring子目錄,裡面放置Spring的設定檔,在web.xml中如下設定即可載入Spring的設定檔:
Xml代碼   <context-param>       <param-name>contextConfigLocation</param-name>       <param-value>classpath:spring/*.xml</param-value>   </context-param>   <!-- Spring載入設定檔來初始化IoC容器 -->   <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>  
    Spring中主要配置資料來源,iBatis的SqlMapClient和SqlMapClientTemplate,交易處理還有Action和Service的管理,其實內容大家也都很熟悉了,比較簡單:
Xml代碼   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"           destroy-method="close">           <property name="driverClassName" value="com.mysql.jdbc.Driver" />           <property name="url" value="jdbc:mysql://localhost:3306/test" />           <property name="username" value="root" />           <property name="password" value="123" />           <property name="maxActive" value="100" />           <property name="maxIdle" value="50" />           <property name="maxWait" value="100" />           <property name="defaultAutoCommit" value="true" />       </bean>       <!-- 建立JdbcTemplate -->       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">           <property name="dataSource" ref="dataSource" />       </bean>       <!-- 建立sqlMapClient -->       <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">           <property name="configLocation" value="classpath:sqlMapConfig.xml" />           <property name="dataSource" ref="dataSource" />       </bean>       <!-- 建立sqlMapClientTemplate -->       <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">           <constructor-arg>               <ref bean="sqlMapClient" />           </constructor-arg>       </bean>       <!-- 交易管理員 -->       <bean id="transactionManager"           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">           <property name="dataSource" ref="dataSource" />       </bean>       <!-- 配置交易處理通知 -->       <tx:advice id="txAdvice" transaction-manager="transactionManager">           <tx:attributes>               <tx:method name="get*" read-only="true" />               <tx:method name="add*" rollback-for="Exception" />               <tx:method name="addOrUpdate*" rollback-for="Exception" />               <tx:method name="del*" rollback-for="Exception" />               <tx:method name="update*" rollback-for="Exception" />           </tx:attributes>       </tx:advice>       <aop:config proxy-target-class="true">           <aop:pointcut id="serviceMethod"               expression="execution(* org.ourpioneer.service.*Service.*(..))" />           <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />       </aop:config>  
    之後對Service和Action進行配置:
Xml代碼   <bean id="baseService" class="org.ourpioneer.service.BaseService">       <property name="jdbcTemplate" ref="jdbcTemplate"></property>   </bean>      <bean id="userService" class="org.ourpioneer.service.UserService"       parent="baseService">       <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate" />   </bean>   <bean id="userAction" class="org.ourpioneer.action.UserAction">  </

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.