求高手指点HibernateTemplate()对象为空的原因
Dao名称package com.tarena.netctoss.dao.impl;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.tarena.netctoss.dao.CostDAO;
import com.tarena.netctoss.dao.DAOException;
import com.tarena.netctoss.entity.Cost;
@Repository
@Scope("prototype")
public class HibernateCostDAO extends HibernateDaoSupport
implements CostDAO{
@Resource
public void setMySessionFactory(SessionFactory sf){
super.setSessionFactory(sf);
}
public void delete(Integer id) throws DAOException{
Cost cost = findById(id);
getHibernateTemplate().delete(cost);
}
public List<Cost> findAll() throws DAOException {
String hql = "from Cost";
List list = getHibernateTemplate().find(hql);
return list;
}
public List<Cost> findAll(final int page,final int rowsPerPage) throws DAOException {
List list = (List) getHibernateTemplate().execute(
new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException,SQLException{
String hql = "from Cost";
Query query=session.createQuery(hql);
int begin = (page - 1) * rowsPerPage;
query.setFirstResult(begin);
query.setMaxResults(rowsPerPage);
List list = query.list();
return list;
}
});
return list;
}
public Cost findById(Integer id) throws DAOException {
Cost cost =(Cost)getHibernateTemplate().load(Cost.class, id);
System.out.println(getHibernateTemplate());
return cost;
}
public Cost findByName(String name) throws DAOException {
String hql = "from cost where name =?";
Object[] params = {name};
List<Cost> list = getHibernateTemplate().find(hql,params);
if(!list.isEmpty()){
return list.get(0);
}else{
return null;
}
}
public Map<String, String> getCostStatusOptions() {
Map<String,String> statusOptions =
new LinkedHashMap<String,String>();
statusOptions.put("0","开通" );
statusOptions.put("1", "暂停");
return statusOptions;
}
public int getTotalPages(int rowsPerPage) throws DAOException {
String hql = "select count(*) from Cost";
List list = getHibernateTemplate().find(hql);
int totalRows = Integer.parseInt(list.get(0).toString());
if(totalRows%rowsPerPage ==0){
return totalRows/rowsPerPage ;
}else{
return totalRows/rowsPerPage+1;
}
}
public void save(Cost cost) {
getHibernateTemplate().save(cost);
}
public void update(Cost cost) {
getHibernateTemplate().update(cost);
}
}
<bean id="myDataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 注入数据库连接参数 -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe">
</property>
<property name="driverClassName" value="oracle.jdbc.OracleDriver">
</property>
<property name="username" value="system">
</property>
<property name="password" value="system">
</property>
<!-- 设置连接最大数 -->
<property name="maxActive" value="20">
</property>
<!-- 设置连接池实例化时初始创建的连接数 -->
<property name="initialSize" value="1">
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入数据库连接信息 -->
<property name="dataSource" ref="myDataSource">
</property>
<!-- 注入hibernate配置参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 注入映射描述文件 -->
<property name="mappingResources">
<list>
<value>com/tarena/netctoss/entity/Cost.hbm.xml</value>
</list>
</property>
</bean>
<!-- 组件扫描 -->
<context:component-scan
base-package="com.tarena.netctoss"/>
<!-- 事务管理配置 -->
<!-- 定义事务管理Bean -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入session资源 -->
<property name="sessionFactory"
ref="sessionFactory">
</property>
</bean>
<!-- 定义方面和通知 -->
<!-- 可以指定目标对象中不同方法
采用不同的事务里机制 -->
<!--
<tx:advice id="txAdvice"
transaction-manager="txManager">
<tx:attributes>
<tx:method name="execute" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
-->
<!-- 定义切入点,aop切入 -->
<!-- proxy-target-class=true表示采用CGLIB方式生成代理类 -->
<!--
<aop:config proxy-target-class="true">
<aop:pointcut id="actionPointcut"
expression="within(com.tarena.netctoss.action..*)"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="actionPointcut"/>
</aop:config>
-->
<!-- 开启事务注解配置 -->
<tx:annotation-driven proxy-target-class="true"
transaction-manager="txManager"/>
</beans>
补充:Java , Web 开发