<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<!-- 加載常量 -->
<context:annotation-config />
<context:component-scan base-package="com.demo.oa" />
<!-- 配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hibernate_oa"/>
<property name="username" value="root"/>
<property name="password" value="taojun"/>
<property name="initialSize" value="1"></property>
<!--初始化連接池 -->
<property name="maxActive" value="50"></property>
<!-- 最大連接數 -->
<property name="maxIdle" value="20"></property>
<!-- 最大空閒連接數 -->
<property name="minIdle" value="0"></property>
<!-- 最小空閒連接數 -->
<property name="maxWait" value="2000"></property>
<!-- 連接池耗盡,等待時間2s -->
<property name="testOnBorrow" value="true"></property>
<!-- 獲取連接時,檢測是否是有較連接 -->
<property name="testOnReturn" value="true"></property>
<!-- 歸還連接時,檢測是否是有較連接 -->
<property name="validationQuery" value="select 1"></property>
<!-- 配合獲取與歸還使用 -->
<property name="logAbandoned" value="true"></property>
<!-- 是否在自動回收超時連接的時候打印連接的超時錯誤 -->
<property name="removeAbandoned" value="true" />
<!--removeAbandoned: 是否自動回收超時連接 -->
<property name="removeAbandonedTimeout" value="60" />
<!--removeAbandonedTimeout: 超時時間(以秒數為單位) -->
<property name="minEvictableIdleTimeMillis" value="5000"></property>
<!--空閒連接最大有效時間 -->
<property name="timeBetweenEvictionRunsMillis" value="5000"></property><!--
每隔5S踢除超時空閒連接 -->
</bean>
<!--定義了Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 映射文件采用annotation形式,不用mapping -->
<property name="packagesToScan" value="com.demo.oa.model"></property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
hibernate.jdbc.batch_size=20
</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- more bean definitions go here -->
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.itheima.oa.model.Department;
public class DBUtil {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource(name = "hibernateTemplate")
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void findDep(){
List li=hibernateTemplate.find("from Department");
System.out.println(li);
}
public void save(Department department) {
// TODO Auto-generated method stub
HibernateTemplate h=(HibernateTemplate) ToolsUtil.context.getBean("hibernateTemplate");
System.out.println(h);
System.out.println("實現層調用hibernateTemplate成功!");
// this.getHibernateTemplate().save(department);
System.out.println(department.getName()+"----"+department.getDescription());
new DBUtil().findDep();
}
確定該方法執行了,控制台結果如下:
調用departmentDaoImpl成功!
org.springframework.orm.hibernate3.HibernateTemplate@2331ebb3
實現層調用hibernateTemplate成功!
通過ApplicationContext中的getBean方法拿到了hibernateTtemplate,怎麼就是不能執行save,find等方法呢,hibernateTemplate好像一直是空的