9、Struts2-Spring-MyBatis整合

9.1、Spring-Mybaits整合

添加整合包:

mybatis-spring-1.3.2.jar

修改配置文件

  <!--获取 sqlSessionFactory  -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   		 <!-- 依赖数据源对象 -->
 		 <property name="dataSource" ref="ds" />
 		 <!-- 加载mybatis的配置文件 -->
 		 <property name="configLocation" value="classpath:mybatis-config.xml"/>
   </bean>
   <!-- 注入映射器 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		 <property name="basePackage" value="cn.org.kingdom.mapper" />
	</bean>

注意对于mybatis原有的配置文件,在做一次修改

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
		<!-- 二级缓存的全局开关 -->
		<setting name="cacheEnabled" value="true"/>
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/> 
	</settings>
	<typeAliases>
		<package name="cn.org.kingdom.pojo"/>
	</typeAliases>
 
  <mappers>
  	<package name="cn.org.kingdom.mapper"/>
  </mappers>
</configuration>

9.2、Spring-Struts2整合

1、添加jar包

spring-web-4.3.14.RELEASE.jar、struts2-spring-plugin-2.3.15.3.jar

2、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  	<context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:SpringContext.xml</param-value>
  	</context-param>
  	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </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>

3、注意:

<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
		<!-- class:只要写action的id即可 -->
       	<action name = "user_Action" class="userAction" method="list">
       		<result name = "success">list.jsp</result>
       	</action>
    </package>
</struts>

最后:做一次测试即可

posted @ 2021-04-09 16:15  Mirindasky  阅读(73)  评论(0编辑  收藏  举报