1. 程式人生 > >Spring學習筆記(七)

Spring學習筆記(七)

全局 tex ng- dev web.xml filter scanner index.jsp batis

本教程對應視頻課程:http://edu.51cto.com/course/14731.html

1、Struts2-Spring-MyBatis整合

1.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>

1.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>

最後:做一次測試即可

Spring學習筆記(七)