📄 cellphonedaohibernate.java
字号:
package com.longtime.wap.module.cellphone.dao.hibernate;
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 com.longtime.wap.common.BaseDao;
import com.longtime.wap.common.web.Page;
import com.longtime.wap.model.Cellphone;
import com.longtime.wap.module.cellphone.common.CellphoneQueryBean;
import com.longtime.wap.module.cellphone.dao.CellphoneDao;
/**
* CellphoneDao层代码实现类
*
* @author shiz
* @date Nov 15, 2007
*/
public class CellphoneDaoHibernate extends BaseDao implements CellphoneDao {
/**
* 统计手机信息数
*
* @param queryBean
* 搜索参数
* @return 手机信息数
*/
public int retrieveCellphonesCount(final CellphoneQueryBean queryBean) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from "
+ "Cellphone cellphone where ");
if (null != queryBean.getSearchType()
&& queryBean.getSearchType().length() > 0) {
hql.append("cellphone.model like :searchType and ");
}
if (null != queryBean.getSearchCompany()
&& queryBean.getSearchCompany().length() > 0) {
hql.append("cellphone.brand like"
+ " :searchCompany and ");
}
if (hql.toString().endsWith(" where ")) {
hql.delete(hql.length() - 6, hql.length());
}
if (hql.toString().endsWith(" and ")) {
hql.delete(hql.length() - 4, hql.length());
}
Query query = session.createQuery(hql.toString());
query.setProperties(queryBean);
return query.uniqueResult();
}
});
}
/**
* 列表分页显示所有符合搜索条件的手机信息
*
* @param queryBean
* 搜索参数
* @param page
* 分页参数
* @return 手机信息列表
*/
public List retrieveCellphonesByCondition(
final CellphoneQueryBean queryBean, final Page page) {
return this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"from Cellphone cellphone where ");
if (null != queryBean.getSearchType()
&& queryBean.getSearchType().length() > 0) {
hql.append("cellphone.model like :searchType and ");
}
if (null != queryBean.getSearchCompany()
&& queryBean.getSearchCompany().length() > 0) {
hql.append("cellphone.brand like :searchCompany and ");
}
if (hql.toString().endsWith(" where ")) {
hql.delete(hql.length() - 6, hql.length());
}
if (hql.toString().endsWith(" and ")) {
hql.delete(hql.length() - 4, hql.length());
}
hql.append(" order by cellphone_id asc");
Query query = session.createQuery(hql.toString());
query.setProperties(queryBean);
return query.setFirstResult(page.getFirstResult())
.setMaxResults(page.getPageSize()).list();
}
});
}
/**
* 获取手机信息列表
*
* @param ids
* 手机信息编号
* @return 手机信息列表
*/
public List retrieveCellphonesByIds(final String[] ids) {
return (List) this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"from Cellphone where ");
for (int i = 0; i < ids.length; i++) {
hql.append("cellphoneId=" + ids[i]);
if (i < ids.length - 1) {
hql.append(" or ");
}
}
Query query = session.createQuery(hql.toString());
return query.list();
}
});
}
/**
* 获取手机信息
*
* @param id
* 手机信息编号
* @return 手机信息
*/
public Cellphone retrieveCellphoneById(Long id) {
return (Cellphone) getHibernateTemplate().get(Cellphone.class, id);
}
/**
* 更新手机信息
*
* @param cellphone
* 手机对象
*/
public void updateCellphone(Cellphone cellphone) {
this.getHibernateTemplate().update(cellphone);
}
/**
* 新增手机信息
*
* @param cellphone
* 手机对象
*/
public void createCellphone(Cellphone cellphone) {
this.getHibernateTemplate().save(cellphone);
}
/**
* 删除手机信息
*
* @param cellphones
* 手机对象列表
*/
public void deleteCellphoneByIds(List cellphones) {
this.getHibernateTemplate().deleteAll(cellphones);
}
/**
* 通过UA参数获得手机列表
*
* @param ua
* 手机UA
* @return 手机信息列表
*/
public List retrieveCellphoneByUA(String ua) {
return (List) this.getHibernateTemplate().find(
"from Cellphone where ua='" + ua + "'");
}
/**
* 通过手机类型参数获得手机列表
*
* @param model
* 手机类型
* @return 手机信息列表
*/
public List retrieveCellphoneByModel(String model) {
return (List) this.getHibernateTemplate().find(
"from Cellphone where model='" + model + "'");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -