struts2.3.4+hibernate4.1.1+spring3.2(二)简单例子

简单跑了一个例子,发现spring3不支持hibernate4的hibernatetemplate很无语。

datasource用的c3p0,sessionfactory用的hibernate4的

先贴下配置文件:

下面是web.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

   </welcome-file-list>

  <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

<!-- default: /WEB-INF/applicationContext.xml -->

</listener>

  <filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>



struts.xml的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="user" extends="struts-default">
<action name="login" class="com.cvicse.action.LoginAction">
<result name="success">/success.jsp</result>
</action>
</package>

</struts>

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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cvicse" />

<!-- 配置数据源  -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="user" value="root"></property>
<property name="password" value="cvicse"></property>
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>com.cvicse.db</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>


    <!-- 自定义的UserDao -->
    <bean id="userDao" class="com.cvicse.dao.UserDao" >
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

<!-- 自定义UserService,注入UserDao -->
<bean id="userService" class="com.cvicse.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>

<!-- 自定义LoginAction,注入userService -->
<bean id="login" class="com.cvicse.action.LoginAction">
<property name="userService" ref="userService"></property>
</bean>

</beans>

猜你喜欢

转载自87430997.iteye.com/blog/1757059