Front-end system three: display page implementation: create program business logic class Service class; (Service part of the Model of the paging module)

table of Contents

I. Overview

Two: Develop the PaintingService class: complete the program business logic class of the paging query part

As a result, the Service part of the Model is developed; that is, the Model part is developed.


I. Overview

This part is mainly the Service part in the Model layer of the MVC architecture: Then, the specific content of this blog is the Service part of the paging function:

Service is the most complete realization of all business logic;

mainly includes:

     (1) PaintingService class : a complete program business logic class , accepting Controller's request, below call Dao (part of Model part) class to get data; then PaintingService class itself can add a little other (non-data query) business code; Taken together, the Service class and Dao together form the Model;


Two: Develop the PaintingService class: complete the program business logic class of the paging query part

The specific business is:

package com.imooc.mgallery.service;

import java.util.List;

import com.imooc.mgallery.dao.PaintingDao;
import com.imooc.mgallery.entity.Painting;
import com.imooc.mgallery.utils.PageModel;

/**
 * 这个类主要职责:完成完成的程序业务逻辑;
 * 涉及到与底层数据交互的工作,交给Dao类去实现
 * @author dell
 *
 */
public class PaintingService {
	private PaintingDao paintingDao = new PaintingDao();
	/**
	 * 调用PaintingDao类的pagination()方法,获得分页数据;
	 * 这个类的内容看似和PageModel类的内容雷同,但是这个类还是必须的,在实际的开发中,需要遵从MVC原
	 * 则的按层逐级调用的规范;;;所以,即便没有其他的业务逻辑,我们也要写一个Service,然后让这个Service去调用Dao;
	 * @param page 当前第几页
	 * @param rows 每页有几条数据
	 * @return
	 */
	public PageModel pagination(int page,int rows) {
		if(rows == 0) {   // 可以看到,Service类中不但需要调用Dao来进行数据访问;
			// Service类还包括:一些前置条件的检查,以及得到调用结果后的后置数据的处理,这些工作都是与底层数据无关的
			// Service类中的方法用于处理完整的业务逻辑,Service类中方法需要尽量写的完整;
			// 而Dao中的方法只与底层数据进行交互的;;;
			// 所以,在这个例子中,即使Service类中的方法和Dao中的内容基本相同,也必须要要写这个Service类;
			throw new RuntimeException("无效的rows参数");
		}
		return paintingDao.pagination(page, rows);
	}
	
//	public static void main(String[] args) {   // 这个main方法,仅仅是开发时测试之用
//		PaintingService paintingService = new PaintingService();
//		PageModel pageModel = paintingService.pagination(2, 6);
//		List<Painting> paintingList = pageModel.getPageData();
//		for(Painting painting:paintingList) {
//			System.out.println(painting.getPname());
//		}
//		System.out.println(pageModel.getPageStartRow()+":"+pageModel.getPageEndRow());
//	}

}

A few descriptions of the PaintingService class:

(1) PaintingService class: the completed program business logic; this class provides complete services to the Controller part;

(2) After the Controller's request comes, the PaintingService class needs to accept parameters, preprocess the parameters, call the Dao part to get the returned data, and then (if necessary) do some processing on the data returned by Dao, and then send it to the Controller;

(3) Although the PaintingService has nothing to do, it is very simple; even the content is similar to Dao, but this class cannot be replaced; because this PaintingService class may need to contain some other (may be not directly related to obtaining data) business Processing, for example, in the above code, it is judged whether the rows parameter is 0;


As a result, the Service part of the Model is developed; that is, the Model part is developed.

 

 

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114836768