大家好我还是新的西斯春天和hibernate我想连接到db 但我在控制台中收到此错误,我正在寻找你的帮助 这是我的代码
UserBeanRegister
package com.pfa.bean;
public class UserBeanRegister {
private int idUser;
private String nameUser;
private String pwdUser;
private String emailUser;
private int iCv;
public int getIdUser() {
return idUser;
}
public void setIdUser(int idUser) {
this.idUser = idUser;
}
public String getNameUser() {
return nameUser;
}
public void setNameUser(String nameUser) {
this.nameUser = nameUser;
}
public String getPwdUser() {
return pwdUser;
}
public void setPwdUser(String pwdUser) {
this.pwdUser = pwdUser;
}
public String getEmailUser() {
return emailUser;
}
public void setEmailUser(String emailUser) {
this.emailUser = emailUser;
}
public int getiCv() {
return iCv;
}
public void setiCv(int iCv) {
this.iCv = iCv;
}
}
LoginController.java
package com.pfa.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@RequestMapping(value="/admissionForm.html", method = RequestMethod.GET)
public ModelAndView getAdmissionForm() {
ModelAndView model = new ModelAndView("AdmissionForm");
return model;
}
@RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
public ModelAndView submitAdmissionForm(@RequestParam Map<String,String> reqPar)
{
String name = reqPar.get("nameUser");
String hobby = reqPar.get("pwdUser");
ModelAndView model = new ModelAndView("AdmissionSuccess");
model.addObject("msg","Details submitted by you:: Name: "+name+ ", Hobby: " + hobby);
return model;
}
}
userDAO的
package com.pfa.dao;
import com.pfa.model.User;
public interface UserDao {
public void registerUser(User user);
}
UserDaoImpl.java
package com.pfa.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.pfa.model.User;
@Repository("UserDao")
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void registerUser(User user) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
}
User.java
package com.pfa.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = -723583058586873479L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "idUser")
private Integer idUser;
@Column(name="nameUser")
private String nameUser;
@Column(name="pwdUser")
private String pwdUser;
@Column(name="emailUser")
private Long emailUser;
@Column(name="idCv")
private Integer idCv;
public Integer getIdUser() {
return idUser;
}
public void setIdUser(Integer idUser) {
this.idUser = idUser;
}
public String getNameUser() {
return nameUser;
}
public void setNameUser(String nameUser) {
this.nameUser = nameUser;
}
public String getPwdUser() {
return pwdUser;
}
public void setPwdUser(String pwdUser) {
this.pwdUser = pwdUser;
}
public Long getEmailUser() {
return emailUser;
}
public void setEmailUser(Long emailUser) {
this.emailUser = emailUser;
}
public Integer getIdCv() {
return idCv;
}
public void setIdCv(Integer idCv) {
this.idCv = idCv;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
UserService.java
package com.pfa.service;
import com.pfa.model.User;
public interface UserService {
public void registerUser(User user);
}
UserServiceImpl.java
package com.pfa.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.pfa.dao.UserDao;
import com.pfa.model.User;
@Service("UserService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserServiceImpl implements UserService{
@Autowired
private UserDao UserDao;
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void registerUser(User user) {
UserDao.registerUser(user);
}
}
弹簧调度-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
<context:component-scan base-package="com.gontuseries.studentadmissioncontroller" />
<mvc:annotation-driven />
<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.pfa" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.pfa.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto} </prop>
</props>
</property>
</bean>
</beans>
database.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/pfa_db
database.user=root
database.password=
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
AdmissionForm.jsp
<html>
<body>
<h1> STUDENT ADMISSION FORM FOR ENGINEERING COURSES</h1>
<form action="/FirstSpringMVCProject/submitAdmissionForm.html" method="post">
<p>
User : <input type="text" name="nameUser" />
</p>
<p>
password : <input type="text" name="pwdUser" />
</p>
<input type="submit" value="Submit this form" />
</form>
</body>
</html>
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>FirstSpringMVCProject</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
并且有一些stackk跟踪
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could no load properties; nested exception is java.io.FileNotFoundException: class path resource [resources/database.properties] cannot be opened because it does not exist
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessB eanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactory PostProcessors(AbstractApplicationContext.java:663)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactory PostProcessors(AbstractApplicationContext.java:638)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractA pplicationContext.java:407)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(Fra meworkServlet.java:442)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(Fra meworkServlet.java:458)
at o org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(Frame workServlet.java:339)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServle t.java:306)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4909 )
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:519 )
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: class path resource [resources/database.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.j ava:158)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(Prope rtiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(Pro pertiesLoaderSupport.java:161)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessB eanFactory(PropertyResourceConfigurer.java:69)
... 21 more