Spring的SSM标准配置

一、首先是web.xml文件的配置

<welcome-file-list>
    <!--设置默认显示登陆界面-->
    <welcome-file>login.jsp</welcome-file>
    <!--<welcome-file>index.jsp</welcome-file>-->
  </welcome-file-list>

  <!-- spring的ContextLoaderlistener -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Springmvc 的前端控制器 -->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 解决中文乱码问题 -->
  <filter>
    <filter-name>CharacterEncodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 处理put和delete请求 -->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

二、配置spring的配置文件beans.xml

<!--1 配置扫描包-->
    <context:component-scan base-package="包的路径如:com.baidu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>
    <!--2 加载jdbc配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--3 配置数据源信息 我这里使用的是阿里的druid数据库-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource">
        <property name="username" value="${jdbc.userName}" />
        <property name="password" value="${jdbc.password}" />
        <property name="url" value="${jdbc.jdbcUrl}" />
        <property name="driverClassName" value="${jdbc.driverClass}" />
    </bean>
    <!--4 配置数据源事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"/>
    </bean>
    <!--5 开启基于注解的事务-->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!--6 加载mybatis全局配置文件 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="druidDataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!--7 配置扫描Mapper接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="这里是包的路径如:com.ujiuye.*.mapper" />
    </bean>

三、配置springmvc的配置文件

   <!--1 配置扫描包-->
    <context:component-scan base-package="这里是包的路径如:com.ujiuye" use-default-filters="false 把默认使用过滤器关闭了,这样就可以只加载下面的两个包">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>
    <!--2 配置内部资源视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--3 处理静态资源文件的标签-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven />

    <!--文件上传的配置-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxInMemorySize" value="#{500*1024*1024}"/>
    </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="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <!--添加分页插件 分页是可以用到,当然不用的话也可以删掉-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="mysql"></property>
            <property name="reasonable" value="true"></property>
        </plugin>
    </plugins>
</configuration>

五、数据库的基本配置文件

#连接哪个数据库
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/crmpro?characterEncoding=utf-8
#数据库驱动,我这里使用的是MySQL jdbc.driverClass=com.mysql.jdbc.Driver
#用户名 jdbc.userName=root
#密码 jdbc.password=root

 

posted @ 2019-11-05 21:16  lb行走的程序猿  阅读(316)  评论(4编辑  收藏  举报