一、采用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;
}
}
分享到:
相关推荐
项目配置:Hibernate3.3.2+Spring3.0+Struts2.2.3 + ext3.4.0,真实项目实现,项目中可参考经典权限设计,通用数据录入,批量数据处理(poi),统一的JSON格式封装,灵活的组合模式设计,EXTJS面向组件编程参考......
Spring3.0+Hibernate4.0+SpringMVC整合ExtJS4是一个集成的应用系统,希望对学习javaweb和extjs的朋友有帮助
ExtJSweb 开发指南中的 简单图书管理系统 ExtJS3.0+spring+hibernate+struts
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。这个是分卷,同时下载2.3.4部分
Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0_Annotations方式快速开发框架资料。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
extjs+spring+struts+hibernate整合实例
Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS...
extjs3.0+struts2 实现登陆 验证码 tree树的加载 首页面。 主要代码还是借鉴别人,然后我再原有基础上再加工。 还没添加数据库类。对于初学者来说还是不错的入门源码。
Struts2(json-plugin) + Spring2 + ExtJS2.2 开源网络硬盘系统
Struts2+Spring+Hibernate+ExtJS开发CRUD功能
Hibernate+Spring+Struts2+ExtJS开发CRUD功能 ,里面包含源码及安装说明文档。可以作为项目开发的基础框架。
Hibernate+Spring+Struts2+ExtJS整合开发实例 非常经典!学习的好帮助!
strut2+hibernate+spring+extjs3.0+excel 动态加载grid 数据导出excel,导入excel datasource自己修改你自己的数据源,数据库中的表可见实体类创建,或者自己修改实体类
采用了struts2.0,spring2.2,ibatis,quartz,extjs3.0 实现了关于短信的调度框架,对接收人维护,动态添加每日短信数据,编写短信模版,最后通过quartz定时发送,因为短信接口收费所以不提供,需要jar包自己下载,...
利用dwr,整合extjs,spring,hibernate.是你学习dwr的参考指南
Extjs3.1+struts2+spring2.5+hibernate3.5环境