SSMA(SSM)的配置

SSMA

SpringMvc +Spring +Mybatis+Ajax=SSMA(基于maven)

1.springmvc

    1.1 springmvc核心配置jar包
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.bundles</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8_2</version>
        </dependency>
        当配置完后,maven会自动配置其他所依赖的jar包

    1.2 springmvc的核心配置文件
        首先在web.xml文件中配置DispatcherServlet
  <!-- 配置DispatcherServlet -->
    <servlet>
        <servlet-name>mymvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置DispatcherServlet -->
    <servlet>
        <servlet-name>mymvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 自定义springmvc配置文件名 -->
<init-param>
    <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/springmvc.xml</param-value>
        </init-param>   
        <!-- 在服务器启动时就初始化(必须是大于0的值)-->
        <load-on-startup>1</load-on-startup>
    </servlet>
<!-- 拦截所有请求 -->
<servlet-mapping>
        <servlet-name>mymvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>  
其次在web.xml同级目录下创建一个xml文件(填入下方的代码),命名规则是web.xml中的标签<servlet-name>中填写的值加“-Servlet”,默认这里应该命名为“mymvc-Servlet.xml”,但是我自定义了命名,所以应该是“springmvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    ">
    <!-- 类扫描,springmvc只扫描控制层 -->
    <context:component-scan base-package="ssam"></context:component-scan>
    <!-- 类扫描,springmvc只扫描控制层 -->
    <context:component-scan base-package="ssam.controller">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
</beans>

    1.2 配置支持restful风格的url   
            Web.xml中添加
            <!-- 配置过滤器,请求method支持 put和delete必须添加过滤器,注意要在请求中要有_mehtod-->
    <filter>
        <filter-name>coding</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</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>
    <filter-mapping>
        <filter-name>coding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    1.3 配置字符拦截器
Web.xml中添加
        <!-- 配置拦截器设置字符集 -->
    <filter>
        <filter-name>charsetFilter</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>
    <filter-mapping>
        <filter-name>charsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2.spring

2.1 创建spring配置文件
    在根目录下创建spirng.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    ">
    <!-- spring扫描service和repository -->
    <context:component-scan base-package="ssam">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>
    2.2 配置spring
        web.xml中加
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    2.3配置消息转换器
        Springmvc.xml中添加
        <!-- 配置消息转换器MappingJackson2HttpMessageConverter -->
    <mvc:annotation-driven>
        <mvc:message-converters>
<!-- 数组转换器 -->
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html</value>
                        <value>application/x-www-form-urlencoded</value>
                    </list>
                </property>
            </bean>
        <!-- 对象转换器 -->
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html</value>
                        <value>application/x-www-form-urlencoded</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
2.4 配置数据源
        这里使用druid
    Pom.xml中添加
<dependency>
        <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.5</version>
</dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
</dependency>
    spring.xml添加
    <!-- 配置数据源 -->
    <context:property-placeholder location="class:jdbc.properties"/>
    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${url}"></property>
        <property name="username" value="${username1}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClassName" value="${driverClass}"></property>
        <property name="initialSize" value="1"></property>
        <property name="maxActive" value="200"></property>  
        <property name="maxIdle" value="100"></property>
        <property name="filters" value="stat"></property>
    </bean>

3.mybatis

3.1 添加依赖
    Pom.xml添加
    <dependency>
     <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
     <version>1.2.3</version>
</dependency>
<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
</dependency>
3.2 配置session工厂
Pom.xml中添加
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>   </bean>
<!-- 扫描mybatis接口映射 -->
    <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="ssam.mapper"></property>
    </bean>
3.3 配置事务
    <!-- 事务管理器 -->
    <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
    <tx:advice id="txAdvice" transaction-manager="tm">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut expression="execution(* ssam.service.*.*(..))" id="myPonintCut"/>
        <!-- 管联切点和事务管理器 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/>
    </aop:config>

4.AJAX

    //封装ajax
    function sendAjax(url, methodType, param, retnFunction) {
        //首先创建xmlhttp对象
        var xmlhttp = null;
        //根据浏览器创建不同的对象,兼容所有浏览器(XHR)
        if (window.XMLHttpRequest) {//适应 IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// 适应 IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        //readyState 存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。0: 请求未初始化
        //1: 服务器连接已建立
        //2: 请求已接收
        //3: 请求处理中
        //4: 请求已完成,且响应已就绪
        //status 状态码 200: "OK"  404: 未找到页面(还有很多)
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                retnFunction(xmlhttp.responseText);
            }
        }
        if (methodType.toUpperCase() == "GET") {
            xmlhttp.open("GET", url + "?" + param, true);
            xmlhttp.send();
        } else {
            xmlhttp.open("POST", url, true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
            alert(param);
            xmlhttp.send(param);
        }
    }

5.配置druid监控

web.xml添加
<!-- druid监控 -->
    <servlet>
        <servlet-name>statViewServlet</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <init-param>
            <param-name>loginUsername</param-name>
            <param-value>admin</param-value>
        </init-param>
        <init-param>
            <param-name>loginPassword</param-name>
            <param-value>zyp</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>statViewServlet</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值