天天看點

中軟國際實訓全記錄——第五天實作對使用者清單的分頁式管理實作角色管理的資料庫以及初步操作個人總結

中軟國際實訓第五天——實作分頁式管理及

  • 實作對使用者清單的分頁式管理
  • 實作角色管理的資料庫以及初步操作
  • 個人總結

實作對使用者清單的分頁式管理

在昨天我們實作了對資料庫中的增添查改,是以在此處我們不再贅述如何實作,今天我們就主要來實作分頁式的管理以及對增添查改進行微調。

我們是使用MyBatis的插件PageHelper來實作分頁的,使用PageHelp需要我們在dependency中添加依賴字段。

<dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper</artifactId>
     <version>5.1.2</version>
</dependency>
           

之後等待項目自己下載下傳所需要的jar包,在下載下傳完成之後我們需要在ApplicationContext中配置PageHelper。

<property name="plugins">
    <array>
        <bean class="com.github.pagehelper.PageInterceptor">
            <property name="properties">
                <props>
                    <prop key="helperDialect">mysql</prop>
                    <prop key="reasonable">true</prop>
                </props>
            </property>
        </bean>
    </array>
</property>
           

我們需要将findAll函數中加上兩個int參數pages與size,用來标志是第幾頁與每頁最大的長度。還需要在UserInfoServiceImpl中添加PageHelper.startPage(pages, size);用來設定初始的值。

@Override
public List<UserInfo> findAll(int pages, int size){
    PageHelper.startPage(pages, size);
    return iUserInfoDao.findAll(pages, size);
}
           

在UserInfoController中我們要建立一個PageInfo的對象,将擷取到的使用者List儲存到其中。在pages與size前面添加一個@RequestParam用來設定一個初始值。

@RequestMapping("findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int pages, @RequestParam(defaultValue = "5") int size){
    ModelAndView modelAndView = new ModelAndView();
    List<UserInfo> userInfos = iUserInfoService.findAll(pages, size);
    PageInfo users = new PageInfo(userInfos);
    modelAndView.addObject("users",users);
    modelAndView.setViewName("user-list");
    return modelAndView;
}
           

接下來就需要修改user-list.jsp檔案,使用剛剛建立的PageInfo對象來傳遞資料。users.list來擷取userinfo資訊。

<tbody>
<c:forEach var="user" items="${users.list}">
		<tr>
			<td><input name="ids" type="checkbox"></td>
			<td>${user.id}</td>
			<td>${user.username}</td>
			<td>${user.password}</td>
			<td class="text-center">
				<a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
				<a href="${pageContext.request.contextPath}/user/delete.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
				<a href="#" class="btn bg-olive btn-xs">添加角色</a>
			</td>
		</tr>
	</c:forEach>
</tbody>
           

在分頁中我們需要使用使用users這一對象中的pageNumber與pages對象,來确定需要分頁的位置。pageNumber在一個循環中顯示可以儲存下需要多少資料的頁面,pages中總共有多少頁面。

</div>
<!-- /.box-body -->
	<div class="box-tools pull-right">
		<ul class="pagination">
			<li><a href="${pageContext.request.contextPath}/user/findAll.do?pages=1&size=5" aria-label="Previous">首頁</a></li>
			<li><a href="${pageContext.request.contextPath}/user/findAll.do?pages=${users.pageNum-1}&size=5">上一頁</a></li>
			<c:forEach begin="1" end="${users.pages}" var="pageNumber">
				<li><a href="${pageContext.request.contextPath}/user/findAll.do?pages=${pageNumber}&size=5">${pageNumber}</a></li>
			</c:forEach>
			<li><a href="${pageContext.request.contextPath}/user/findAll.do?pages=${users.pageNum+1}&size=5">下一頁</a></li>
			<li><a href="${pageContext.request.contextPath}/user/findAll.do?pages=${users.pages}&size=5" aria-label="Next">尾頁</a></li>
		</ul>
	</div>
</div>
           

實作角色管理的資料庫以及初步操作

要對角色管理,我們就需要在資料庫中建立兩張表role與user_role。

中軟國際實訓全記錄——第五天實作對使用者清單的分頁式管理實作角色管理的資料庫以及初步操作個人總結

在項目的對應檔案夾下建立接口與實作的類。

中軟國際實訓全記錄——第五天實作對使用者清單的分頁式管理實作角色管理的資料庫以及初步操作個人總結

個人總結

今天我們主要就是實作了查詢的分頁,首先就是需要在dependency中添加PageHelp的依賴,之後需要在ApplicationContext中配置PageHelp。在使用的時候我們需要pages與size來确定是哪一個頁面與資料的數量。在jsp檔案中使用封裝好的PageInfo來傳遞資料供頁面使用。