问题描述
- 配置问题,求大神解惑
-
我使用的是applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="bean"></context:component-scan> <!-- 配置数据源 --> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置 Hibernate 的 SessionFactory 实例 : 通过Spring 提供的LocalSessionFactoryBean配置--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置数据源属性 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置 hibernate 配置文件的名称及位置 --> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> --> <!-- 使用hibernateProperties属性来配置Hibernate原生的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> </props> </property> <!-- 配置 hibernate 映射文件的位置及名称 , 可以使用通配符--> <property name="mappingResources"> <list> <value>bean/card.hbm.xml</value> </list> </property> </bean> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" propagation="REQUIRED" /> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* dao.*.*(..))"/> --> <aop:pointcut expression="execution(* dao.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
执行
package test; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import bean.card; import bean.test; public class test1{ /** * @param args */ public static void main(String[] args) { SessionFactory sessionFactory = null; Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); //SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory() 注解为AnnotationConfiguration //2. Sesson Session session = sessionFactory.openSession(); //3. 开启事务 Transaction transaction = session.beginTransaction(); //4. 执行保存操作 card t=new card(); t.setCardid("123"); t.setCardnum(12221); t.setId(12); session.save(t); //5. 提交事物 transaction.commit(); //6. 关闭Session session.close(); //7. 关闭SessionFectory sessionFactory.close(); System.out.println("成功!"); }}
报下面这个错 是为什么呢?
```Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
解决方案
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<!-- 使用hibernateProperties属性来配置Hibernate原生的属性 -->
这个文件配置的地方你已经注释掉了,spring扫描的时候,扫描不到,还有就是这个文件需要凡在和applicationContext.xml文件同样的包下面
时间: 2024-10-30 02:41:57