SIS整合
1、Spring&Mybatis&Struts2整合步骤:
1)src目录下新建Spring的配置文件:applicationContext.xml;
--------------------------------------------------------------------------------------------------------------------------------------------------
2)在Spring的配置文件配置数据库连接池:
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" class="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" class="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="usename" class="test"></property>
<property name="password" class="123456"></property>
</bean>
--------------------------------------------------------------------------------------------------------------------------------------------------
3)指定要连接的资源,连接数据库:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<!-- 指定映射文件,指定到所有的xml文件 -->
<property name="mapperLocations" value="classpath:org/great/bean/*.xml"/>
</bean>
--------------------------------------------------------------------------------------------------------------------------------------------------
4)扫描所需的Mapper:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描org.great.mapper下的所有mapper -->
<property name-"basePackage" value="org.great.mapper"/>
<!-- 以下为了防止mapper包里有不需要定义的类,指定当注解为AllMapper时才实例化 -->
<property name="annotationClass" value="org.great.mapper.AllMapper" />
</bean>
--------------------------------------------------------------------------------------------------------------------------------------------------
5)在Spring的配置文件中开启组件扫描:
<context:component-scan base-package="org.great"></context:component-scan>
--------------------------------------------------------------------------------------------------------------------------------------------------
6)配置web.xml文件整合Struts2:
a.配置原有struts2前段控制器:
<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>
b.添加监听:
<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>//applicationContext.xml为Spring的配置文件名
</context-param>
--------------------------------------------------------------------------------------------------------------------------------------------------
7)在src目录下新建struts.xml配置文件(与原有struts2配置文件内容一致);
--------------------------------------------------------------------------------------------------------------------------------------------------
8)在action中使用Spring注解@Controller对action进行注解,以实现bean的定义;
--------------------------------------------------------------------------------------------------------------------------------------------------
9)在action中引用Mapper接口,获得接口中的方法;
--------------------------------------------------------------------------------------------------------------------------------------------------
10)由于action中的Mapper属性参数来源于Spring中bean的定义,因此在此上注解@Resource,注入参数;
--------------------------------------------------------------------------------------------------------------------------------------------------
浙公网安备 33010602011771号