SSM框架的基本配置

1.dispatch-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.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 "
>

    <!--SpringMVC的配置文件:只是控制网站跳转逻辑-->

    <!--
    use-default-filters="false"  禁用掉默认的过滤行为
    -->
    <context:component-scan base-package="cn.beacon" use-default-filters="false">
        <!--配置SpringMVC注解扫描 只扫描控制器就可以了-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--解决引用静态资源的问题-->
    <mvc:default-servlet-handler/>
    <!--解决一些问题-->
    <mvc:annotation-driven/>

    <!--解决前端ajax访问跨域问题-->
    <mvc:cors>
        <mvc:mapping path="/**" />
    </mvc:cors>

    <!--文件上传 配置multipartResolver-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <!--配置上传文件的最大大小 16MB = 1638400 = 16*1024*100 (1024==1kb)-->
        <property name="maxUploadSize" value="1638400"/>
    </bean>

</beans>

2.applicationContext.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.xsd">

    <!--1.Spring的配置文件:希望管理所有的业务逻辑组件-->
    <context:component-scan base-package="cn.beacon">
        <!--过掉Controller注解:也就是除了Controller注解标识的类,其余注解标识的了Spring都希望拿到来处理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--Spring来控制业务逻辑,包括:数据源(用的是c3p0.jar),事物控制,aop等等-->

    <!--2.引入数据库的配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置C3P0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 指定连接数据库的用户名-->
        <property name="user" value="${jdbc.user}"/>
        <!-- 指定连接数据库的密码-->
        <property name="password" value="${jdbc.password}"/>
        <!-- 指定连接数据库的驱动-->
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <!-- 指定连接数据库的URL-->
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <!-- 指定连接池中保留的最小连接数-->
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
        <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
        <!-- 指定连接池中保留的最大连接数. Default:15-->
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
        <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
        <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
        <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>

        <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->
        <!--<property name="maxStatements" value="${jdbc.maxStatements}"/>-->
        <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
        <!--<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>-->

        <!--连接池在获得新连接失败时重试的次数,如果小于等于0则无限重试直至连接获得成功。default : 30-->
        <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"/>
        <!--连接池在获得新连接时的间隔时间。default : 1000 单位ms -->
        <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"/>
    </bean>

    <!--3.配置事务管理:配置Spring事物管理器:使用事物管理器来控制数据库事物-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--transactionManager控制住指定的数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--启动基于注解的事物-->
    <!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
    <!--启动基于Xml配置形式的事物-->
    <aop:config>
        <!--切入点表达式:指定控制cn.beacon.service包下的所有类的事物-->
        <aop:pointcut expression="execution(* cn.beacon.service..*(..))" id="txPoint"/>
        <!--配置事物增强-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    <!--配置事物增强 事物如何切入-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--切入点切入的所有方法都是事物方法-->
            <tx:method name="*"/>
            <!--以query命名开始的所有方法都是查询方法,只读的,可以调优,指定 read-only="true"-->
            <tx:method name="query*" read-only="true"/>
            <!--以find命名开始的所有方法都是查询方法,只读的,可以调优,指定 read-only="true"-->
            <tx:method name="find*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--
     4.整合Mybatis
     目的: 1.Spring管理所有组件。如调用mybatis中xxxMapper的实现类时,不再需要使用SqlSessionFactory.openSession()等等操作,
                                  只需要(再service层调用dao层时)使用@Autowired注解自动注入mapper即可;
            2.Spring用来管理事物
    -->
    <!--SqlSessionFactoryBean在Spring整合SpringMVC必要的mybatis-spring-1.3.0.jar里-->
    <!--这个配置 就是在Spring IOC一启动就创建SqlSessionFactory对象-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- configLocation指定全局配置文件的位置 -->
        <property name="configLocation" value="classpath:conf/mybatis.xml"/>
        <!--mapperLocations: 指定mapper文件的位置-->
        <!--扫描所有的mapper文件夹下所有Mapper文件-->
        <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
        <!--扫描指定的Mapper文件-->
        <!--<property name="mapperLocations" value="classpath:mapper/ApiInfoMapper.xml"/>-->
    </bean>

    <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
   base-package:指定mapper接口的包名
    -->
    <!--<mybatis-spring:scan base-package="com.beacon.dao"/>-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.beacon.dao"/>
    </bean>

</beans>

3.mybatis.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>
        <!--
        开启驼峰命名规则:如果数据库字段是A_NAME 而JavaBean中的字段是aName,这样使用 select * form TabName 也是可以将查
到的A_NAME值赋值给aName的。而不需要 这样写数据库语句 select A_NAME aName,password,sex form TabName才能将查到的A_NAME值赋值给aName的
        -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!--兼容Oracle(默认是mySql)-->
        <setting name="jdbcTypeForNull" value="NULL"/>

        <!--配置二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>


</configuration>

4.db.properties文件

jdbc.user=root
jdbc.password=123456
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/msrc?useUnicode=true&characterEncoding=utf-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.minPoolSize = 10
jdbc.initialPoolSize = 20
jdbc.maxPoolSize = 60
jdbc.maxIdleTime = 100
jdbc.acquireIncrement = 5
#连接池在获得新连接失败时重试的次数,如果小于等于0则无限重试直至连接获得成功
jdbc.acquireRetryAttempts=30
#连接池在获得新连接时的间隔时间
jdbc.acquireRetryDelay=1000

5.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">
    <display-name>Archetype Created Web Application</display-name>

    <!--Spring配置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--SpringMvc配置-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param><!--用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数    -->
            <param-name>contextConfigLocation</param-name>  <!--参数名称-->
            <param-value>classpath:conf/dispatcher-servlet.xml</param-value> <!--参数值-->
        </init-param>
        <load-on-startup>1</load-on-startup><!--当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.-->
        <async-supported>true</async-supported><!--设置是否启用异步支持-->
    </servlet>
    <servlet-mapping><!--用来定义servlet所对应的URL-->
        <servlet-name>dispatcher</servlet-name> <!--指定servlet的名称-->
        <url-pattern>/</url-pattern>  <!--指定servlet所对应的URL-->
    </servlet-mapping>

    <!--配置启动页-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

猜你喜欢

转载自blog.csdn.net/qq_33429583/article/details/81805238
今日推荐