⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catalogdao.java

📁 网上购物系统struts+spring+hibernate实现
💻 JAVA
字号:
package com.sdi0708.bdifn.bookstore.dao.impl;import java.util.ArrayList;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import com.sdi0708.bdifn.bookstore.dao.ICatalogDao;import com.sdi0708.bdifn.bookstore.domain.Catalog;public class CatalogDao extends BaseDao implements ICatalogDao {	/**	 * 因为类别不常改变,把它放到内存中,以空间换时间	 */	private List<Catalog> catalogs = null;  		/**	 * 增加一个类别,如果进行了此操作,清空缓存的集合信息	 */	public void addCatalog(Catalog catalog,Long parentId) {				Session session = openSession();		Catalog temp = catalog;		//获取下一个主键值.自定义算法实现		temp.setId(getPK(parentId));		session.save(catalog);		//可以考虑用spring的aop实现,看时间允许与否		clearCatalog();	}	public void deleteById(Long id) {		Session session = openSession();		String hql = "";		Query query = null;		if(id % 100 == 0) {			//如果是顶层的类别,根结点的话的,要把子结点也要删除			Long hid = id / 100; //取得高位,			Long cid = hid * 100 + 99; //xx00 到 xx99都删除						hql = "delete from Catalog c  where c.id between ? and ? ";			query = session.createQuery(hql);			query.setLong(0, id);			query.setLong(1, cid);		} else {			//如果是叶子节点,可以直接删除.			hql = "delete from Catalog c where c.id = ?";			query = session.createQuery(hql);			query.setLong(0, id);		}		query.executeUpdate();		clearCatalog();	}	@SuppressWarnings("unchecked")	public List<Catalog> findAllCatalogs() {		if(catalogs == null) {			synchronized(this) {				Session session = openSession();				String hql = "from Catalog c order by c.id";				Query query = session.createQuery(hql);				catalogs = query.list();			}		}		return catalogs;	}	public Catalog findById(Long id) {		Catalog catalog = null;		if(catalogs != null) {			//先从列表中找			for(int i = 0 ; i < catalogs.size() ; i ++) {				Catalog c = catalogs.get(i);				if(c.getId().equals(id)) {					catalog = c;				}			}		} else {			Session session = openSession();			catalog = (Catalog) session.get(Catalog.class, id);		}		return catalog;	}	public void modifyCatalod(Catalog catalog) {		Session session = openSession();		session.update(catalog);		clearCatalog();	}	public void clearCatalog() {		//如果发生了任何的改变,则把catalogs置空		if(catalogs != null && !catalogs.isEmpty())		catalogs.clear();		catalogs = null;	}	//为四位数	@SuppressWarnings("unchecked")	public Long getPK(Long parent) {		Session session = openSession();		Long key = null;		Long h = parent;				String hql = "select max(id) from Catalog";		Query q = session.createQuery(hql);		List result = q.list();				//表示增加顶层类别		if(h == null){			if(result != null && result.size() == 1) {				h = (Long) result.get(0)/ 100 + 1;			}			//如果是没有记录,从10起.下一个是11下下一个是12			else h = 10L;			//它是需层类别,则后两个为零			key = h * 100 ;		} else {			//把后两位数去掉			key = (h /100) * 100 + (Long) result.get(0) % 100 + 1;		}		return key;	}	/**	 * 取得所有的顶层类别	 */	public List<Catalog> findParentCatalogs() {		List<Catalog> catas = new ArrayList<Catalog>();		List<Catalog> cs =  findAllCatalogs();		for(int i = 0 ; i < cs.size() ; i ++) {			Catalog c = cs.get(i);			if(c.getId() % 100 == 0) {				//如果第二位ID位是00的话表明是顶层父类别				catas.add(c);			}		}		return catas;	}	public List<Catalog> findLeafCatalogs(Long parentId) {		List<Catalog> catas = new ArrayList<Catalog>();		List<Catalog> cs =  findAllCatalogs();		for(int i = 0 ; i < cs.size() ; i ++) {			Catalog c = cs.get(i);			Long tempId = (c.getId() / 100) * 100 ;			//不把根结点包含进来			if(tempId.equals(parentId) && !c.getId().equals(parentId) ) {				catas.add(c);			}		}		return catas;	}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -