1. 程式人生 > >ssm框架專案功能點

ssm框架專案功能點

一,模糊查詢,條件查詢,分頁


在mvc 層中 ,分頁的賦值等,都寫在service層,原因是在service層設定了事務,如果程式有錯。service層具有回滾,但是controller層並不具備。
一個通用的pageUtil,可以對多個類和多個頁面進行分頁,pageUtil可以為被繼承的方式來進行分頁。在controller層中,獲取引數也可以直接用pageUtil或者被繼承的類來直接獲取。
例:一個商品介面的模糊,按條件分頁查詢:
商品通過模糊名字和商品價位查詢
1.寫一個通用的pageutil類:

private Integer currPage = 1;
	
	private Integer pageSize =
4; private Integer count; private Integer totalPage; private List<?> data;

list的泛型用? 增加通用性。 當前頁currPage 初始值設為1 ,每頁條數pageSize 設為4

public PageUtil(Integer currPage, Integer count, List<?> data) {
		this.currPage = currPage;
		this.count = count;
		this.data = data;
		// 計算總頁數
		this
.totalPage = this.count % this.pageSize == 0 ? this.count / this.pageSize : this.count / this.pageSize + 1; if(this.totalPage == 0) this.totalPage = 1; }

用有參函式給5個賦值,引數只有三個,通過建構函式計算其他變數。
注意:計算分頁的起始位置因為是通過pageutil物件來進行傳值,在mapper.xml檔案裡的limit 語句 並不能計算分頁的起始位置:所以寫一個get方法

/**
	 * 計算分頁的起始位置
	 * @return
	 */
public Integer getStart() { return (this.currPage - 1 ) * this.pageSize; }

寫一個ProductQuery類,搜尋類,繼承PageUtil方法。

@Alias("pq")
@Setter@Getter@ToString
public class ProductQuery extends PageUtil {

	private String qqName;
	
	private Integer minPrice;
	
	private Integer maxPrice;

重點:

// 當前頁列表
	List<Product> getList(ProductQuery pq);
	
	// 總頁數
	Integer getCount(ProductQuery pq);

在mapper的介面中 直接傳ProductQuery 物件,因為繼承了PageUtil。

<!-- 分頁 + 組合查詢 -->
	<sql id="where_sql">
		<where>
			<if test="qqName != null">
				and pname like concat('%', #{qqName}, '%')
			</if>
			<if test="minPrice != null">
				and price >= #{minPrice}
			</if>
			<if test="maxPrice != null">
				and price &lt;= #{maxPrice}
			</if>		
		</where>
	</sql>
	<select id="getList" resultType="product">
		select p.*, t.tname typeName from tab_product p 
		left join tab_type t on p.tid = t.id
		<include refid="where_sql"/>
		limit #{start}, #{pageSize}
	</select>
	<select id="getCount" resultType="int">
		select count(1) from tab_product
		<include refid="where_sql"/>
	</select>

要注意的是 include refid=“where_sql”,因為兩個sql語句都有大量相同的語句 所以可以用include 引入相同語句。

@Override
	public PageUtil list(ProductQuery pq) {
		// 所有的操作都在服務層進行
		
		// pq:
		// 查詢條件: 
		// 分頁資訊:pageUtil
		
		// 1. 查詢當前頁的列表
		List<Product> list = pm.getList(pq);
		
		// 2. 查詢總記錄數
		Integer total = pm.getCount(pq);
		
		PageUtil pu = new PageUtil(pq.getCurrPage(), total, list);
		
		return pu;
	}

在service所有的操作都在服務層進行。

@RequestMapping("/productList")
	public String list(Model m, @ModelAttribute("pq")ProductQuery pq) {
		
		System.out.println(pq);
		
		PageUtil pu = ps.list(pq);
		
		m.addAttribute("pu", pu);
		
		return "productList";
	}
	

controller層中 傳ProductQuery,需要用註解,其原因是如果查詢後多條資料 需要下一頁,則要資料回顯。使得點選第二頁的時候不會重新整理。

<form id="queryForm" action="productList" method="post">
					
					<!-- 隱藏一個當前頁的資訊 -->
					<input id="currPage" type="hidden" name="currPage" value="1"/>
				
					名稱:<input type="search" name="qqName" value="${pq.qqName }">
					<br/>
					價格區間:<input type="number" name="minPrice" value="${pq.minPrice }"> - 
					<input type="number" name="maxPrice" value="${pq.maxPrice }">
					
					<input type="submit" value="查詢"> 
				</form>
<td colspan="9" >
				<a href="javascript:void(0)" onclick="page(1)">首頁</a>
				<a href="javascript:void(0)" onclick="page(${pu.currPage - 1 > 1 ? pu.currPage - 1 : 1})">上一頁</a>
				<a href="javascript:void(0)" onclick="page(${pu.currPage + 1 < pu.totalPage ? pu.currPage + 1 : pu.totalPage })">下一頁</a>
				<a href="javascript:void(0)" onclick="page(${pu.totalPage })">末頁</a>
				當前第${pu.currPage }/共${pu.totalPage }</td>



function page(currPage) {
	$("#currPage").val(currPage);
	$("#queryForm").submit();	
}


其介面為:
在這裡插入圖片描述
在這裡插入圖片描述

二,修改商品上下架功能

商品有spzt的功能 0和1代表上架狀態和下架狀態
功能通過ajax實現。

<c:if test="${p.spzt == 0 }">
							<input type="button" value="上架" onclick="editSpzt(${p.id}, 1)"/>
						</c:if>
						<c:if test="${p.spzt == 1 }">
							<input type="button" value="下架" style="color:red" onclick="editSpzt(${p.id}, 0)"/>
						</c:if>

jsp頁面,以下是ajax

function editSpzt(proId, state) {
	
	if(confirm("確定要修改編號為 " + proId + " 商品的狀態?")) {
		
		$.post("editSpzt", {proId: proId, state: state}, function(data){
			
			if(data) {
				// 重新整理頁面
				location.reload();
			} else {
				alert("網路異常,請稍後重試!");
			}
			
		}, "json");
		
	}
	
}

在controller層中

/**
	 * 修改商品的狀態
	 * @param proId
	 * @param state
	 * @return
	 */
	@RequestMapping("/editSpzt")
	@ResponseBody
	public String editSpzt(Integer proId, Integer state) {
		String flag = "false";
		
		try {
			ps.editSpzt(proId, state);
			flag = "true";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return flag;
	}