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

📄 catalogdaoimpl.java

📁 基于SSH框架的代码例子
💻 JAVA
字号:
package com.barter.catalog.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.barter.catalog.dao.ICatalogDao;
import com.barter.catalog.dto.CatalogDto;
import com.sino.framework.exception.DaoException;
import com.sino.framework.util.Page;

public class CatalogDaoImpl extends HibernateDaoSupport implements ICatalogDao {

	public CatalogDto findCatalogById(String id) throws DaoException {
		StringBuffer hql = new StringBuffer();
		CatalogDto catalogDto = null;

		hql.append("select c.name,p.name,o.name,c.remark");
		hql.append(" from Catalog c,Org o,ProjectType p where c.typeId = p.id and c.orgId = o.id and c.id='");
		hql.append(id);
		hql.append("'");
		List tempList = getHibernateTemplate().find(hql.toString());
		
		if(tempList != null && tempList.size() > 0){
			
			catalogDto = new CatalogDto();
			
			Object [] obj = (Object [])tempList.get(0);
			
			if(obj[0] != null){
				catalogDto.setName(obj[0].toString());
			}
			
			if(obj[1] != null){
				catalogDto.setTypeName(obj[1].toString());
			}
			
			if(obj[2] != null){
				catalogDto.setOrgName(obj[2].toString());
			}
			
			if(obj[3] != null){
				catalogDto.setRemark(obj[3].toString());
			}
		}
		
		return catalogDto;
	}
	
	public Page getCatalogByName(final int pageNo,final int pageSize,final String name) throws DaoException { 
		
		int count = getCatalogByNameCount(name);
		List dataList =	null;
		Page page = null;
		
		if(count > 0){
			HibernateCallback hc = new HibernateCallback() { 
				public Object doInHibernate(Session session) throws HibernateException, SQLException { 
					
					Query query = null;		
					List tempList = null;
					
					int startIndex = 0;
					int totalCount = 0;
					
					totalCount = getCatalogByNameCount(name);
					
					if (totalCount < 1){
						return new Page();
					}
					
					startIndex = Page.getStartOfPage(pageNo, pageSize);
					
					StringBuffer hql = new StringBuffer("from Catalog c where 1=1");
					
					if(name != null && !name.trim().equals("")){
						hql.append(" and c.name like:name order by c.createDate DESC");
						query = session.createQuery(hql.toString());
						tempList = query.setString("name",new StringBuffer().append( "%").append(name).append("%").toString()).setFirstResult(startIndex).setMaxResults(pageSize).list();
					}
					else{
						hql.append(" order by c.createDate DESC");
						query = session.createQuery(hql.toString());
						tempList = query.setFirstResult(startIndex).setMaxResults(pageSize).list();
					}

					return tempList;
				} 
			};
			
			dataList =	getHibernateTemplate().executeFind(hc);
			
			page = new Page(Page.getStartOfPage(pageNo, pageSize), count, pageSize, dataList);
		}
		
		return page;
	}
	
	private int getCatalogByNameCount(final String name) {
		
		HibernateCallback hc = new HibernateCallback() { 
			public Object doInHibernate(Session session) throws HibernateException, SQLException { 
				
				Query query = null;		
				List tempList = null;
				
				StringBuffer totalCountHql = new StringBuffer("select count(*) from Catalog c where 1=1");
				
				if(name != null && !name.trim().equals("")){

						totalCountHql.append(" and c.name like:name");
						query = session.createQuery(totalCountHql.toString());
						query.setString("name",new StringBuffer().append( "%").append(name).append("%").toString());
						tempList = query.list();

				}
				else{
					
					query = session.createQuery(totalCountHql.toString());
					tempList = query.list();
				}
				
				return tempList;
			} 
		};
		
		List dataList =	getHibernateTemplate().executeFind(hc);
		
		if(dataList != null && dataList.size() > 0){
		
			long temp = (Long)dataList.get(0);
			return (int)temp;
		}
	
		return 0;
	}
}

⌨️ 快捷键说明

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