ssh框架 使用spring的注解以及hibernate的正向生成

 ssh框架 使用spring的注解以及hibernate的正向生成,做一个注册的例子

  1.导入spring,struts,hibernate的jar包

 

   

2.项 目 的 结 构


3 web.xml的配置的核心代码

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

      <!-- 配置struts -->
      <init-param>
          <param-name>config</param-name>
          <param-value>struts-default.xml,struts-                                       plugin.xml,struts.xml</param-value>
      </init-param>    
    </filter>
<!-- spring容器的初始化 -->
 <listener>
    <listener-class>

    org.springframework.web.context.ContextLoaderListener

   </listener-class>
</listener>


<!-- 指定加载的配置文件 -->
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
 <!-- 过滤器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
      <filter-class>

         org.springframework.web.filter.CharacterEncodingFilter

      </filter-class>
      <init-param>
         <param-name>encoding</param-name>
         <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
      </init-param>
  </filter>    
 

4.applicationContext.xml的配置文件

     <!-- 头文件  --!>

  <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.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-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">

  <!-- 开启spring注解方式 -->
  <context:annotation-config></context:annotation-config>
  <context:component-scan base-package="com.huang.ssh">

  </context:component-scan>
 <!-- 配置数据集 -->
  <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">

   <property    

    name="driverClassName" value="com.mysql.jdbc.Driver"> 

  </property>
  <property name="url" value="jdbc:mysql://localhost:3306/test">

  </property>    
  <property name="username" value="root"></property>
  <property name="password" value="123456"></property>
 </bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory"     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 <property name="dataSource">
    <ref bean="dataSource" />
   </property>
   <property name="hibernateProperties">
     <props>
        <prop key="hibernate.dialect">
              org.hibernate.dialect.MySQLDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.hbm2ddl.auto">update</prop>
     </props>
   </property>
        
<!-- 加载实体类的包名 -->
<property name="packagesToScan" value="com.huang.ssh.entity"></property>
    </bean> 
  <!-- 配置事务管理器,使用spring来管理hibernate的事务 -->
  <bean id="transactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <aop:config>
  <!-- 需要进行事务管理的方法 -->
  <aop:pointcut expression="execution(* com.huang.ssh.biz.*.*(..))"
  id="daoMethods" />
  <aop:advisor advice-ref="transcationAdvice" pointcut-ref="daoMethods" />
  </aop:config>

 <tx:advice id="transcationAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 <tx:method name="*" propagation="REQUIRED" />
 </tx:attributes>
    </tx:advice>

5.实体类User

                     

   6.  BaseDao类的代码  

 

 

 7. UserDao类的代码

    

8.UserServlte类的代码

 

   

  9 Add.action类的代码

 

  

 10.struts.xml的配置文件

   

 11.一个注册的index.jspyem

    


 12.注册页面

 

13.结果

    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值