创建名称为'trangchuController'的bean时出错:通过字段'sessionFactory'表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的类型为org.hibernate.SessionFactory的合格Bean:至少应有1个可以作为自动装配候选的Bean。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
IoC.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<context:component-scan base-package="tuan.controller"/>
<context:component-scan base-package="tuan.entity"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/db_thoitrang" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="tuan.entity"></property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
控制器
package tuan.controller;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tuan.entity.NhanVien;
@Controller
@RequestMapping("/")
public class trangchuController {
@Autowired
SessionFactory sessionFactory;
@GetMapping
@Transactional
public String index() {
Session session = sessionFactory.getCurrentSession();
String sql = "from nhanvien";
List<NhanVien> list = session.createQuery(sql).getResultList();
for(NhanVien nv: list) {
System.out.println(nv.getTenNhanVien()+" : "+nv.getTuoi());
}
return "trangchu";
}
}
NanVien
`package tuan.entity;
import javax.persistence.Entity; 导入javax.persistence.Id;
@Entity(name="nhanvien")
public class NhanVien {
@Id
int idNhanVien;
String TenNhanVien;
int Tuoi;
public int getId() {
return idNhanVien;
}
public void setId(int idNhanVien) {
this.idNhanVien = idNhanVien;
}
public String getTenNhanVien() {
return TenNhanVien;
}
public void setTenNhanVien(String tenNhanVien) {
TenNhanVien = tenNhanVien;
}
public int getTuoi() {
return Tuoi;
}
public void setTuoi(int tuoi) {
Tuoi = tuoi;
}
}