- 浏览: 72617 次
文章分类
最新评论
-
benjaminz:
话说,我现在是用8i的client连接10g的数据库所有和数据 ...
oracle 9i的客户端好像是访问不了10g的服务器的 -
icefire:
性能问题!
一般是是在log.debug("&quo ...
日志的写法 -
zhangfengsimon:
YuLimin 写道<p>这是完全错误的理解,是不 ...
log4j xml 配置方式 -
YuLimin:
<p>这是完全错误的理解,是不是在想当然?< ...
log4j xml 配置方式 -
sun_cat:
昨天晚上本来打算上传附件的 可是老是上传不上去
偶是刚开始学s ...
一个DAO 一个Interface 搞定spring + hibernate+动态hql查询
找了一些spring的文章看了下,有所启发,于是大概的总结了下,希望大家多提提意见
为了实现自己程序对spring和hibernate的过分依赖,因此自己写了一个接口,封装了8个常用的数据库操作
接口如下
java 代码
- package com.pig.dao;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import org.springframework.dao.DataAccessException;
- /**
- * 实现具体数据访问的接口,该接口将程序和框架进行了分离,提供了抽象出来的几个常用的方法 优点:减少了对spring,hibernate的依赖
- * 缺点:提供了有限的几个方法,可能造成提供的方法不全
- *
- * @author lizhihui
- * @see DAOInterfaceHibernate3Imp
- */
- public interface DAOInterface {
- /**
- * Persist the given transient instance.
- * @param entity the transient instance to persist
- * @return the generated identifier
- */
- public Serializable save(Object entity);
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate Session)
- * @throws DataAccessException in case of Hibernate errors
- * @see net.sf.hibernate.Session#saveOrUpdate(Object)
- */
- public void saveOrUpdate(Object entity) throws DataAccessException;
- /**
- * Save or update all given persistent instances,
- * according to its id (matching the configured "unsaved-value"?).
- * @param entities the persistent instances to save or update
- * (to be associated with the Hibernate Session)
- * @throws DataAccessException in case of Hibernate errors
- * @see net.sf.hibernate.Session#saveOrUpdate(Object)
- */
- public void saveOrUpdate(Collection entities) throws DataAccessException;
- /**
- * Delete the given persistent instance.
- * @param entity the persistent instance to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see net.sf.hibernate.Session#delete(Object)
- */
- public void delete(Object entity) throws DataAccessException;
- /**
- * Delete the instance according to identifier
- * @param c class of instance
- * @param id identifier
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- */
- public void delete(Class c, Serializable id) throws DataAccessException;
- /**
- * Delete all given persistent instances.
- *
This can be combined with any of the find methods to delete by query
- * in two lines of code, similar to Session's delete by query methods.
- * @param entities the persistent instances to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see net.sf.hibernate.Session#delete(String)
- */
- public void delete(Collection entities) throws DataAccessException;
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- *
This method is a thin wrapper around
- * {@link net.sf.hibernate.Session#load(Class, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id an identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see net.sf.hibernate.Session#load(Class, java.io.Serializable)
- */
- public Object load(Class entityClass, Serializable id) throws DataAccessException;
- /**
- * Execute an HQL query
- * @param queryString a query expressed in Hibernate's query language
- * @return a List containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see net.sf.hibernate.Session#find(String, Object[], net.sf.hibernate.type.Type[])
- * @see net.sf.hibernate.Session#createQuery
- */
- public List find(String queryString) throws DataAccessException;
- }
他的实现
java 代码
- package com.pig.dao;
- import java.io.Serializable;
- import java.sql.SQLException;
- import java.util.Collection;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- 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;
- public class DAOInterfaceHibernate3Imp extends HibernateDaoSupport implements DAOInterface {
- private static Log log=LogFactory.getLog(DAOInterfaceHibernate3Imp.class);
- public void delete(Object entity) throws DataAccessException {
- getHibernateTemplate().delete(entity);
- }
- public void delete(Class c, Serializable id) throws DataAccessException {
- Object entiry = load(c, id);
- getHibernateTemplate().delete(entiry);
- }
- public void delete(Collection entities) throws DataAccessException {
- getHibernateTemplate().deleteAll(entities);
- }
- public List find(String queryString) throws DataAccessException {
- return getHibernateTemplate().find(queryString);
- }
- public Object load(Class entityClass, Serializable id) throws DataAccessException {
- return getHibernateTemplate().load(entityClass, id);
- }
- public Serializable save(Object entity) {
- return getHibernateTemplate().save(entity);
- }
- public void saveOrUpdate(Object entity) throws DataAccessException {
- getHibernateTemplate().saveOrUpdate(entity);
- }
- public void saveOrUpdate(Collection entities) throws DataAccessException {
- getHibernateTemplate().saveOrUpdateAll(entities);
- }
- }
这样如果 hibernate如果有了变得 只改变他的实现类就可以了,甚至可以改变成其他的中间件实现
在程序中只用了实现了一个DAO,感觉他可以对所以的类操作了,至于其他的和业务相关联的操作 例如用户登陆,修改秘密等操作 我把他们归结到了 service中,这样service的实现可能要好多的类和接口
大家看下我的DAO
java 代码
- package com.pig.dao;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import org.springframework.dao.DataAccessException;
- import com.pig.util.PropertyResource;
- public class BasicDAO {
- private DAOInterface dao;
- private PropertyResource sqlMapping;
- public BasicDAO(){
- }
- public void delete(Object entity) throws DataAccessException {
- dao.delete(entity);
- }
- public void delete(Class c, Serializable id) throws DataAccessException {
- dao.delete(c,id);
- }
- public void delete(Collection entities) throws DataAccessException {
- dao.delete(entities);
- }
- public List find(String queryStringKey,Object[] args) throws DataAccessException {
- return dao.find(sqlMapping.getFormattedString(queryStringKey, args));
- }
- public Object load(Class entityClass, Serializable id) throws DataAccessException {
- return dao.load(entityClass, id);
- }
- public Serializable save(Object entity) {
- return dao.save(entity);
- }
- public void saveOrUpdate(Object entity) throws DataAccessException {
- dao.saveOrUpdate(entity);
- }
- public void saveOrUpdate(Collection entities) throws DataAccessException {
- dao.saveOrUpdate(entities);
- }
- }
在这个DAO中注入了一个DAOInterface 和 一个properties 文件 这个文件可以根据key来找到对应的hql 或者sql语句 并将参数格式化进去,看下这个方法
java 代码
- public List find(String queryStringKey,Object[] args) throws DataAccessException {
- return dao.find(sqlMapping.getFormattedString(queryStringKey, args));
- }
这个方法直接根据给定的hql语句 查询出结果集。本来还想给出一个根据hql来更新数据库的操作,但是发现hibernate好像没有提供这个接口(我对hibernate的了解还不是很多)
下面将读取hql文件的的类也一并给出 大家参考下把,这个hql文件应该放到classpath下
java 代码
- package com.pig.util;
- import java.text.MessageFormat;
- import java.util.*;
- /**
- * read properties file in classpath
- * @author lzh
- * @version 1.0
- */
- public class PropertyResource {
- private String resourceName;
- private Locale locale;
- private Map<String, String> properties;
- public PropertyResource() {
- }
- private void init() {
- if (properties == null) {
- properties = new Hashtable<String, String>();
- ResourceBundle rb = null;
- if (locale == null) {
- rb = ResourceBundle.getBundle(resourceName);
- } else {
- rb = ResourceBundle.getBundle(resourceName, locale);
- }
- Enumeration<String> e = rb.getKeys();
- String key = null;
- String value = null;
- while (e.hasMoreElements()) {
- key = e.nextElement();
- value = rb.getString(key);
- properties.put(key, value);
- }
- }
- }
- public String getString(String key) {
- if (properties == null) {
- init();
- }
- return properties.get(key);
- }
- public String getString(String key, String defaultValue) {
- if (properties == null) {
- init();
- }
- String value = properties.get(key);
- return value == null ? defaultValue : value;
- }
- public String getFormattedString(String key, Object args[]) {
- String msg = MessageFormat.format(getString(key), args);
- return msg;
- }
- public Locale getLocale() {
- return locale;
- }
- public void setLocale(Locale locale) {
- this.locale = locale;
- }
- public String getResourceName() {
- return resourceName;
- }
- public void setResourceName(String resourceName) {
- this.resourceName = resourceName;
- }
- }
最后大家看下我spring的配置文件把,这个配置文件还配置了对事务的管理(参考了他人的做法)
java 代码
- <!-- Hibernate SessionFactory -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.generate_statistics">true</prop>
- </props>
- </property>
- <property name="eventListeners">
- <map>
- <entry key="merge">
- <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
- </entry>
- </map>
- </property>
- </bean>
- <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <!--define bean of transaction interceptor -->
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager" />
- <property name="transactionAttributes">
- <props>
- <prop key="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- define BeanNameAutoProxyCreator-->
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <!-- 所有名字以DAO,Service结尾的bean,将由该"bean后处理器"为其创建事务代理;实际上应该在业务层进行事务管理,这里只是举一个简单例子 -->
- <value>*DAO,*Service</value>
- </property>
- <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
- <property name="interceptorNames">
- <list>
- <!-- 可以增加其他的拦截器 -->
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
- <bean id="daoInterface" class="com.pig.dao.DAOInterfaceHibernate3Imp">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="sqlPropertyResource" class="com.pig.util.PropertyResource">
- <property name="resourceName" value="com.pig.resource"/>
- </bean>
- <bean id="basicDAO" class="com.pig.dao.BasicDAO">
- <property name="dao" ref="daoInterface"/>
- <property name="sqlMapping" ref="sqlPropertyResource"/>
- </bean>
希望大家能多提意见,偶对spring才看了没几天,看了大家的贴子,可以说是照葫芦画瓢 见笑见笑
评论
6 楼
sun_cat
2007-03-15
昨天晚上本来打算上传附件的 可是老是上传不上去
偶是刚开始学spring 希望大家多多指教
偶是刚开始学spring 希望大家多多指教
5 楼
eyejava
2007-03-15
BasicDAO 包装DAOInterface 还真有点怪怪的,只是多了一个find方法而已.
另外hql 放在配置文件里面运行期间的参数接收将很麻烦.
appgen 已经提供生成dao代码的模版, 这些重复的代码都可以不用手工写的.
另外hql 放在配置文件里面运行期间的参数接收将很麻烦.
appgen 已经提供生成dao代码的模版, 这些重复的代码都可以不用手工写的.
4 楼
jfy3d
2007-03-15
我也这么用 不过是Ibatis的
因为SQL在SQLMAP 里 功能应该比ResourceBundle更多些
连service也简化了
普通GRUD的service
只要
public UserService extends BaseService {
}
就可以了
单DAO 确实提高很大生产速度
因为SQL在SQLMAP 里 功能应该比ResourceBundle更多些
连service也简化了
普通GRUD的service
只要
public UserService extends BaseService {
}
就可以了
单DAO 确实提高很大生产速度
3 楼
daoger
2007-03-15
这是常用的配置方式中的一种,希望LZ进一步研究,能有更好的文章分享!
2 楼
huangpengxiao
2007-03-15
严重同意楼上
1 楼
lighter
2007-03-14
1、建议楼主打包一下程序,方便看一下,这样拉下来,看得头昏
2、“搞定spring + hibernate+动态hql查询”有标题党的嫌疑,有时间看清楚后再回复你的帖子
2、“搞定spring + hibernate+动态hql查询”有标题党的嫌疑,有时间看清楚后再回复你的帖子
相关推荐
用三大框架整合实现分页,附加可用,逻辑简单适合初学者
高效率spring+struts+hibernate分页算法
使用到了Hibernate中的: HQL查询 SQL查询 QBC查询 配置文件中配置SQL/HQL语句查询 使用到了Spring中的: Ioc实现动态代理 和 Aop实现自动事务处理!
由于Generic DAO不能对复杂的HQL或视图,存贮过程进行操作,目前还有待完善,可以不用hibernate-generic-dao - Generic DAO, 用Spring3.2.0和Hibernate4替换,就是一个非常好的Struts2+Spring3+Hibernate4基于...
hibernate查询之HQLhibernate查询之HQLhibernate查询之HQL
加详细注释Jsp+hibernate实现留言板
使用说明: ...1、修复HQL方式日期范围查询BUG 2、添加分组分页的功能 3、添加pageSize设置功能 V1.1版本更新内容 1、实现日期范围查询 V1.0版本 1、初始版本。实现JSP+SERVLET+Hibernate基本分页
Hibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询....
Hibernate hql查询语法总结 Hibernate hql查询语法总结
这个实例是我看到得flex+hibernate 最经典的实例
基于Struts+Hibernate+Spring三大框架下编写的简单的分页代码,其中列举了SQL与HQL两种不同的分页。
最近新来公司,做的一个间的增删改查案例,相信对于想学习SSH,struts2+hibernate+spring三大框架java来说是个很好的案例哦。
HQL是hibernate自己的一套查询
Hibernate中的关联查询实际上生成的是数据库表连接查询的SQL语句
之前也发了一个,这个包含之前的,还添加了hibernate对数据库操作的代码(注意:controller层两个QBC/SQLTest代码不能运行,只是写出了操作数据库的除了HQL的另外2种方式)。我发现现在还是有很多公司用hibernate+...
hibernate+spring +ant+junit 1.整合hibernate+spring 2.ant自动生成xml文件和api 3.junit测试hql语句 4.c标签写的前台分页程序
Hibernate查询语言HQL.PPT
Hibernate HQL基础练习小列子+数据库
Hibernate+ Spring +hql +mysql +ajax 内容简单易懂!可以作为教程学习 也可以通过修改 做成一个小项目!