`
history1918
  • 浏览: 9931 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring3.0+Struts2.2+Hibernate3.6+ExtJS3.2.0+DWR框架 整合二

阅读更多
一、采用Spring的IOC和DI实现持久

1、定义BaseDao接口基本服务类
package com.hanz.dao.base;

import java.io.Serializable;
import java.util.List;

/**
* <p>
* Title: 基础DAO接口
* </p>
* <p>
* Description:
* </p>
*
* @author 曹彦彬
* @version 1.0.0.20080703
*/
public interface BaseDao {
/**
* 根据类和id找到pojo对象
*
* @param pojoClass
*            Class pojo的类
* @param id
*            String 唯一标识
* @return Object pojo对象
* @throws RuntimeException
*/
public Object loadById(Class pojoClass, Serializable id)
throws RuntimeException;

/**
* 从数据库查询相应列表
*
* @param ql
*            查询语言
* @return Object pojo对象
* @throws RuntimeException
*/
public List<Object> find(String hql) throws RuntimeException;

/**
* 创建新对象
*
* @param pojo
*            Object 新对象
* @throws RuntimeException
*/
public void save(Object pojo) throws RuntimeException;

/**
* 更新已有对象
*
* @param pojo
*            Object 需要更新的对象
* @throws RuntimeException
*/
public void update(Object pojo) throws RuntimeException;

/**
* 插入或更新已有对象
*
* @param pojo
*            Object 需要插入或更新的对象
* @throws RuntimeException
*/
public void insertOrUpdate(Object pojo) throws RuntimeException;

/**
* 删除对象
*
* @param pojo
*            Object 需要删除的对象
* @throws RuntimeException
*/
public void delete(Object pojo) throws RuntimeException;

/**
* 删除对象,根据id
*
* @param pojoClass
*            Class 需要删除的对象类
* @param id
*            String 唯一标识
* @throws RuntimeException
*/
public void delete(Class pojoClass, Serializable id)
throws RuntimeException;

/**
* 删除集合中的全部对象
*
* @param pojoName
*            pojo映射名
* @param ids
*            要删除的ID集合
* @throws RuntimeException
*/
public void deleteAll(Class pojoName, Integer[] ids) throws RuntimeException;

/** */
/**
* 分页查询
*
* @param hql
*            查询的条件
* @param offset
*            开始记录
* @param length
*            一次查询几条记录
* @return
*/
public List queryForPage(final String hql, final int offset,
final int length) throws RuntimeException;

/**
* 分页查询
*
* @param hql
*            查询的条件
* @return
*/
public List queryForPage(final String hql) throws RuntimeException;

/** */
/**
* 查询所有记录数
*
* @param hql
*            查询的条件
* @return 总记录数
*/
public int getAllRowCount(String hql) throws RuntimeException;

}
2、完成实现类(内部注入HibernateTemplate)
package com.hanz.dao.base.imp;

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

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.hanz.dao.base.BaseDao;

/**
* <p>
* Title: 基础DAO实现类
* </p>
* <p>
* Description:
* </p>
*
* @author 曹彦彬
* @version 1.0.0.20080703
*/
@Component("baseDao")
public class BaseDaoImp implements BaseDao {
protected HibernateTemplate hibernateTemplate;

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#delete(java.lang.Object)
*/
public void delete(Object pojo) throws RuntimeException {
hibernateTemplate.delete(pojo);
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#delete(java.lang.Class,
* java.io.Serializable)
*/
@SuppressWarnings("unchecked")
public void delete(Class pojoClass, Serializable id)
throws RuntimeException {
hibernateTemplate.delete(loadById(pojoClass, id));
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#find(java.lang.String)
*/
@SuppressWarnings("unchecked")
public List find(String hql) throws RuntimeException {
return hibernateTemplate.find(hql);
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#save(java.lang.Object)
*/
public void save(Object pojo) throws RuntimeException {
hibernateTemplate.save(pojo);
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#loadById(java.lang.Class,
* java.io.Serializable)
*/
@SuppressWarnings("unchecked")
public Object loadById(Class pojoClass, Serializable id)
throws RuntimeException {
return hibernateTemplate.get(pojoClass, id);
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#update(java.lang.Object)
*/
public void update(Object pojo) throws RuntimeException {
hibernateTemplate.update(pojo);
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#insertOrUpdate(java.lang.Object)
*/
public void insertOrUpdate(Object pojo) throws RuntimeException {
hibernateTemplate.saveOrUpdate(pojo);
}

/*
* (non-Javadoc)
*
* @see com.hanz.dao.base.dao.BaseDao#deleteAll(java.lang.Class,
* java.lang.Long[])
*/
@SuppressWarnings("unchecked")
public void deleteAll(final Class pojoName, final Integer[] ids)
throws RuntimeException {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query deleteQuery = session.createQuery("delete from "
+ pojoName.getName() + " where id in(:ids)");
deleteQuery.setParameterList("ids", ids);
int dels = deleteQuery.executeUpdate();
return dels;
}

});
}

/**
* 分页查询
*
* @param hql
*            查询的条件
* @param offset
*            开始记录
* @param length
*            一次查询几条记录
* @return
*/
public List queryForPage(final String hql, final int offset,
final int length) throws RuntimeException {
List list = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}

/**
* 查询所有记录数
*
* @return 总记录数
*/
public int getAllRowCount(String hql) throws RuntimeException {
return hibernateTemplate.find(hql).size();
}

@Override
public List queryForPage(final String hql) throws RuntimeException {
List list = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
List list = query.list();
return list;
}
});
return list;
}
}

3、编写自定义接口
package com.hanz.dao;

import com.hanz.dao.base.BaseDao;
import com.hanz.domain.User;



public interface UserDAO extends BaseDao{

public User getUser(User user) throws Exception;
}
4、实现自定义接口类
package com.hanz.dao.imp;

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.stereotype.Component;

import com.hanz.dao.UserDAO;
import com.hanz.dao.base.imp.BaseDaoImp;
import com.hanz.domain.User;

@Component("userDao")
public class UserDAOImp extends BaseDaoImp implements UserDAO {

/**
* 自定义实现登录的方法
*/
@Override
public User getUser(final User user) throws Exception {

List list = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from User as user where user.user_code=? and user.pwd=? and user.popedom=? ";
Query query = session.createQuery(hql);
query.setString(0, user.getUser_code());
query.setString(1, user.getPwd());
query.setInteger(2, user.getPopedom().getId());
List list = query.list();
return list;
}
});
if (list.size() > 0)
return (User) list.get(0);
else
return null;
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics