当前位置: 代码迷 >> J2EE >> 明明注入了SessionFactory 为什么还是报这样的异常呀?'sessionFactory' or 'hibernateTemplate' is required
  详细解决方案

明明注入了SessionFactory 为什么还是报这样的异常呀?'sessionFactory' or 'hibernateTemplate' is required

热度:577   发布时间:2016-04-22 02:18:46.0
明明注入了SessionFactory 为什么还是报这样的错误呀?'sessionFactory' or 'hibernateTemplate' is required
applicationContext.xml

Java code
<?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.mysql.jdbc.Driver">        </property>        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>        <property name="username" value="root"></property>        <property name="password" value="root"></property>    </bean>    <bean id="SessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource">            <ref bean="DataSource"></ref>        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">                    org.hibernate.dialect.MySQLDialect                </prop>            </props>        </property>        <property name="mappingResources">            <list>                <value>mybean/Message.hbm.xml</value>                <value>mybean/User.hbm.xml</value></list>        </property>    </bean>        <!-- 配置用户DAO组件 -->    <bean id="UserDaoImpl" class="daoimp.UserDaoImpl">        <property name="sessionfactory">            <ref bean="SessionFactory"/>        </property>    </bean>    <!-- 配置消息DAO组件 -->    <bean id="MessageDaoImpl" class="daoimp.MessageDaoImpl">        <property name="sessionfactory">            <ref bean="SessionFactory"/>        </property>        <property name="pageSize" value="2"/>    </bean>    <!-- 配置业务逻辑组件 -->    <bean id="MessageManagerBusinessImpl" class="businessimp.MessageManageerBusinessImpl">        <property name="userDao" ref="UserDaoImpl"></property>        <property name="messageDao" ref="MessageDaoImpl"></property>    </bean></beans>



UserDaoImpl.java

Java code
package daoimp;import java.util.List;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import mybean.User;import dao.UserDao;public class UserDaoImpl extends HibernateDaoSupport implements UserDao {    private SessionFactory sessionfactory;    public SessionFactory getSessionfactory() {        return sessionfactory;    }    public void setSessionfactory(SessionFactory sessionfactory) {        this.sessionfactory = sessionfactory;    }    public UserDaoImpl() {        // TODO 自动生成构造函数存根    }    public void deleteUser(User u) {        getHibernateTemplate().delete(u);    }    public User findByUserName(String name) {        List<User> ul = getHibernateTemplate().find("from User as u where u.name = ?",name);        if(ul!=null && ul.size()>0){            return ul.get(0);        }        return null;    }    public User getUser(int id) {        return (User)getHibernateTemplate().get(User.class, new Integer(id));    }    public int saveUser(User u) {        getHibernateTemplate().save(u);        return u.getId();    }    public void updataUser(User u) {        getHibernateTemplate().saveOrUpdate(u);    }    public void deleteUser(int id) {        getHibernateTemplate().delete(getHibernateTemplate().get(User.class, new Integer(id)));    }}
  相关解决方案