我是 Hibernate 和 Spring 的新手,在执行应用程序时遇到了上述异常。
我正在尝试从数据库中获取记录的值。
以下是我得到的例外: -
message Request processing failed; nested exception is org.hibernate.HibernateException: /hibernate.cfg.xml not found
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: /hibernate.cfg.xml not found
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
org.hibernate.HibernateException: /hibernate.cfg.xml not found
org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1497)
org.hibernate.cfg.Configuration.configure(Configuration.java:1519)
org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
com.me.app.HomeController.handleRequestInternal(HomeController.java:56)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.
以下是我的文件:-
POJO
public class Usertable {
int id;
String userName;
String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
控制器
//@Controller
public class HomeController extends AbstractController{
//private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
// @RequestMapping(value = "/", method = RequestMethod.GET)
// public String home(Locale locale, Model model) {
// logger.info("Welcome home! The client locale is {}.", locale);
//
// Date date = new Date();
// DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
//
// String formattedDate = dateFormat.format(date);
//
// model.addAttribute("serverTime", formattedDate );
//
// return "home";
// }
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
Usertable user;
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session hibsession = sf.openSession();
Transaction tx = hibsession.beginTransaction();
user = (Usertable)hibsession.get(Usertable.class, userName);
System.out.println("UserName is "+ user.getUserName());
System.out.println("Password is "+ user.getPassword());
tx.commit();
hibsession.close();
return new ModelAndView("first","abc","abc");
}
休眠.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/contacts</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="Usertable.hbm.xml"/>
</session-factory>
</hibernate-configuration>
用户表.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Mar, 2013 4:26:30 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.me.app.Usertable" table="USERTABLE">
<id name="id" type="java.lang.Integer">
<column name="UserID" />
<generator class="native" />
</id>
<property name="userName" type="java.lang.String">
<column name="UserName" />
</property>
<property name="password" type="java.lang.String">
<column name="UserPassword" />
</property>
</class>
</hibernate-mapping>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- <resources mapping="/resources/**" location="/resources/" /> -->
<beans:bean id="urlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<beans:bean name="/books.htm" class="com.me.app.HomeController" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.me.app" />
</beans:beans>
请帮我解决这个问题。在此先感谢。!