SSM环境搭建

系统框架设计

  

SSM框架搭建

  

 导入jar文件

  

 配置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" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
    
    <!-- 描述性的文字说明,没实际意义 -->
    <display-name>springMVC</display-name>
    
    <!-- 欢迎页列表 -->
    <welcome-file-list>
        <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
    </welcome-file-list>
    
    <!-- log4j config path -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <!-- webapp root path -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>SMBMS.root</param-value>
    </context-param>
    <!-- Spring相关的配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>
    
    <!-- 监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- 初始化Spring容器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 字符编码过滤器 -->
    <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>
    
    <!-- 配置Servlet -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

 配置文件(/resources)

  database.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root
initialSize=5
minIdle=45
maxIdle=50
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true

  log4j.properties

log4j.rootLogger=debug,CONSOLE,file

log4j.logger.cn.smbms=debug
log4j.logger.org.apache.ibatis=debug
log4j.logger.org.mybatis.spring=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=debug
log4j.appender.CONSOLE.DatePattern=yyyy-MM-dd
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=${SMBMS.root}/logs/log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

log4j.logger.com.opensymphony.xwork2=debug

  mybatis-config.xml 

<?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="lazyLoadingEnabled" value="false"/>
    </settings>

    <!-- 实体类取别名 -->
    <typeAliases>
        <package name="cn.smbms.pojo"/>
    </typeAliases>

</configuration>

  applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 扫描到有@Component @Service等这些注解的类,则把这些类注册为bean -->
    <context:component-scan base-package="cn.smbms.dao"/>
    <context:component-scan base-package="cn.smbms.service"/>
    
    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:database.properties"/>
    
    <!-- JNDI获取数据源(使用dbcp连接池) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
        <property name="initialSize" value="${initialSize}"/>
        <property name="maxActive" value="${maxActive}"/>
        <property name="maxIdle" value="${maxIdle}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxWait" value="${maxWait}"/>
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
        <property name="removeAbandoned" value="${removeAbandoned}"/>
        <!-- sql心跳    保证连接池中的连接是真是有效的 -->
        <!-- 开启Evict的定时校验,循环校验 -->
        <property name="testWhileIdle" value="true"/>
        <!-- 定义Evict的时间间隔,单位:毫秒 -->
        <property name="timeBetweenEvictionRunsMills" value="60000"/>
        <!-- 在进行borrowObject处理时,会对拿到的连接进行校验-false -->
        <property name="testOnBorrow" value="false"/>
        <!-- 在进行ruturnObject处理时,会对返回的连接进行校验-false -->
        <property name="testOnReturn" value="false"/>
        <!-- 校验使用的sql语句,validatetionQuery,复杂的校验sql会影响性能 -->
        <property name="validationQuery" value="select 1"/>
        <!-- 配置每次校验连接的数量,一般等于maxActive -->
        <property name="numTestsPerEvictionRun" value="maxActive"/>
    </bean>
       
    <!-- 配置 SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    
    <!-- sql 映射 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.smbms.dao"/>
    </bean>
    
    <!-- 注解实现AOP -->
    <aop:aspectj-autoproxy/>
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 事务增强 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
           <tx:method name="smbms*"  propagation="REQUIRED" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 -->
    <aop:config  proxy-target-class="true">
        <aop:pointcut id="transService" expression="execution(* *cn.smbms.service..*(..))"/>
        <aop:advisor pointcut-ref="transService" advice-ref="txAdvice"/>
    </aop:config>
    
</beans>

  springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- 控制器的扫描 -->
    <context:component-scan base-package="cn.smbms.controller"/>
    
    <!-- 消息转换器 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteDateUseDateFormat</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <!-- 静态资源文件的引入 -->
    <mvc:resources location="/statics/" mapping="/statics/**"/>
    
    <!-- 拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/sys/**"/>
            <bean class="cn.smbms.interceptor.SysInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    <!-- 多视图解析器 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="favorParameter" value="true"/>
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html;charset=UTF-8"/>
                <entry key="json" value="application/json;charset=UTF-8"/>
                <entry key="xml" value="application/xml;charset=UTF-8"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
    </bean>
    
    <!-- 文件上传解析 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="500000"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

</beans>

 创建自定义拦截器

  

  

Bootstrap

  

  

原文地址:https://www.cnblogs.com/xhddbky/p/9486949.html