SSM框架快速搭建(三)

6、在resources.config.spring下创建文件applicationContext.xml

        applicationContext.xml内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       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/jdbc
       http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/context/spring-cache.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/aop/spring-util.xsd
       ">

    <!--引入jdbc配置文件-->
    <context:property-placeholder location="classpath:config/jdbc.properties"/>

    <!--dataSource 配置-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!--  指定数据库驱动-->
        <!--                    <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>-->
        <!--  指定连接数据库的URL-->
        <!--                    <property name="url"><value>jdbc:mysql://wonder:3306/j2ee</value></property>-->
        <!--  root为数据库的用户名-->
        <!--                    <property name="username"><value>root</value></property>-->
        <!--  pass为数据库密码-->
        <!--                    <property name="password"><value>pass</value></property>-->

        <property name="url" value="${jdbc.url}"/>
        <!--  root为数据库的用户名-->
        <property name="username" value="${jdbc.username}"/>
        <!--  pass为数据库密码-->
        <property name="password" value="${jdbc.password}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="20"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000"/>

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="false"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>

        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat"/>
    </bean>


    <!-- mybatis文件配置,扫描所有mapper文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"
          p:configLocation="classpath:config/mybatis/mybatis-config.xml"
          p:mapperLocations="classpath:config/mybatis/mapper/*.xml"/>

    <!-- spring与mybatis整合配置,扫描所有dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.aaa.ssm.dao"
          p:sqlSessionFactoryBeanName="sqlSessionFactory"/>

    <!-- 对dataSource 数据源进行事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource"/>


    <!-- 配置使Spring采用CGLIB代理 -->
<!--    <aop:aspectj-autoproxy proxy-target-class="true"/>-->
    <aop:aspectj-autoproxy  expose-proxy="true"    proxy-target-class="true"/>

    <!-- 启用对事务注解的支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- Cache配置 -->
    <!--    <cache:annotation-driven cache-manager="cacheManager" />-->
    <!--    <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />-->
    <!--    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />-->

</beans>

7、创建config下创建jdbc.properties文件完成jdbc的配置

        jdbc.properties内容:(写你自己的数据库用户名和密码

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/aaa?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

8、在config.mybatis下编写mybatis-config.xml文件(设置log打印日志)

        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="logImpl" value="log4j"/>
    </settings>
</configuration>

9、在resources.config.spring下创建dispatcher-servlet.xml配置文件(这个dispatcher-servlet.xml配置文件就是所谓的springMVC配置文件吧?)(在这儿特别指出,视频中只给出了扫描controller包,没有)

        dispatcher-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:context="http://www.springframework.org/schema/context"
       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.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.aaa.ssm.controller"/>
    <!--视频中没有添加扫描service包,会报错-->
    <context:component-scan base-package="com.aaa.ssm.service"/>
    <context:annotation-config/>

    <mvc:resources mapping="/css/*" location="/static/css/"></mvc:resources>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver2">
        <property name="suffix" value=".jsp"/>
        <property name="prefix" value="/views/"/>
    </bean>



<!--    &lt;!&ndash;开通静态资源的访问,即jsp或html文件&ndash;&gt;-->
<!--    <mvc:default-servlet-handler/>-->
<!--    &lt;!&ndash;让所有的Controller能够解析,不可缺少,<mvc:annotation-driven>里面那一整段,用来解决@ResponseBody传输字符串中文乱码问题&ndash;&gt;-->
<!--    <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>-->
<!--        </mvc:message-converters>-->
<!--    </mvc:annotation-driven>-->
<!--    &lt;!&ndash; 视图解析器/定位 &ndash;&gt;-->
<!--    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--        &lt;!&ndash; 配置前缀 &ndash;&gt;-->
<!--        <property name="prefix" value="/WEB-INF/jsp/"/>-->
<!--        &lt;!&ndash; 配置后缀 &ndash;&gt;-->
<!--        <property name="suffix" value=".jsp"/>-->
<!--    </bean>-->
<!--    &lt;!&ndash;文件上传  &ndash;&gt;-->
<!--    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
<!--        &lt;!&ndash; 设定默认编码 &ndash;&gt;-->
<!--&lt;!&ndash;        <property name="defaultEncoding" value="UTF-8"></property>&ndash;&gt;-->
<!--        &lt;!&ndash; 设定文件上传的最大值5MB,5*1024*1024 &ndash;&gt;-->
<!--&lt;!&ndash;        <property name="maxUploadSize" value="5242880"></property>&ndash;&gt;-->
<!--    </bean>-->


</beans>

10、在webapp.statics目录下创建静态资源文件夹css,js,images

11、在webapp.WEB-INF下创建web.xml(有的话删了重新写就可以)

        web.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--设置spring配置文件的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/spring/applicationContext.xml</param-value>
    </context-param>
    <!--配置spring listener-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--解决Post乱码的问题-->           <!--之前的配置已经很好的解决了乱码的问题,所以注释掉了-->
<!--    <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>-->
    
    <!--springmvc前端控制器配置-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>   <!--如果你没有别的路径下创建dispatcher-servlet.xml配置文件,直接是在WEB-INF下创建的,那么是不用初始化参数的-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:config/spring/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    
</web-app>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值