程序猿如何进阶成攻城狮 2016-04-21 06:51 采纳率: 37.5%
浏览 1206
已结题

配置问题,求大神解惑

我使用的是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


  • 写回答

1条回答

  • qq_25340713 2016-04-21 08:12
    关注
    <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
    <!-- 使用hibernateProperties属性来配置Hibernate原生的属性 -->
        这个文件配置的地方你已经注释掉了,spring扫描的时候,扫描不到,还有就是这个文件需要凡在和applicationContext.xml文件同样的包下面
    
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用