开发者社区> 问答> 正文

springmvc+hibernate注入sessionFactory一直报错N?400报错

求大神解答,试了很多方法都没解决。

相关bean.xml:

<bean id="dataSource" class="com.mysql.jdbc.Driver"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="100" />
        <property name="maxidle" value="20" />
        <property name="maxWait" value="1000" />
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties" />
    

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packageToScan">
            <value>spring.mvc.model</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.fomrat_sql">false</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
    </bean>

Dao类

package spring.mvc.dao.imp;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import spring.mvc.dao.UserDao;
import spring.mvc.model.Pager;
import spring.mvc.model.SystemContext;
import spring.mvc.model.User;

@SuppressWarnings("deprecation")
@Repository("userDaoImp")
public class UserDaoImp extends HibernateDaoSupport implements UserDao {
    
    @Resource(name="sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        this.setSessionFactory(sessionFactory);
    }
    
    

    @Override
    public void add(User user) {
        this.getHibernateTemplate().save(user);
    }

    @Override
    public void update(User user) {
        this.getHibernateTemplate().update(user);

    }

    @Override
    public void delete(int id) {
        User user = this.load(id);
        this.getHibernateTemplate().delete(user);

    }

    @Override
    public User load(int id) {
        return this.getHibernateTemplate().load(User.class, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> list() {
        return this.getSession().createQuery("from User").list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager<User> find() {
        int size = SystemContext.getSize();
        int offset = SystemContext.getOffset();
        Query query = this.getSession().createQuery("from User");
        query.setFirstResult(offset).setMaxResults(size);
        List<User> datas = query.list();
        Pager<User> us = new Pager<User>();
        us.setDatas(datas);
        us.setOffset(offset);
        us.setSize(size);
        long total = (long) this.getSession()
                .createQuery("select count(*) from User").uniqueResult();
        us.setTotal(total);
        return us;
    }

    @Override
    public User loadByUsername(String username) {
        return (User) this.getSession().createQuery("from User where username=?").setParameter(0, username).uniqueResult();
    }

}

异常:

严重: Servlet.service() for servlet [user] in context with path [/spring_user] threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:701)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:276)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:261)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:192)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
 

 

展开
收起
爱吃鱼的程序员 2020-06-08 13:25:16 723 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    @Resource(name="sessionFactory")
       publicvoidsetSuperSessionFactory(SessionFactorysessionFactory){
          this.setSessionFactory(sessionFactory);
       }

     

    换成

     

    @Resource(name="sessionFactory")

    private SessionFactorysessionFactory;


       publicvoidsetSuperSessionFactory(){
          this.setSessionFactory(sessionFactory);
       }

    2020-06-08 13:25:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载