问题描述
- spring3中的hibernateTemplate怎么拿不到数据
- <?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好像一直是空的
解决方案二:
你直接判断下它是不是空的不就行了么?