Spring与Hibernate混合编程(一)

来源:互联网 发布:淘宝天下天下网商区别 编辑:程序博客网 时间:2024/06/08 06:36

一、创建Java Web项目,创建数据库,添加Hibernate支持,使用数据库反向工程建立POJO类和DAO层代码;

二、添加Spring支持;

此时Run as>>MyEclipse Server Application时,报错: 

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]


而且,浏览器显示404。


原因:Spring容器没有配置数据源!

要在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:p="http://www.springframework.org/schema/p"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-4.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/myuser?useSSL=false" /><property name="username" value="root" /><property name="password" value="admin" /></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"></property><property name="dataSource" ref="dataSource"></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>


0 0
原创粉丝点击