zoukankan      html  css  js  c++  java
  • 配置baseDao(hibernateTemplate)

    当用 (struts2+hibernate+spring)开发的时候,想写个baseDao,同时继承该类的Dao类能使用hibernateTemplate,

    1)在baseDao中注入hibernateTemplate属性。

    手动注入hibernateTemplate属性。

    baseDao.java代码如下:

     1 import org.hibernate.SessionFactory;
    2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    3 import org.springframework.stereotype.Repository;
    4
    5 // class created by wwei 2011-11-28 下午10:12:01
    6
    7 @Repository("baseHibernateDao")
    8 public abstract class BaseHibernateDaoImpl<T> implements BaseHibernateDao<T>{
    9
    10 private HibernateTemplate hibernateTemplate;
    11
    12 public HibernateTemplate getHibernateTemplate() {
    13 return hibernateTemplate;
    14 }
    15 @Resource(name = "hibernateTemplate")
    16 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    17 this.hibernateTemplate = hibernateTemplate;
    18 }
    19
    20 @SuppressWarnings("unchecked")
    21 public List<T> findUser(String hql) throws Exception{
    22 try{
    23 return (List<T>)this.getHibernateTemplate().find(hql);
    24 }catch(Exception e){
    25 logger.info(e);
    26 throw new Exception();
    27 }
    28 }
    29 }

    2) 继承HibernateDaoSupport类。

    同时,还需要注入sessionFactory或者hibernateTemplate。

    此处自定义增加了setSessionFactoryOverride方法。增加了findUser方法,实现自BaseHibernateDao接口。

     1 import java.io.Serializable;
    2 import java.util.List;
    3
    4 import javax.annotation.Resource;
    5
    6 import org.hibernate.SessionFactory;
    7 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    8 import org.springframework.stereotype.Repository;
    9
    10 // class created by wwei 2011-11-28 下午10:12:01
    11
    12 @Repository("baseHibernateDao")
    13 public abstract class BaseHibernateDaoImpl<T> extends HibernateDaoSupport implements BaseHibernateDao<T>{
    14
    15 @Resource(name = "sessionFactory")
    16 public void setSessionFactoryOverride(SessionFactory sessionFactory) {
    17 super.setSessionFactory(sessionFactory);
    18 }
    19
    20 @SuppressWarnings("unchecked")
    21 public List<T> findUser(String hql) throws Exception{
    22 try{
    23 return (List<T>)this.getHibernateTemplate().find(hql);
    24 }catch(Exception e){
    25 logger.info(e);
    26 throw new Exception();
    27 }
    28 }
    29
    30
    31 }

     spring.xml代码如下:

    显示代码:
     1 <?xml version="1.0" encoding="utf-8"?>
    2 <beans xmlns="http://www.springframework.org/schema/beans"
    3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    5 xsi:schemaLocation="
    6 http://www.springframework.org/schema/beans
    7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    8 http://www.springframework.org/schema/tx
    9 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    10 http://www.springframework.org/schema/aop
    11 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    12 http://www.springframework.org/schema/context
    13 http://www.springframework.org/schema/context/spring-context-2.5.xsd
    14 ">
    15 <context:annotation-config /><!-- 使用annotation @resource(name="")-->
    16 <context:component-scan base-package="com.wykd"/><!-- 自动去检测类的注解 -->
    17
    18 <bean
    19 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    20 <property name="locations">
    21 <value>classpath:jdbc.properties</value><!-- properties文件 -->
    22 </property>
    23 </bean>
    24
    25 <bean id="dataSource" destroy-method="close"
    26 class="org.apache.commons.dbcp.BasicDataSource">
    27 <property name="driverClassName" value="${jdbc.driverClassName}" />
    28 <property name="url" value="${jdbc.url}" />
    29 <property name="username" value="${jdbc.username}" />
    30 <property name="password" value="${jdbc.password}" />
    31 </bean>
    32
    33 <bean id="sessionFactory"
    34 class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    35 <property name="dataSource" ref="dataSource"></property>
    36
    37 <property name="packagesToScan">
    38 <list>
    39 <value>com.wykd.bean</value><!-- 自动检测bean类 -->
    40 </list>
    41 </property>
    42
    43 <property name="hibernateProperties"> <!-- 其他hibernate.cfg.xml属性值设置 -->
    44 <props>
    45 <prop key="hibernate.dialect">
    46 org.hibernate.dialect.MySQLDialect
    47 </prop>
    48 <prop key="hibernate.show_sql">true</prop>
    49 <prop key="hibernate.format_sql">true</prop>
    50 </props>
    51 </property>
    52
    53 </bean>
    54
    55 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
    56 <property name="sessionFactory" ref="sessionFactory"></property>
    57 </bean>
    58 </beans>
  • 相关阅读:
    Lambda表达式、依赖倒置
    ASP.NET vNext 概述
    Uname
    RHEL4 i386下安装rdesktop【原创】
    Taxonomy of class loader problems encountered when using Jakarta Commons Logging(转)
    How to decompile class file in Java and Eclipse
    先有的资源,能看的速度看,不能看的,抽时间看。说不定那天就真的打不开了(转)
    Google App Engine 学习和实践
    【VBA研究】VBA通过HTTP协议实现邮件轨迹跟踪查询
    js正則表達式语法
  • 原文地址:https://www.cnblogs.com/csuwangwei/p/2283662.html
Copyright © 2011-2022 走看看