SSM框架(一)mybatis,sqlserver分頁查詢

一、利用BootstrapTable插件進行數據展現,指定sidePagination=‘server’以後,須要從後端進行分頁,在請求‘information/getAllNews’的時候,會傳遞兩個參數offset和limit,後臺根據這兩個參數進行分頁查詢。java

$('#tableNews').bootstrapTable({
    type: 'GET',
    url: '<%=request.getContextPath()%>/information/getAllNews',
    striped: 'true',
    cache: false,
    sidePagination: 'server',
    pageSize: 10,
    pageList: [10, 20, 50],
    pageNumber: 1,
    pagination: true,
    columns: [
        {
            title: '序號',
            formatter:function(value,row,index){
                return index+1;
            }
        },
        {
            title: '標題',
            formatter:function(value,row,index){
                return '<a href="<%=request.getContextPath()%>/information/toNewsCenterDetails?serial='+row.serial+'">' +row.infotopic+'</a>';
            }
        },
        {
            field: 'createdate',
            title: '時間',
            formatter: function (value, row, index) {
                return changeDateFormat(value)
            }
        }
    ]
});

二、後端配置mysql

(1)pom.xml:git

<!--分頁器pagehelper-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.0.0</version>
</dependency>

(2)spring-mybatis.xml:github

<!-- spring和MyBatis完美整合,不須要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 自動掃描mapping.xml文件 -->
    <property name="mapperLocations" >
        <list>
            <value>classpath:com/wapssei/mapper/*.xml</value>
        </list>
    </property>

    <!--分頁-->
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties" >
                    <value >
                        <!--分頁插件會自動檢測當前的數據庫連接,自動選擇合適的分頁方式。 你能夠配置helperDialect屬性來指定分頁插件使用哪一種方言。配置時,可使用下面的縮寫值:-->
                        <!--oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby-->
                        <!--特別注意:使用 SqlServer2012 數據庫時,須要手動指定爲 sqlserver2012,不然會使用 SqlServer2005 的方式進行分頁。-->
                        helperDialect=sqlserver2012

                        <!--reasonable,默認爲false。爲 true 時,pageNum<=0 時查詢第一頁, pageNum>pages(超過總數時),查詢最後一頁。-->
                        <!--當 offsetAsPageNum=false 的時候,因爲 PageNum 問題,RowBounds查詢的時候 reasonable 會強制爲 false。使用 PageHelper.startPage 方法不受影響。-->
                        reasonable=true

                        <!--supportMethodsArguments,支持經過 Mapper 接口參數來傳遞分頁參數,默認值false,分頁插件會從查詢方法的參數值中,自動根據上面 params 配置的字段中取值,
                        查找到合適的值時就會自動分頁。 使用方法能夠參考測試代碼中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和ArgumentsObjTest。-->
                        supportMethodsArguments=true

                        <!--params:爲了支持startPage(Object params)方法,增長了該參數來配置參數映射,用於從對象中根據屬性名取值, 能夠配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默認值, 默認值爲pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。-->
                        params=count=countSql

                        <!--autoRuntimeDialect:默認值爲 false。 爲 true時,這樣在使用不一樣數據源時,會使用匹配的分頁進行查詢。 這種狀況下,你還須要特別注意 closeConn 參數,因爲獲取數據源類型會獲取一個數據庫鏈接,因此須要經過這個參數來控制獲取鏈接後,是否關閉該鏈接。 默認爲 true,有些數據庫鏈接關閉後就無法進行後續的數據庫操做。而有些數據庫鏈接不關閉就會很快因爲鏈接數用完而致使數據庫無響應。因此在使用該功能時,特別須要注意你使用的數據源是否須要關閉數據庫鏈接。-->
                        autoRuntimeDialect=true

                        <!--closeConn:默認值爲 true。當使用運行時動態數據源或沒有設置 helperDialect 屬性自動獲取數據庫類型時,會自動獲取一個數據庫鏈接, 經過該屬性來設置是否關閉獲取的這個鏈接,默認true關閉,設置爲 false 後,不會關閉獲取的鏈接,這個參數的設置要根據本身選擇的數據源來決定。-->
                        closeConn=true

                        <!--rowBoundsWithCount:默認值爲false,該參數對使用 RowBounds 做爲分頁參數時有效。 當該參數設置爲true時,使用 RowBounds 分頁會進行 count 查詢。-->
                        rowBoundsWithCount=true

                        <!--offsetAsPageNum:默認值爲 false,該參數對使用 RowBounds(offset、limit) 做爲分頁參數時有效。 當該參數設置爲 true 時,offset會當成 pageNum 使用,limit 和 pageSize 含義相同。-->
                        offsetAsPageNum=false

                        <!--pageSizeZero:默認值爲 false,當該參數設置爲 true 時,若是 pageSize=0 或者 RowBounds.limit = 0 就會查詢出所有的結果(至關於沒有執行分頁查詢,可是返回結果仍然是 Page 類型)。-->
                        pageSizeZero=true
                    </value>
                </property>
            </bean>
        </array>
    </property>
</bean>

(3)封裝後臺返回的數據格式,用於在BootstrapTable中顯示:spring

public class OffsetLimitPage {
    private Long total;
    private List rows;

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public OffsetLimitPage() {

    }

    public OffsetLimitPage(List rows, Long total) {
        this.rows = rows;
        this.total = total;
    }

    public OffsetLimitPage(Page rows) {
        this(rows,rows.getTotal());
    }
}

(4)Controller:sql

@ResponseBody
@RequestMapping(value = "getAllNews")
public OffsetLimitPage getAllNews(HttpServletRequest request, HttpServletResponse response, Model model,Integer offset,Integer limit){
    OffsetLimitPage result = newsInfoService.getAllNews(offset,limit);
    return result;
}

(5)Service.java:數據庫

public abstract OffsetLimitPage getAllNews(Integer offset, Integer limit);

(6)ServiceImpl.java:bootstrap

public OffsetLimitPage getAllNews(Integer offset, Integer limit){
    PageHelper.offsetPage(offset, limit);
    List<NewsInfo> newsInfoList = newsInfoMapper.selectAllNews();
    return new OffsetLimitPage((Page)newsInfoList);
}

(7)Mapper.java:(這裏不須要offset和limit參數)後端

List<NewsInfo> selectAllNews();

(8)Mapper.xml:(這裏不須要offset和limit參數)mybatis

<select id="selectAllNews" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List" />
  from Info
  where Type = 14 ORDER BY CreateDate DESC
</select>

三、運行結果:

相關文章
相關標籤/搜索