使用HibernateTemplate进行Spring Hibernate集成

时间:2015-11-10 19:08:32

标签: hibernate spring-mvc

我正在 Netbeans IDE 中使用HibernateTemplate进行我的第一个Spring + Hibernate集成示例。导入所有spring和hibernate相关的jar,包括 ojdbc14.jar

我已在Oracle 12G数据库中创建了一个名为 userstable 的表,该表的列名与我的model / POJO类中声明的属性相同。

我在Glassfish服务器窗口中粘贴了错误的主要部分/警告。以下是错误:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT



Caused by: java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT



Severe:   Startup of context /TestingMyGDPProject failed due to previous errors
Severe:   Exception during cleanup after start failed
org.apache.catalina.LifecycleException: Manager has not yet been started


 Severe:   ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT



Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT


Caused by: java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT



Warning:   java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT



 Severe:   Exception while invoking class com.sun.enterprise.web.WebApplication start method
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT


Severe:   Exception during lifecycle processing
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is        java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT


Severe:   Exception while loading the app
Severe:   Undeployment failed for context /TestingMyGDPProject
Severe:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [DataDAO.UserDAOImplementationHT] for bean with name 'ddd' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: DataDAO.UserDAOImplementationHT

Here is My Project Structure

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">



<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
    <!--<url-pattern>/</url-pattern>-->
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>registration.jsp</welcome-file>
</welcome-file-list>

调度-servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"  
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


  <context:component-scan base-package="Controller" />  
  <mvc:annotation-driven />  


                <!--VIEW RESOLVER-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="prefix" value="/View/"></property>
      <property name="suffix" value=".jsp"></property>
</bean>    

的applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
 <!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
  <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     
    <property name="driverClassName"  value="oracle.jdbc.OracleDriver"></property>  
    <property name="url" value="jdbc:oracle:thin:@localhost:1521/ORCL"></property>  
    <property name="username" value="system"></property>  
    <property name="password" value="Oracle_1"></property>  
</bean> 


<bean id="mysessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">            
    <property name="dataSource" ref="ds"></property>
    <property name="hibernateProperties">  
        <props>  
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>      
        </props>  
    </property>              
    <property name="mappingResources">  
        <list>  
            <value>resources/user.hbm.xml</value>  
        </list>  
    </property> 
</bean>  


<bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">              
    <property name="sessionFactory" ref="mysessionFactory"></property>          
</bean>     

<bean id="ddd" class="DataDAO.UserDAOImplementationHT">  
    <property name="ht" ref="template"></property>      
</bean>

DAO接口实施类

package DataDAO;

import Model.User;

import org.springframework.orm.hibernate4.HibernateTemplate;


public class UserDAOImplementationHT implements UserDAO{

private HibernateTemplate ht;

public void setHt(HibernateTemplate ht) {
    this.ht = ht;
}

public UserDAOImplementationHT() {
}

@Override
public void registerAnUser(User newuser) {
    ht.save(newuser);
}    
}

服务层接口实施类

package DataService;


import DataDAO.UserDAO;


import Model.User;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class DataServiceImplementation implements DataService{



ConfigurableApplicationContext cap = new ClassPathXmlApplicationContext("applicationContext.xml");

UserDAO daoob = (UserDAO) cap.getBean("ddd");             



@Override
public void registerAnUser(User newuser) {
   daoob.registerAnUser(newuser);       
}    
}

控制器类

@Controller
public class NewController  extends MultiActionController{


DataService dataservice;  



@RequestMapping("/register.htm")
public ModelAndView regUser(HttpServletRequest request, HttpServletResponse response) throws ParseException {          


    if(request.getParameter("regPassword").equals(request.getParameter("regConfrimPass"))){  
        String myDateFormat = request.getParameter("date1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Date dateofbirth = sdf.parse(myDateFormat);

        User newUser = new User(request.getParameter("email"),request.getParameter("regPassword"),request.getParameter("regFirstname"),request.getParameter("regLastname"),request.getParameter("regUniversityname"),Long.parseLong(request.getParameter("regPhonenum")),dateofbirth);                          
        dataservice.registerAnUser(newUser);

        return new ModelAndView("SuccessPage");
    }
    else {
        return new ModelAndView("FailurePage");
    }
}

}

有谁可以帮我解决这个错误?我一直试图通过引用互联网上的几种资源来解决这个问题,但在每一次尝试都失败了。

提前致谢。

0 个答案:

没有答案