Spring Boot实践应用开发(10) - Go语言中文社区

Spring Boot实践应用开发(10)


【检索功能】

 

准备,下载seventh.zip并导入到eclipse,下载地址http://pan.baidu.com/s/1o6spOJS

 

1,新规UsersInputForm.java

package org.com.sample.form;

import lombok.Data;

@Data
public class UsersInputForm {

	private String username;
}

 

2,修改UsersExtMapper.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.com.sample.mapper.ext.UsersExtMapper">
	<resultMap id="BaseResultMap" type="org.com.sample.dataset.ext.UsersExt">
		<id column="USERNAME" property="username" jdbcType="VARCHAR" />
		<result column="PASSWORD" property="password" jdbcType="VARCHAR" />
		<result column="ROLENAME" property="rolename" jdbcType="VARCHAR" />
	</resultMap>

	<sql id="Base_Column_Ext_List">
		USERNAME, PASSWORD, ROLENAME
	</sql>

	<sql id="Base_Column_WithTbl_Ext_List">
		u.USERNAME, u.PASSWORD, r.ROLENAME
	</sql>

	<select id="countForPaging" parameterType="map" resultType="java.lang.Integer">
		select count(*) from USERS u
		<where>
			<if test="form.username != null">
				and u.USERNAME like
				concat(concat('%',#{form.username,jdbcType=VARCHAR}),'%')
			</if>
		</where>
	</select>

	<select id="selectMcUserExtForPaging" resultMap="BaseResultMap"
		parameterType="map">
		select
		<include refid="Base_Column_Ext_List" />
		from (
		select
		<include refid="Base_Column_WithTbl_Ext_List" />
		, ROWNUM line
		from USERS u
		left join ROLES r
		on u.ROLE = r.ROLE
		<where>
			<if test="end_line != null">
				and ROWNUM <= #{end_line,jdbcType=DECIMAL}
			</if>
			<if test="form.username != null">
				and u.USERNAME like
				concat(concat('%',#{form.username,jdbcType=VARCHAR}),'%')
			</if>
		</where>
		<if test="orderByClause != null">
			order by ${orderByClause}
		</if>
		)
		<where>
			<if test="start_line != null">
				and line >= #{start_line,jdbcType=DECIMAL}
			</if>
		</where>
	</select>
</mapper>

3,修改users.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Users</title>
<meta
	content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'
	name='viewport' />

<meta th:replace="head-cssjs" />

</head>
<body class="skin-blue">
	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>Users</h1>
			</section>

			<!-- Main content -->
			<section class="content">
				<div class="row">
					<div class="col-md-12">
						<form id="usersInputForm" th:object="${usersInputForm}"
							method="post" class="form-horizontal">
							<div id="maincontent" class="box box-primary"
								style="overflow-y: auto; overflow-x: hidden;">
								<div class="box-body">
									<div class="form-group">
										<div class="col-md-12">
											<div class="row">
												<div class="col-md-12">
													<table class="table-condensed"
														style="border: 0; margin: 0; cellSpacing: 0; cellPadding: 0;">
														<tr>
															<td class="col-md-2">用户名</td>
															<td class="col-md-2"><input type="text"
																class="form-control" id="username"
																th:field="*{username}" maxlength="20" /></td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
														</tr>
													</table>
												</div>
											</div>
											<div class="row">
												<div class="col-md-12">
													<table class="table-condensed"
														style="border: 0; margin: 0; cellSpacing: 0; cellPadding: 0;">
														<tr>
															<td class="col-md-2">
																<button id="doSearch" type="button"
																	class="btn btn-primary" style="width: 150px">検索</button>
															</td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
															<td class="col-md-2"></td>
														</tr>
													</table>
												</div>
											</div>

											<div th:if="${page} != null and ${ph} != null " class="row">
												<div class="col-md-12">
													<p class="pageList">
														检索结果 <span th:text="${page.totalElements}"></span>件 <span
															class="prev"
															th:attr="style=${ph.hiddenFirst} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=0,size=${page.size})}"
															id="searchIdFirst"><<</a></span> <span class="prev"
															th:attr="style=${ph.hiddenPrev} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=${page.number - 1},size=${page.size})}"><</a></span>
														<span th:text="${page.number + 1}"></span> / <span
															th:text="${page.totalPages}"></span> 页 <span class="next"
															th:attr="style=${ph.hiddenNext} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=${page.number + 1},size=${page.size})}">></a></span>
														<span class="next"
															th:attr="style=${ph.hiddenLast} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=${page.totalPages - 1},size=${page.size})}">>></a></span>
													</p>
												</div>
											</div>
											<div class="box box-success"
												th:attrappend="style=${page == null ? 'display: none;' : ''}">
												<div class="col-md-12" style="width: 99%;">
													<table id="searchResult"
														class="table table-condensed table-striped table-hover">
														<thead>
															<tr>
																<th class="col-md-1">No.</th>
																<th class="col-md-1">username</th>
																<th class="col-md-1">password</th>
																<th class="col-md-1">rolename</th>
															</tr>
														</thead>
														<tbody>
															<tr th:each="content, iterStat : ${page}">
																<td class="col-md-1" th:id="${iterStat.index}"
																	th:name="${iterStat.index}"
																	th:value="${iterStat.index}"
																	th:text="${iterStat.index} + 1"></td>
																<td class="col-md-1"
																	th:id="'page['+${iterStat.index}+'].username'"
																	th:name="'page['+${iterStat.index}+'].username'"
																	th:value="${content.username}"
																	th:text="${content.username}"></td>
																<td class="col-md-1"
																	th:id="'page['+${iterStat.index}+'].password'"
																	th:name="'page['+${iterStat.index}+'].password'"
																	th:value="${content.password}"
																	th:text="${content.password}"></td>
																<td class="col-md-1"
																	th:id="'page['+${iterStat.index}+'].rolename'"
																	th:name="'page['+${iterStat.index}+'].rolename'"
																	th:value="${content.rolename}"
																	th:text="${content.rolename}"></td>
															</tr>
														</tbody>
													</table>
												</div>
											</div>
											<div th:if="${page} != null and ${ph} != null " class="row">
												<div class="col-md-12">
													<p class="pageList">
														检索结果 <span th:text="${page.totalElements}"></span>件 <span
															class="prev"
															th:attr="style=${ph.hiddenFirst} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=0,size=${page.size})}"
															id="searchIdFirst"><<</a></span> <span class="prev"
															th:attr="style=${ph.hiddenPrev} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=${page.number - 1},size=${page.size})}"><</a></span>
														<span th:text="${page.number + 1}"></span> / <span
															th:text="${page.totalPages}"></span> 页 <span class="next"
															th:attr="style=${ph.hiddenNext} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=${page.number + 1},size=${page.size})}">></a></span>
														<span class="next"
															th:attr="style=${ph.hiddenLast} ? 'visibility:hidden'"><a
															class="js-pagenation"
															th:href="@{/sample/users/show(page=${page.totalPages - 1},size=${page.size})}">>></a></span>
													</p>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</section>
			<!-- /.content -->
		</div>
		<!-- /.content-wrapper -->
	</div>
	<!-- ./wrapper -->

	<div th:replace="bottom-js"></div>

	<script th:inline="javascript">
		/*<![CDATA[*/
		$(document).ready(function() {

			var table = $('#searchResult').DataTable({
				scrollY : "390px",
				scrollX : true,
				scrollCollapse : true,
				paging : false,
				searching : false,
			});

			new $.fn.dataTable.FixedColumns(table, {
				leftColumns : 1
			});

			$('.js-pagenation').each(function() {
				$(this).click(function() {
					$('#usersInputForm').attr('action', $(this).attr('href'));
					$(this).attr('href', '#');
					$('#usersInputForm').submit();
				});
			});

			$('#usersInputForm').focus();

			$('#doSearch').bind(
					'click',
					function() {
						$('#usersInputForm').attr("action",
								/*[[@{/sample/users/doSearch}]]*/);
						$('#usersInputForm').submit();
					});
		});
		/*]]>*/
	</script>
</body>
</html>

 

4,修改UsersController.java

package org.com.sample.controller;

import java.util.HashMap;
import java.util.Map;

import org.com.common.paging.PagenationHelper;
import org.com.sample.dataset.Users;
import org.com.sample.facade.UsersFacade;
import org.com.sample.form.UsersInputForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/sample/users")
public class UsersController {

	private static final Logger logger = LoggerFactory.getLogger(UsersController.class);

	private static final int DEFAULT_PAGEABLE_SIZE = 10;

	@Autowired
	UsersFacade usersFacade;

	@RequestMapping(value = "/show", method = { RequestMethod.GET, RequestMethod.POST })
	public String selectAllUser(@PageableDefault(page = 0, size = DEFAULT_PAGEABLE_SIZE) Pageable pageable,
			Model model) {

		// List<Users> userList = usersFacade.selectByExample(new
		// UsersExample());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start_line", pageable.getOffset() + 1);
		map.put("end_line", (pageable.getPageNumber() + 1) * pageable.getPageSize());
		map.put("orderByClause", "username asc");
		map.put("form", new UsersInputForm());

		Page<Users> page = usersFacade.selectUserListForPaging(map, pageable);
		PagenationHelper ph = new PagenationHelper(page.getNumber(), page.getSize(), page.getTotalPages());

		model.addAttribute("page", page);
		model.addAttribute("ph", ph);

		return "sample/users/users";
	}

	@RequestMapping(value = "/doSearch", method = { RequestMethod.GET, RequestMethod.POST })
	public String doSearch(UsersInputForm form, BindingResult result,
			@PageableDefault(page = 0, size = DEFAULT_PAGEABLE_SIZE) Pageable pageable, Model model) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start_line", pageable.getOffset() + 1);
		map.put("end_line", (pageable.getPageNumber() + 1) * pageable.getPageSize());
		map.put("orderByClause", "username asc");
		map.put("form", form);

		Page<Users> page = usersFacade.selectUserListForPaging(map, pageable);
		PagenationHelper ph = null;

		if (page != null) {
			ph = new PagenationHelper(page.getNumber(), page.getSize(), page.getTotalPages());
		}

		model.addAttribute("page", page);
		model.addAttribute("ph", ph);

		return "sample/users/users";
	}

	@ModelAttribute
	public UsersInputForm getUsersInputForm() {
		return new UsersInputForm();
	}

}


5, 启动项目,输入URL,http://localhost:8888/sample/users/show

 

 

 


代码下载地址

http://pan.baidu.com/s/1o6spOJS,eighth.zip

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/engchina/article/details/49045517
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-27 17:06:42
  • 阅读 ( 1197 )
  • 分类:Go应用

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢