Error creating bean with name 'sessionFactory' defined in class path resource [...]

已解决(spring+hibernate)

报错原因:applicationContext.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="minaexam.Dwsqlentity">
    <!-- 
        name:即实体类的全名
        table:映射到数据库里面的那个表的名称
        catalog:数据库的名称
     -->
    <class name="minaexam.Dwsqlentity" table="histrack" catalog="yzxc_lbs">
        <!-- class下必须要有一个id的子元素 -->
        <!-- id是用于描述主键的 -->
        <id name="histrackid"  column="HistrackId">
            <!-- 主键生成策略 -->
            <generator class="assigned"/></id>
        <!-- 
        assigned:人工插入;native:本地策略
            使用property来描述属性与字段的对应关系
            如果length忽略不写,且你的表是自动创建这种方案,那么length的默认长度是255
        -->

        <property name="terminalserialNo" column="TerminalserialNo"></property>
       
        <property name="receiveTime" column="ReceiveTime" ></property>
        <property name="longitude" column="Longitude" ></property>
        
        <property name="latitude" column="Latitude" ></property>
        <property name="speed" column="Speed" ></property>
        <property name="direction" column="Direction" ></property>
        <property name="gpstime" column="GpsTime" ></property>
    </class>
</hibernate-mapping>

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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-3.0.xsd
	http://www.springframework.org/schema/cache
	http://www.springframework.org/schema/cache/spring-cache.xsd
	">
	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 自动扫描注解,通过注解注入 -->
	<context:component-scan base-package="minaexam" />
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- 基本属性 url、user、password -->
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />

		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="6000" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="6000" />

		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="30000" />

		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />

		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
		<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
		<property name="filters" value="stat" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
		<list>
		<value>minaexam</value>
		</list>
		</property>
	  	<property name="mappingLocations" value="/minaexam/Dwsqlentity.hbm.xml"></property> 
			
		<!-- 第二种配置方式 -->
		<!-- <property name="configLocation" value="classpath:hibernate_mysql.cfg.xml" 
			/> -->
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true
		<!--  		hibernate.connection.autocommit=true -->

			</value>
		</property>
	</bean>

	<!-- 开户事务注解功能 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />

	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
		<property name="cacheQueries">
			<value>true</value>
		</property>
	</bean>
<!-- 	<bean id="httpService"
		class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="command" />
		<property name="serviceInterface" value="com.invoker.CommandInfo" />
	</bean> -->
</beans>

猜你喜欢

转载自blog.csdn.net/qq_28433521/article/details/82627410