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

📄 filesinfodao.java

📁 java web模拟的简单的一个网络硬盘
💻 JAVA
字号:
package com.dark.nethd.dao.hibernate;

import java.util.List;

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

import com.dark.nethd.bean.FilesInfo;
import com.dark.nethd.dao.IFilesInfoDao;

public class FilesInfoDAO extends HibernateDaoSupport implements IFilesInfoDao {
	private static final String DELETE_FILE_BY_ID = "delete from FilesInfo where id = ? and userId = ?";

	public boolean deleteFilesInfoByUserIdAndId(final Integer fid,
			final Integer userId) throws DataAccessException {
		try {
			this.getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException {
					Query query = session.createQuery(DELETE_FILE_BY_ID);
					query.setInteger(0, fid.intValue());
					query.setInteger(1, userId.intValue());
					query.executeUpdate();
					return true;
				}
			});
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public FilesInfo findFilesInfoByID(Integer id) {
		return (FilesInfo) this.getHibernateTemplate().get(FilesInfo.class, id);
	}

	private static final String FIND_FILES_BY_USERID = "from FilesInfo where userId = ?";

	public List findFilesInfoByUserID(Integer userId)
			throws DataAccessException {
		List result = this.getHibernateTemplate().find(FIND_FILES_BY_USERID,
				userId);
		return result;
	}

	public FilesInfo saveFilesInfo(FilesInfo fi) throws DataAccessException {
		try {
			this.getHibernateTemplate().saveOrUpdate(fi);
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
		return fi;
	}

	private static final String GET_ALL_FILES_SIZE_BY_USERID = "select sum(fileSize) from FilesInfo where userId = ?";

	public Integer getAllFilesSizeByUserId(final Integer userId)
			throws DataAccessException {
		List ls = this.getHibernateTemplate().find(
				GET_ALL_FILES_SIZE_BY_USERID, userId);
		if (ls.get(0) == null) {
			return 0;
		} else {
			return ((Integer) ls.get(0)).intValue();
		}
	}
}

⌨️ 快捷键说明

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