我在做SSH项目时,hibernate,struts都交得spring管理,可是怎么在实现类中继承HibernateDaoSupport,但是无法获得session,spring配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433">
</property>
<property name="username" value="sa"> </property>
<property name="password" value="123456789zzh"> </property>
</bean>
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="hibernateProperties">
<props>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true </prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true </prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zzh/mybbs/vo/User.hbm.xml </value> </list> </property> </bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userdao" class="zzh.mybbs.dao.UserDao" abstract="true">
</bean>
<bean id="userdaoimpl" class="zzh.mybbs.dao.impl.UserDaoImpl" parent="userdao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean name="/jsp/user" class="zzh.mybbs.struts.action.UserAction">
<property name="userdao">
<ref bean="userdaoimpl"/>
</property>
</bean>
</beans>
实现类:package zzh.mybbs.dao.impl;
import org.hibernate.Query;
import org.jgroups.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import zzh.mybbs.dao.UserDao;
import zzh.mybbs.vo.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void insert(User user) throws Exception {
super.getSession().save(user);
}
public User queryByUserid(String userid) throws Exception {
User user=null;
String hql="FROM User as u WHERE u.userid=?";
Query q=super.getSession().createQuery(hql);
q.setString(0, userid);
List all=(List) q.list();
if(all.size()>0)
{
user=(User)((java.util.List) all).get(0);
}
return user;
}
}
用这个类测试:package zzh.mybbs;
import java.util.Iterator;
import java.util.List;
import zzh.mybbs.dao.impl.UserDaoImpl;
import zzh.mybbs.vo.User;
public class test {
public static void main(String args[])
{ List all=null;
UserDaoImpl user=new UserDaoImpl();
try
{
all=(List)user.queryByUserid("11");
Iterator iter=all.iterator();
while(iter.hasNext())
{
User u=(User)iter.next();
System.out.print(u.getUserpwd());
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
出错:java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:140)
at zzh.mybbs.dao.impl.UserDaoImpl.queryByUserid(UserDaoImpl.java:20)
at zzh.mybbs.test.main(test.java:15)
HibernateDaoSupport
最新推荐文章于 2017-02-25 21:31:54 发布