2014-10-07 126 views
0

我創建了一個Spring + Hibernate項目,但無法正常工作。我在哪裏做錯了? 爲什麼我寫sessionfactory.getCurrentSession時會出錯?我總是要寫sessionfactory.openSession。這是我第二個使用hibernate的項目。在第一個項目中,我使用JPA批註在類學生,但現在我已經創建了一個文件student.hbm.xmlSpring Hibernate項目無法正常工作

錯誤代碼

ott 07, 2014 11:10:21 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
Informazioni: Refreshing org[email protected]c5eb8a: startup date [Tue Oct 07 11:10:21 CEST 2014]; root of context hierarchy 
ott 07, 2014 11:10:21 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
Informazioni: Loading XML bean definitions from class path resource [springConfig.xml] 
ott 07, 2014 11:10:22 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} 
ott 07, 2014 11:10:22 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.3.6.Final} 
ott 07, 2014 11:10:22 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
ott 07, 2014 11:10:22 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
ott 07, 2014 11:10:24 AM org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
ott 07, 2014 11:10:24 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
ott 07, 2014 11:10:24 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> 
INFO: HHH000397: Using ASTQueryTranslatorFactory 
ott 07, 2014 11:10:25 AM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet 
Informazioni: Using DataSource [[email protected]] of Hibernate SessionFactory for HibernateTransactionManager 
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [coreservlets.StudentDAOImpl] is defined 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:338) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:298) 
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968) 
at coreservlets.MainApp.main(MainApp.java:12) 

Student.java

package coreservlets; 

public class Student { 

private Integer id; 

private String name; 

private Integer age; 

public Integer getId(){return id;}//getId 

public void setId(Integer id){this.id=id;}//setId 

public String getName(){return name;}//getName 

public void setName(String name){this.name=name;}//setName 

public Integer getAge(){return age;}//getAge 

public void setAge(Integer age){this.age=age;}//setAge 

}//Student 

StudentDAO。 java的

package coreservlets; 

import org.hibernate.SessionFactory; 

public interface StudentDAO { 

public void setSessionFactory(SessionFactory sessionFactory); 

public void create(String name,Integer age); 

}//StudentDAO 

StudentDAOImpl.java

package coreservlets; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Transactional; 

@Repository 
public class StudentDAOImpl implements StudentDAO { 

private SessionFactory sessionFactory; 

@Autowired 
public void setSessionFactory(SessionFactory sessionFactory){ 
    this.sessionFactory=sessionFactory; 
}//setSessionFactory 

@Transactional 
public void create(String name,Integer age){ 
    Session session=sessionFactory.getCurrentSession(); 
    Student student=new Student(); 
    student.setName(name); 
    student.setAge(age); 
    session.persist(student); 
}//create 

}//StudentDAOImpl 

MainApp.java

package coreservlets; 

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

public class MainApp { 

public static void main(String[] args) { 

    ApplicationContext context=new ClassPathXmlApplicationContext("springConfig.xml"); 

    StudentDAOImpl student=(StudentDAOImpl)context.getBean(StudentDAOImpl.class); 

    student.create("John", new Integer(33)); 

}//main 

}//MainApp 

springConfig.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:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 

<tx:annotation-driven transaction-manager="transactionManager"/> 

<context:annotation-config/> 


<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/spring_hibernate"/> 
    <property name="username" value="root"/> 
    <property name="password" value="password"/> 
    <property name="initialSize" value="5"/> 
    <property name="maxTotal" value="10"/> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
<property name="dataSource" ref="dataSource"/> 
<property name="mappingResources"> 
    <list> 
     <value>student.hbm.xml </value> 
    </list> 
</property> 
<property name="annotatedClasses"> 
    <list> 
    <value>coreservlets.Student</value> 
    </list> 
</property> 
<property name="hibernateProperties"> 
    <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
    </props> 
</property> 

</bean> 

</beans> 

student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="coreservlets"> 
<class name="Student" table="student"> 
    <id name="id" column="id"> 
     <generator class="native"/> 
    </id> 
    <property name="name" column="name" /> 
    <property name="age" column="age" /> 
</class> 
</hibernate-mapping> 

SQL代碼

create table student 
(
id integer not null auto_increment, 
name varchar(20) not null, 
age integer not null, 
primary key(id) 
); 
+0

在你的xml中的bean StudentDAOImpl定義在哪裏? 2014-10-07 09:35:37

+0

我在StudentDaoImpl中使用了@Autowired註解,在springConfig.xml中使用了 Alex 2014-10-07 09:39:21

+0

@Autowired StudentDAO studentDAO;在主類中,但在靜態方法中使用此方法不是一個好習慣 – 2014-10-07 09:42:28

回答

1

接口:

public interface StudentDAO { 

} 

類,它實現接口StudentDAO

@Repository 
public class StudentDAOImpl implements StudentDAO { 

} 

自動裝配StudentDAOImpl類,你必須把@Autowired註釋上接口名稱,你的類:

@Autowired 
private StudentDAO studentDAO; 

也你必須照顧配置,就像你的組件掃描一樣在你的spring配置文件中添加以下行。在彈出配置xml中放入
<context:component-scan base-package="com.org.dao"/>並將com.org.dao更改爲您的軟件包名稱,其中將dao文件放置在應用程序中。

相關問題