Service层代码编写:
现在开始编写 Service 层代码:
在 com.game.products.services.iface 包中新建 ProductsService 接口,代码如下:
- package com.game.products.services.iface;
- import java.util.List;
- import com.game.products.model.Products;
- public interface ProductsService {
- void addProduct(Products pd); // 添加记录
- void deleteProduct(Products pd); // 删除记录
- List getProducts(); // 获得所有记录
- int getRows();; // 获得总行数
- List getProducts( int pageSize, int startRow) ; // 获得一段记录
- Products getProduct(String gameId); // 根据ID获得记录
- String getMaxID(); // 获得最大ID值
- void updateProductd(Products pd); // 修改记录
- List queryProducts(String fieldname,String value); // 根据条件查询的所有记录
- int getRows(String fieldname,String value); // 获得总行数
- List queryProducts(String fieldname,String value, int pageSize, int startRow); // 根据条件查询的一段记录
- }
在 com.game.products.services 包中新建 ProductsServiceImp 类,这个类实现了 ProductsService 接口,代码如下:
- package com.game.products.services;
- import java.util.List;
- import com.game.products.dao.iface.ProductsDao;
- import com.game.products.model.Products;
- import com.game.products.services.iface.ProductsService;
- public class ProductsServiceImp implements ProductsService {
- private ProductsDao productsDao;
- public ProductsServiceImp() {}
- /** */ /**
- * 函数说明:添加信息
- * 参数说明:对象
- * 返回值:
- */
- public void addProduct(Products pd) {
- productsDao.addProduct(pd);
- }
- /** */ /**
- * 函数说明:删除信息
- * 参数说明: 对象
- * 返回值:
- */
- public void deleteProduct(Products pd) {
- productsDao.deleteProduct(pd);
- }
- /** */ /**
- * 函数说明:获得所有的信息
- * 参数说明:
- * 返回值:信息的集合
- */
- public List getProducts() {
- return productsDao.getProducts();
- }
- /** */ /**
- * 函数说明:获得总行数
- * 参数说明:
- * 返回值:总行数
- */
- public int getRows() {
- return productsDao.getRows();
- }
- /** */ /**
- * 函数说明:获得一段信息
- * 参数说明:
- * 返回值:信息的集合
- */
- public List getProducts( int pageSize, int startRow) {
- return productsDao.getProducts(pageSize, startRow);
- }
- /** */ /**
- * 函数说明:获得一条的信息
- * 参数说明: ID
- * 返回值:对象
- */
- public Products getProduct(String gameId) {
- return productsDao.getProduct(gameId);
- }
- /** */ /**
- * 函数说明:获得最大ID
- * 参数说明:
- * 返回值:最大ID
- */
- public String getMaxID() {
- return productsDao.getMaxID();
- }
- /** */ /**
- * 函数说明:修改信息
- * 参数说明: 对象
- * 返回值:
- */
- public void updateProductd(Products pd) {
- productsDao.updateProductd(pd);
- }
- /** */ /**
- * 函数说明:查询信息
- * 参数说明: 集合
- * 返回值:
- */
- public List queryProducts(String fieldname,String value) {
- return productsDao.queryProducts(fieldname, value);
- }
- /** */ /**
- * 函数说明:获得总行数
- * 参数说明:
- * 返回值:总行数
- */
- public int getRows(String fieldname,String value) {
- return productsDao.getRows(fieldname, value);
- }
- /** */ /**
- * 函数说明:查询一段信息
- * 参数说明: 集合
- * 返回值:
- */
- public List queryProducts(String fieldname,String value, int pageSize, int startRow) {
- return productsDao.queryProducts(fieldname, value,pageSize,startRow);
- }
- public ProductsDao getProductsDao() {
- return productsDao;
- }
- public void setProductsDao(ProductsDao productsDao) {
- this .productsDao = productsDao;
- }
- }
基本的业务层代码就这些了。因为还有分页的业务,所以接下来编写分页的代码。
分页是个公共的类,所以放在 com.game.commons 中。
Pager 类,封装了分页需要的属性,代码如下:
- package com.game.commons;
- import java.math. * ;
- public class Pager {
- private int totalRows; // 总行数
- private int pageSize = 30 ; // 每页显示的行数
- private int currentPage; // 当前页号
- private int totalPages; // 总页数
- private int startRow; // 当前页在数据库中的起始行
- public Pager() {
- }
- public Pager( int _totalRows) {
- totalRows = _totalRows;
- totalPages = totalRows / pageSize;
- int mod = totalRows % pageSize;
- if (mod > 0 ) {
- totalPages ++ ;
- }
- currentPage = 1 ;
- startRow = 0 ;
- }
- public int getStartRow() {
- return startRow;
- }
- public int getTotalPages() {
- return totalPages;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setTotalRows( int totalRows) {
- this .totalRows = totalRows;
- }
- public void setStartRow( int startRow) {
- this .startRow = startRow;
- }
- public void setTotalPages( int totalPages) {
- this .totalPages = totalPages;
- }
- public void setCurrentPage( int currentPage) {
- this .currentPage = currentPage;
- }
- public void setPageSize( int pageSize) {
- this .pageSize = pageSi, e.display='inline'; Codehighlighter1_784_815_Closed_Text.style.display='inline';" src="http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top> public void setStartRow( int startRow) {
- this .startRow = startRow;
- }
- public void setTotalPages( int totalPages) {
- this .totalPages = totalPages;
- }
- public void setCurrentPage( int currentPage) {
- this .currentPage = currentPage;
- }
- public void setPageSize( int pageSize) {
- this .pageSize = pageSize;
- }