Struts+Hibernate常用工具類

建立相關工具包

1.分頁信息包:存儲頁面分頁信息
2.Hibernate工具包:用於創建session
3.BeanFactory包:用於創建Bean

分頁信息中除了結果集以外,還要包含其他的一些索引信息,便於前臺操作

package com.houlu.drp.utils;

import java.util.List;

/**
 * 分頁信息類
 * @author Administrator
 * @param <T>
 *
 */
public class PageModel<T> {

    //結果集
    private List<T> list;

    //記錄數
    private int totalRecords;

    //每頁多少條數據
    private int pageSize;

    //第幾頁
    private int pageNo;

    //總頁數
    private int totalPages;

    //首頁
    private int topPageNo;

    //前一頁
    private int previousPageNo;

    //下一頁
    private int nextPageNo;


    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getTopPageNo() {
        return topPageNo;
    }

    public void setTopPageNo(int topPageNo) {
        this.topPageNo = topPageNo;
    }

    public int getPreviousPageNo() {
        return previousPageNo;
    }

    public void setPreviousPageNo(int previousPageNo) {
        this.previousPageNo = previousPageNo;
    }

    public int getNextPageNo() {
        return nextPageNo;
    }

    public void setNextPageNo(int nextPageNo) {
        this.nextPageNo = nextPageNo;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }


}

HibernateUtil用於建立Session,過程如下:
1.利用hibernate.cfg.xml文件建立Configuration類。
2.利用Configuration建立Session的工廠,factory作爲該類的屬性
以上兩步在靜態代碼塊中完成。
3.對外提供取得session的方法

package com.houlu.drp.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

    private static SessionFactory factory;

    static{
        try {
            //讀取hibernate.cfg.xml文件
            Configuration cfg = new Configuration().configure();
            //建立SessionFactory工廠
            factory = cfg.buildSessionFactory();
        }catch(Exception e) {
            e.printStackTrace();
            throw new java.lang.RuntimeException(e);
        }   
    }

    public static SessionFactory getSessionFactory(){
        return factory;
    }

    /**
     * 取得Session
     * @return
     */
    public static Session getSession() {
        return factory.openSession();
    }

    public static void closeSession(Session session) {
        if (session != null) {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
}
package com.houlu.drp.utils;

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


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.houlu.drp.usermgr.dao.UserDao;
import com.houlu.drp.usermgr.dao.impl.UserDao4SqlServerImpl;
import com.houlu.drp.usermgr.service.UserService;
import com.houlu.drp.usermgr.service.impl.UserService4BeanFactoryImpl;


BeanFactory在沒使用Spring的情況下用於建立對象,該類利用單例模式生成,將生成的bean存入map中,其他人從這裏取bean時,利用newInstance生成,該方法生成的對象同時也是單例的
/**
 * Bean工廠
 * @author Administrator
 *
 */
public class BeanFactory {

    private static BeanFactory instance = new BeanFactory();

    //存放產品{key=產品編號,value=具體產品實例}
    private Map beans = new HashMap();
    //讀取配置文件
    private Document doc;

    private BeanFactory() {
        try {
            doc = new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream("beanFactoryConfig.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static BeanFactory getInstance() {
        if (instance == null) {
            synchronized (BeanFactory.class) {
                if (instance == null) {
                    instance = new BeanFactory();
                }
            }
        } 
        return instance;
    }

    /**
     * 根據產品標識得到產品
     * @param id
     * @return
     */
    public  Object  getBean(Class c) {
        synchronized(beans) {
            //如果在Map存在已經創建的產品返回
            if (beans.containsKey(c.getName())) {
                return beans.get(c.getName());
            }
            //查找xml文件中id屬性等於某值的bean標籤

            Object object = null;
            try {
                Element elt = (Element)doc.selectObject("//bean[@id=\"" + c.getName() + "\"]");
                object = Class.forName(elt.attributeValue("class")).newInstance();
                //將創建好的產品放入到Map中
                beans.put(c.getName(), object);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }
    }

    public static void main(String[] args) {

    //  ItemService itemService = (ItemService)BeanFactory.getInstance().getBean(ItemService.class);
    //  System.out.println(itemService);
        UserDao userDao = (UserDao)BeanFactory.getInstance().getBean(UserDao.class);
        System.out.println(userDao);
        UserService userService1 = (UserService)BeanFactory.getInstance().getBean(UserService.class);
        UserService userService2 = (UserService)BeanFactory.getInstance().getBean(UserService.class);
        System.out.println(userService1==userService2);
    }
}

對應的xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="com.houlu.drp.usermgr.dao.UserDao" class="com.houlu.drp.usermgr.dao.impl.UserDao4SqlServerImpl"/>
    <bean id="com.houlu.drp.usermgr.service.UserService" class="com.houlu.drp.usermgr.service.impl.UserService4BeanFactoryImpl"/>
</beans>
發佈了35 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章