我的配置文件是这样的:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 其他datasource有C3P0和BoneCP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="packagesToScan" value="com.apress.prospring3.ch9.domain"/> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:component-scan base-package="com.springhibernate.hibernate" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
然后主程序是这样:
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:HibernateSessionFactory.xml");
// ctx.refresh();
ContactDao contactDao = ctx.getBean("contactDao", ContactDao.class);
SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
// DataSource datasource = (DataSource)ctx.getBean("dataSource");
System.out.println(sessionFactory.toString());
我尝试过获取DataSource是没问题的,可是就是死活获取不了SessionFactory,提示java.lang.ClassNotFoundException: org.hibernate.MappingException,请各位大侠指教指教
------解决思路----------------------
你在实体类上面都加注解了么?
------解决思路----------------------
我觉得楼上说的对,可能是你的hibernate不完整啊
------解决思路----------------------
类没找到的错误:
首先确认你的程序jar包没问题,不缺少同时都已被加载。
然后重新编译整个工程,再运行一下试试。
------解决思路----------------------
实体类 注解加了么? 你看看你的实体映射文件在么,或者实体类上面加了映射的注解了么
<!-- 这个是声明要扫描的 xxx.hbm.xml所在的文件(也扫描了对应的类 ),主要是XXX.hbm.xml文件,更具该文件生成数据库表-->这个是一种
<property name="mappingDirectoryLocations" >
<list>
<value>classpath:com/zsq/sjcj1/model</value>
</list>
</property>
要是用的是hibernate的注解的话,必须要配置扫描的实体类所在的包