我是Spring MVC和hibernate的新手,我一直处于低迷状态,所以希望你们能提供帮助。每次我运行时,我都会在创建 bean 时遇到异常错误。我似乎无法在互联网上找到正确的答案,我似乎无法找到我在这里缺少的东西。我希望你们能帮助我。我的 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:tx="http://www.springframework.org/schema/tx"
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/" />
<!-- 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:annotation-config/>
<context:component-scan base-package="com.test.cedric." />
<beans:bean id="departmentController" class="com.test.cedric.controller.DepartmentController" />
<beans:bean id="departmentDaoImpl" class="com.test.cedric.dao.DepartmentDaoImpl">
</beans:bean>
<beans:bean id="departmentServiceImpl" class="com.test.cedric.service.DepartmentServiceImpl">
</beans:bean>
</beans:beans>
<?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">
My hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test1</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">validate</property>
<mapping resource="com.test.cedric.model.Department"/>
</session-factory>
</hibernate-configuration>
我的服务代码
package com.test.cedric.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.cedric.dao.DepartmentDao;
import com.test.cedric.dao.DepartmentDaoImpl;
import com.test.cedric.model.Department;
@Service
public class DepartmentServiceImpl implements DepartmentService{
@Autowired
DepartmentDao dDao;
@Transactional
@Override
public void addDept(Department department) {
dDao.addDept(department);
// TODO Auto-generated method stub
}
@Override
public void delDept(Department department) {
dDao.delDept(department);
// TODO Auto-generated method stub
}
@Override
public void editDept(Department department) {
dDao.editDept(department);
// TODO Auto-generated method stub
}
@Override
public List<Department> getAllDept() {
// TODO Auto-generated method stub
return dDao.getAll();
}
@Override
public Department getDeptById(int dept_id) {
// TODO Auto-generated method stub
return null;
}
}
我的 DAO
package com.test.cedric.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.test.cedric.model.Department;
@Repository
public class DepartmentDaoImpl implements DepartmentDao {
@Autowired
SessionFactory sessionFactory;
@Override
public void addDept(Department department) {
sessionFactory.getCurrentSession().saveOrUpdate(department);
}
@Override
public void delDept(Department department) {
sessionFactory.getCurrentSession().delete(department);
}
@Override
public void editDept(Department department) {
sessionFactory.getCurrentSession().saveOrUpdate(department);
// TODO Auto-generated method stub
}
@SuppressWarnings("unchecked")
@Override
public List<Department> getAll() {
sessionFactory.getCurrentSession().createQuery("From Department");
// TODO Auto-generated method stub
return sessionFactory.getCurrentSession().createQuery("From Department").list();
}
@Override
public Department getDeptById(int dept_id) {
// TODO Auto-generated method stub
return null;
}
}