简单实现添加用户功能,仅供初学者参考,可自行扩展程序功能(增删改查)。

struts2+spring+hibernte整合示例

这里贴下代码,需要的可以下载看(因为比较懒)。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.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.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">  
   
   <!-- 引入外部配置文件 -->
   <context:property-placeholder location="classpath:jdbc.properties"/>
   <!-- 配置连接池 -->
   <bean >
           <property name="driverClass" value="${jdbc.driverClass}"/>
           <property name="jdbcUrl" value="${jdbc.url}"/>
           <property name="user" value="${jdbc.username}"/>
           <property name="password" value="${jdbc.password}"/>
   </bean>
   
   <!-- 配置Hibernate相关属性 -->
   <bean >
           <!-- 注入连接池 -->
           <property name="dataSource" ref="dataSource"/>
           <!-- 配置Hibernate的属性 -->
           <property name="hibernateProperties">
               <props>
                   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                   <prop key="hibernate.show_sql">true</prop>
                   <prop key="hibernate.format_sql">true</prop>
                   <prop key="hibernate.hbm2ddl.auto">update</prop>
               </props>
           </property>
           
           <!-- 加载Hibernate中的映射文件 -->
           <property name="mappingResources">
               <list>
                   <value>cn/bj/ssh/entity/User.hbm.xml</value>
               </list>
           </property>
           
   </bean>
   <!-- 配置Action类 -->
   <bean >
           <!-- 手动注入service -->
           <property name="userService" ref="userService"/>
   </bean>
   
   
   <!-- 配置业务的类 -->
   <bean >
           <property name="userDao" ref="userDao"/>
   </bean>
   
   <!-- 配置DAO的类 -->
   <bean >
           <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
   
   <!-- 配置事务管理器 -->
   <bean >
           <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
   
   <!-- 开启注解事物 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
 </beans>
View Code

相关文章: