SSH(struts+spring+hibernate)迅速开发--第八章 浏览和选购商品(1)

来源:互联网 发布:食品安全数据 编辑:程序博客网 时间:2024/05/22 15:12

第八章 浏览和选购商品

 

User模块的实现差不多,浏览和选购商品模块,我们也把它划分视图,struts action, services,hibernate dao四个层次,学习这章的作用,我们除了重复温习怎么样编写每个层次的实现外,我们将进一步体会如下几点:

1.       体会SpringBean的注册

2.       体会Spring,注册多个服务

3.       服务的事务管理

4.       基于Hibernate的分页管理

接下来我们一步步的实现:

1.       编写ShopService

2.       注册ShopService

3.       编写配置FormAction

4.       添加资源信息

5.       编写jsp页面

 

 

1.    编写ShopService接口和实现类

根据我们对需求的分析,ShopService主要实现三个功能:一是根据商品名称模糊查询,并实现分页;一是计算分页信息接口(页面总数、当前页码数、每页显示的记录数);一是根据商品的编号,查找商品的详细信息,具体代码如下:

ShopService.java

/**

 *

 */

package cn.com.book.demo.services;

 

import java.util.List;

import java.util.Map;

 

import cn.com.book.demo.hibernate.dao.Shop;

 

/**

 * @author Noble.Yang

 *

 */

public interface ShopServices {

       public static final String SHOP_LIST = "shopList";

       public static final String PAGE_COUNT = "pageCount";

       public static final String CURR_PAGE_NO = "currPageNo";

       public static final String PAGE_SIZE = "pageSize";

       /**

        * 根据条件,分页查找商品

        *

        * @param shopName 商品名称,模糊查询

        * @param targetPage 目标页码,-1表示不分页

        * @param pageSize 每页显示的商品数,-1表示所有

        *

        * @return List

        * */

    public List findShops(String shopName, int targetPage, int pageSize);

   

    /**

     * 根据条件,分页查找

     *

     * @param shopName 商品名称,模糊查询

        * @param targetPage 目标页码,-1表示不分页

        * @param pageSize 每页显示的商品数,-1表示所有

        *

        * @return Map 包含商品列表,一共多少页,当前页码,每页数量

     * */

    public Map findShopsMsg(String shopName, int targetPage, int pageSize);

   

    /**

     * 根据id查找对应的商品

     * */

    public Shop findShopById(int id);

}

 

定义好接口后,我们接着就是实现该接口的实现类:ShopServiceImpl.java,代码如下:

/**

 *

 */

package cn.com.book.demo.services.impl;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import cn.com.book.demo.hibernate.dao.Shop;

import cn.com.book.demo.hibernate.dao.ShopDAO;

import cn.com.book.demo.services.ShopServices;

 

/**

 * @author Noble.Yang

 *

 */

public class ShopServicesImpl implements ShopServices {

       private ShopDAO shopDAO = null;

 

       public void setShopDAO(ShopDAO shopDAO) {

              this.shopDAO = shopDAO;

       }

 

       /*

        * (non-Javadoc)

        *

        * @see cn.com.book.demo.services.ShopServices#findShops(java.lang.String,

        *      int, int)

        */

       public List findShops(String shopName, int targetPage, int pageSize) {

              List shops = null;

              this.initDAO();

 

              String hsql = null;

              Map params = null;

              if (this.isNullString(shopName)) {

                     hsql = "from Shop";

                     shops = this.shopDAO.getAllByHql(hsql, targetPage, pageSize);

              } else {

                     hsql = "from Shop shop where shop." + ShopDAO.NAME + " like :name";

                     params = new HashMap();

                     params.put("name", shopName);

                     shops = this.shopDAO

                                   .getAllByHql(hsql, targetPage, pageSize, params);

              }

 

              return shops;

       }

 

       /*

        * (non-Javadoc)

        *

        * @see cn.com.book.demo.services.ShopServices#findShopsMsg(java.lang.String,

        *      int, int)

        */

       public Map findShopsMsg(String shopName, int targetPage, int pageSize) {

              Map map = new HashMap();

              List shops = new ArrayList();

              int pageCount = 0;

 

              this.initDAO();

 

              String hsql = null;

              Map params = null;

              if (this.isNullString(shopName)) {

                     hsql = "from Shop";

                     shops = this.shopDAO.getAllByHql(hsql,targetPage, pageSize);

                     pageCount = this.shopDAO.getPages(hsql, pageSize);

              } else {

                     hsql = "from Shop shop where shop." + ShopDAO.NAME + " like :name";

                     params = new HashMap();

                     params.put("name", shopName);

                     shops = this.shopDAO

                                   .getAllByHql(hsql, targetPage, pageSize, params);

                     pageCount = this.shopDAO.getPages(hsql, pageSize, params);

              }

        // 保存当前的页码

              map.put(ShopServices.CURR_PAGE_NO, new Integer(targetPage));

        // 保存总共页面数量

              map.put(ShopServices.PAGE_COUNT, new Integer(pageCount));

        // 保存每页显示的记录数量

              map.put(ShopServices.PAGE_SIZE, new Integer(pageSize));

        // 保存当前页面的商品列表

              map.put(ShopServices.SHOP_LIST, shops);

 

              return map;

       }

      

   // 根据商品的Id,查找商品的详细信息

       public Shop findShopById(int id){

              Shop shop = null;

             

              shop = this.shopDAO.findById(id);

             

              return shop;

       }

   

    // 初始化服务中用到的dao对象

       private void initDAO() {

              if (this.shopDAO == null)

                     this.shopDAO = new ShopDAO();

       }

    // 判断字符串是否为null或者是否是长度为零

       private boolean isNullString(String str) {

              boolean bool = false;

              if (str == null || str.trim().equals("")) {

                     bool = true;

              }

              return bool;

       }

}
原创粉丝点击