SSM框架整合 原

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

	<display-name>h5web</display-name>

    <!-- 配置中心listener 请保持在listener第一位-->
    <listener>
        <listener-class>com.zhaogang.config.service.init.InitContextLoaderListener</listener-class>
    </listener>

    <!-- spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 请求相应编码为UTF8 -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring的代理过滤器 用以可以在过滤器中完成spring注入 -->
    <filter>
        <filter-name>AbsoluteSendRedirectDelegatingFilterProxy</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>absoluteSendRedirectFilter</param-value>
        </init-param>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>AbsoluteSendRedirectDelegatingFilterProxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- springMvc -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- session 失效时长 单位分钟 -->
    <session-config>
        <session-timeout>10080</session-timeout>
    </session-config>

    <!--未捕获异常处理-->
    <error-page>
        <error-code>404</error-code>
        <location>/error/error.html</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error/error.html</location>
    </error-page>

</web-app>

dispatcher-servlet.xml

<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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 切面配置 -->
    <aop:aspectj-autoproxy />

    <!-- springMvc 拦截器 -->
    <mvc:interceptors>
        <!-- 权限判断拦截器 -->
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/MP_verify_**" /><!-- 微信js工具调用验证 -->
            <mvc:exclude-mapping path="/statics/**" />
            <mvc:exclude-mapping path="/error/**" />
            <bean class="com.zhaogang.webapp.interceptor.WechatAuthInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- springmvc 只扫描com.zhaogang.webapp -->
    <context:component-scan base-package="com.zhaogang.webapp" />

    <!-- 静态文件过滤可直接访问 -->
    <mvc:resources mapping="/MP_verify_**" location="/WEB-INF/check/" /><!-- 微信js工具调用验证 -->
    <mvc:resources mapping="/statics/**" location="/statics/" />
    <mvc:resources mapping="/error/**" location="/WEB-INF/pages/error/" />

    <!-- 只根据路径解析视图,采用UrlBasedViewResolver -->
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html" />
        <property name="viewClass" value="com.zhaogang.webapp.view.HtmlView"/>
    </bean>

    <!-- 将@ResponseBody返回的数据编码为UTF-8防止乱码 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
            <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="prettyPrint" value="true" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 统一公共异常处理 -->
    <bean id="exceptionHandler" class="com.zhaogang.webapp.handler.UnCatchExceptionHandler"/>

</beans>

applicationContext.xml

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 统一配置文件读入 -->
    <context:property-placeholder location="classpath*:runtime.properties" ignore-unresolvable="true" />

    <!-- redis缓存 -->
    <import resource="classpath*:applicationContext-redis.xml"/>

    <!-- 业务配置 -->
    <import resource="classpath*:applicationContext-biz.xml"/>

</beans>

applicationContext-biz.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" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- Activates scanning of @Autowired -->
	<context:annotation-config />

	<!-- Activates scanning of @Service -->
	<context:component-scan base-package="com.zhaogang" />

	<!-- 切面配置 -->
	<aop:aspectj-autoproxy />

	<!-- 事务管理类 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<!-- 事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"
						  proxy-target-class="true"/>

	<!-- mybatis 自动扫描Mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.zhaogang.mapper"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>

	<!-- mybatis 配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="typeAliasesPackage"
				  value="com.zhaogang.mapper"/>
		<property name="plugins">
			<list>
				<ref bean="mybatisPaginatorInterceptor"/>
			</list>
		</property>
		<property name="mapperLocations" value="classpath*:com/zhaogang/mapper/**/*Mapper.xml"/>
	</bean>

	<!-- mybatis分页插件 -->
	<bean name="mybatisPaginatorInterceptor" class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
		<property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"></property>
	</bean>


	<!-- 数据源配置 start-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>${jdbc.driverClassName}</value>
		</property>
		<property name="url">
			<value>${jdbc.url}</value>
		</property>
		<property name="username">
			<value>${jdbc.username}</value>
		</property>
		<property name="password">
			<value>${jdbc.password}</value>
		</property>
		<property name="initialSize">
			<value>${jdbc.initialSize}</value>
		</property>
		<property name="maxActive">
			<value>${jdbc.maxActive}</value>
		</property>
		<property name="testOnBorrow" value="true"/>
		<property name="testWhileIdle" value="true"/>
		<property name="validationQuery" value="select 1"/>
		<property name="timeBetweenEvictionRunsMillis" value="1200000"/>
		<property name="minEvictableIdleTimeMillis" value="3600000"/>
		<property name="numTestsPerEvictionRun" value="300000"/>
	</bean>
	<!-- 数据源配置 end-->

</beans>

注意:

*mapper.java 到*mapper.xml(IDEA中)

需要添加插件:Free Mybatis plugin

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏不想当开发的产品不是好测试

maven仓库地址配置

maven中央存库在国外,访问缓慢,一般国内镜像,这里推荐阿里云的 http://maven.aliyun.com/nexus/content/groups/p...

25610
来自专栏python3

python xml模块

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀...

12610
来自专栏JavaEdge

AJAX全接触实战1 概念介绍

服务器响应结果 onreadystatechange 事件 当请求被发送到服务器时,我们需要执行一些基于响应的任务。 每当 readyState 改...

8830
来自专栏pydata

use python in windows

此网站上面有编译好了的各种python windows包. 我们首先下载libxml2-python 然后使用pip wheel安装:

13040
来自专栏码云1024

Android Studio 无法预览xml布局视图的解决办法

8320
来自专栏前端说吧

JS-时间处理函数封装汇总 (更新中...)

最近听说thymeleaf好像也挺流行的,还说是spring官方推荐使用,那thymeleaf究竟是什么呢?spring为什么推荐用它呢?怎么用呢?本文将为你揭...

13430
来自专栏JavaEE

Thymeleaf的使用前言:一、thymeleaf简介:二、thymeleaf标准方言:三、thymeleaf与springboot集成案例:总结:

最近听说thymeleaf好像也挺流行的,还说是spring官方推荐使用,那thymeleaf究竟是什么呢?spring为什么推荐用它呢?怎么用呢?本文将为你揭...

15420
来自专栏pydata

java 开发常用错误集锦

1.org.apache.catalina.deply.WebXml addServer错误: Maven工程中用到的tomcat的catalina.jar,...

11520
来自专栏JavaEE

spring和springboot中加密连接数据库的信息前言:一、spring中加密连接数据库的信息:二、springboot项目中加密数据库连接信息:总结:

在实际开发中,一些关键的信息肯定是要加密的,否则就太不安全了。比如连接数据库的用户名和密码,一般就需要加密。接下来就看看spring项目和spring boot...

31530
来自专栏西枫里博客

微信支付,官方demo报错的两处解决办法

一、报错信息:Fatal error: Call to undefined function libxml_disable_entity_loader() in...

15740

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励