spring+hibernate怎么连数据库?
从数据库逆向hibernate生成了hbm.xml和javabean
它自动生成的userDAO,其中的一个方法:
public List findByProperty(String propertyName, Object value) {
log.debug("finding User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from User as model where model."
+ propertyName + "= ?";
return
getHibernateTemplate().find(queryString, value);
} catch (
RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
applicationContext配置:
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg>
<ref local="sessionFactory"></ref>
</constructor-arg>
</bean>
<bean id="userDAO" class="com.LostAndFound.dao.userDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.LostAndFound.service.UserService">
<property name="userDao">
<ref local="userDAO"></ref>
</property>
</bean>
<bean name="/login" class="com.LostAndFound.myaction.LoginAction">
<property name="userService">
<ref bean="userService"></ref>
</property>
</bean>
运行后:
java.lang.NullPointerException com.LostAndFound.dao.userDAO.findByProperty(userDAO.java:92)
是不是说findByProperty里 getHibernateTemplate()是空值?是配置错了吗?没取到数据库的数据?
------解决方案--------------------
这里配置可能有问题
<bean id="userDAO" class="com.LostAndFound.dao.userDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
dao里面应该配的是hibernateTemplate吧