ssh整合註解

第一步 web.xml

        添加struts2的過濾器和spring的監聽器

<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>
 
   <listener>
      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
  <listener>
    <listener-class>
            org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <context-param>
            <param-name>contextConfigLocation</param-name>
          <param-value>WEB-INF/classes/applicationContext*.xml</param-value>    
   </context-param>

第二步 application.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: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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 數據源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/ssh_demo2"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>

</bean>

<!-- 配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <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="mappingDirectoryLocations">
        <array>
            <value>classpath:com/luo/cn/bean</value>
        </array>
    </property> -->
    <!-- 自動掃描實體 -->
    <property name="packagesToScan">
        <array>
            <value>com.luo.cn.bean</value>
        </array>
    </property>
    
</bean>
<bean id="txMgr" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven  transaction-manager="txMgr"/>
<!-- 自動掃描裝配bean -->
<context:component-scan base-package="com.luo.cn"/>
</beans>

第三步 建立實體,以及對應的action service dao

 

 @Service用於標註業務層組件,
     @Controller用於標註控制層組件(如struts中的action),@Controller默認產生的Bean的name就是類(UserAction)的第一個字母小寫(userAction)。
                當然你也可以自己設定啊,@Controller("uu")
     @Repository用於標註數據訪問組件,即DAO組件,
     @Component泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章