rcontentimpl.java

来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 227 行

JAVA
227
字号
/* 
 * Created on 2007-1-22
 * Last modified on 2007-12-20
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.dao.hibernate.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.dao.RContentDAO;
import com.yeqiangwei.club.dao.hibernate.support.HibernateFacade;
import com.yeqiangwei.club.dao.hibernate.support.HibernateProvider;
import com.yeqiangwei.club.model.RContent;
import com.yeqiangwei.club.param.TopicParameter;

public class RContentImpl implements RContentDAO {
	
	private static final Logger logger = Logger.getLogger(RContentImpl.class.getName());
	
	private static final String FIND_REPLYID = "from RContent where replyId=?";
	
	private static final String FIND_RECONTENTID = "from RContent where rcontentId=?";
	
	private static final String DELETE_RECONTENTID = "delete from RContent where rcontentId=?";
	
	private static final String DELETES_RECONTENTID = "delete from RContent where rcontentId in (:ids)";

	private static final String DELETE_TOPICID = "delete from RContent where topicId=?";
	
	private static final String DELETE_REPLYID = "delete from RContent where replyId=?";
	
	private static final String DELETES_TOPICID = "delete from RContent where topicId in (:ids)";

	private static final String DELETES_REPLYID = "delete from RContent where replyId in (:ids)";
	
	
	public int deleteByReplyId(int replyId){
        HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
        facade.createQuery(DELETE_REPLYID);
        facade.setInt(0, replyId);
        return facade.executeUpdate();
	}
	
	public void create(RContent item) {
		HibernateProvider<RContent> facade = new HibernateFacade<RContent>();
		facade.save(item);
	}

	public void update(RContent item) {
		HibernateProvider<RContent> facade = new HibernateFacade<RContent>();
		facade.update(item);
	}

	public int delete(RContent item) {
    	HibernateProvider<RContent> facade = new HibernateFacade<RContent>();
		facade.createQuery(DELETE_RECONTENTID);
		facade.setInt(0, item.getRcontentId());
		return facade.executeUpdate();
	}

	public int delete(List<Integer> ids) {
    	HibernateProvider<RContent> facade = new HibernateFacade<RContent>();
		facade.createQuery(DELETES_RECONTENTID);
		facade.setParameterList("ids",ids);
		return facade.executeUpdate();
	}
	

	public int deleteByReplyId(List<Integer> ids) {
        HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
        facade.createQuery(DELETES_REPLYID);
        facade.setParameterList("ids",ids);
        return facade.executeUpdate();
	}
	
	public int deleteByTopicId(int topicId) {
        HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
        facade.createQuery(DELETE_TOPICID);
        facade.setInt(0, topicId);
        return facade.executeUpdate();
	}

	public int deleteByTopicId(List<Integer> ids) {
        HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
        facade.createQuery(DELETES_TOPICID);
        facade.setParameterList("ids",ids);
        return facade.executeUpdate();
	}

	public RContent findById(int id) {
		HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
        facade.createQuery(FIND_RECONTENTID);
        facade.setInt(0,id);
        facade.setMaxResults(1);
		return facade.uniqueResult();
	}
	
	public RContent findByReplyId(int replyId) {
		HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
        facade.createQuery(FIND_REPLYID);
        facade.setInt(0,replyId);
        facade.setMaxResults(1);
		return facade.uniqueResult();
	}
	
	private String getWhere(TopicParameter param){
		StringBuffer hql = new StringBuffer();
		if(param.getTopicId()!=null){
			hql.append(" and topicId=");
			hql.append(param.getTopicId());
		}
		if(param.getIsDeleted()!=null){
			hql.append(" and isDeleted=?");
		}
		return hql.toString();
	}
	
	private String getOrderBy(TopicParameter param){
		StringBuffer hql = new StringBuffer();
		hql.append(" order by replyId");
		return hql.toString();
	}
	
	public List<RContent> findByParameter(TopicParameter param) {
		List<RContent> list = null;
		HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
		Connection con = facade.getSession().connection();
		PreparedStatement pps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		if(param.getPage()>1){
			/*
			 * sql 2005
			 */
			sql.append("SELECT TOP ");
			sql.append(param.getRows());
			sql.append(" * FROM (select *, ROW_NUMBER() OVER (");
			sql.append(getOrderBy(param));
			sql.append(") AS RowNo FROM RContent where rcontentId>0 ");
			sql.append(this.getWhere(param));
			sql.append(") AS A where RowNo>");
			sql.append(param.getRows()*(param.getPage()-1));
		}else{
			sql.append("SELECT TOP ");
			sql.append(param.getRows());
			sql.append(" * FROM RContent WHERE rcontentId>0 ");
			sql.append(this.getWhere(param));
			sql.append(getOrderBy(param));
		}
		logger.debug(sql);
		try {
			list = new ArrayList<RContent>();
			pps = con.prepareStatement(sql.toString());
			rs = pps.executeQuery();
			while(rs.next()){
				RContent item = new RContent();
				item.setContent(rs.getString("content"));
				item.setCopyright(rs.getByte("copyright"));
				item.setIsDeleted(rs.getBoolean("isDeleted"));
				item.setListSignatures(rs.getByte("listSignatures"));
				item.setRcontentId(rs.getInt("rcontentId"));
				item.setReplyId(rs.getInt("replyId"));
				item.setTopicId(rs.getInt("topicId"));
				list.add(item);
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}
		return list;
	}
	
	/*
	public List<RContent> findByParameter(TopicParameter param) {
		if(param.getTopicId()==null||param.getTopicId().intValue()==0){
			logger.error("List<RContent> findByParameter(TopicParameter param) topicId=0");
			return null;
		}
		StringBuffer hql = new StringBuffer();
		hql.append("from RContent where rcontentId>0 ");
		if(param.getTopicId()!=null){
			hql.append(" and topicId=");
			hql.append(param.getTopicId());
		}
		if(param.getIsDeleted()!=null){
			hql.append(" and isDeleted=?");
		}
		hql.append(" order by replyId");
		HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
		facade.createQuery(hql);
		if(param.getIsDeleted()!=null){
			facade.setBoolean(0, param.getIsDeleted().booleanValue());
		}
		facade.setFirstResult(param.getPagination().getStartRow());
		facade.setMaxResults(param.getPagination().getEndRow());
		return facade.executeQuery();
	}
	*/

	public long countByParameter(TopicParameter param) {
		StringBuffer hql = new StringBuffer();
		hql.append("select count(rcontentId) from RContent where rcontentId>0 ");
		hql.append(this.getWhere(param));
		HibernateFacade<RContent> facade = new HibernateFacade<RContent>();
		facade.createQuery(hql);
		if(param.getIsDeleted()!=null){
			facade.setBoolean(0, param.getIsDeleted().booleanValue());
		}
		return facade.resultTotal();
	}

	public List<RContent> findAll(TopicParameter param) {
		return this.findByParameter(param);
	}

	public long countAll(TopicParameter param) {
		return this.countByParameter(param);
	}

}

⌨️ 快捷键说明

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