专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 SSM框架配置文件详解

SSM框架配置文件详解

更新时间:2022-05-18 09:55:21 来源:赢咖4 浏览1331次

学习框架的时候,一开始最烦的就是一些配置文件,有很多东西需要配置。

Web.xml 文件

<?xml 版本="1.0" 编码="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_3_1.xsd"
         版本="3.1">
    <!-- 配置加载Spring文件的监听器 -->
    <上下文参数>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <听众>
        <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class >
    </listener>
    <!-- 编码过滤器-->
    <过滤器>
        <filter-name>编码</filter-name>
        <filter- class >org.springframework.web.filter.CharacterEncodingFilter</filter- class >
        <初始化参数>
            <param-name>编码</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </过滤器>
    <过滤器映射>
        <filter-name>编码</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <!-- 配置Spring MVC前端核心控制器-->
    <小服务程序>
        <servlet 名称>crm</servlet 名称>
        <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
        <初始化参数>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <!-- 配置服务器启动后立即加载Spring MVC配置文件-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet 映射>
        <servlet 名称>crm</servlet 名称>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>    
    <欢迎文件列表>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Springmvc的配置文件springmvc-config.xml

<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:context ="http://www.springframework.org/schema/context" 
       xmlns:aop ="http://www.springframework.org/schema/aop" 
       xmlns:tx ="http://www.springframework.org/schema/tx" 
       xsi:schemaLocation ="http://www.springframework.org/schema/beans
    网址:www.springframework.org/schema/beans/spring-beans-4.3.xsd 
    http:www.springframework.org/schema/mvc 
    http:www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
    http:www.springframework.org/schema/context 
    http:www.springframework.org/schema/context/spring-context-4.3.xsd 
    http:www.springframework.org/schema/aop 
    http:www.springframework.org/schema/aop/spring-aop-4.3.xsd 
    http:www.springframework.org/schema/tx 
    http:www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 加载属性文件 -->
    <context:property-placeholder location="classpath:resource.properties"/>
    <!-- 配置扫描仪-->
    <context:component-scan base- package ="com.ma.core.web.controller"/>
    <!-- 注解驱动:配置处理器映射器和适配器 -->
    <mvc:annotation-driven/>
    <!--配置静态资源的访问映射,此配置中的文件不会被前端控制器拦截-->
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/fonts/**" location="/fonts/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <!-- 配置视图解释器ViewResolver -->
    <bean id="jspViewResolver"类="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </豆>
    <!--配置拦截器-->
    <mvc:拦截器>
        <mvc:拦截器>
            <mvc:mapping path="/**"/>
            <bean class ="com.ma.core.interceptor.LoginInterceptor"/>
        </mvc:拦截器>
    </mvc:拦截器>
</beans>

ApplicationContext.xml 文件

<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:context ="http://www.springframework.org/schema/context" 
       xmlns:aop ="http://www.springframework.org/schema/aop" 
       xmlns:tx ="http://www.springframework.org/schema/tx" 
       xsi:schemaLocation ="http://www.springframework.org/schema/beans
        网址:www.springframework.org/schema/beans/spring-beans-4.3.xsd 
        http:www.springframework.org/schema/mvc 
        http:www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
        http:www.springframework.org/schema/context 
        http:www.springframework.org/schema/context/spring-context-4.3.xsd 
        http:www.springframework.org/schema/aop 
        http:www.springframework.org/schema/aop/spring-aop-4.3.xsd 
        http:www.springframework.org/schema/tx 
        http:www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!--读取 db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置数据源-->
    <bean id="dataSource"类="org.apache.commons.dbcp2.BasicDataSource">
        <!--数据库驱动器-->
        <property name="driverClassName" value="${jdbc.driver}"/>
        <!--连接数据库的URL-->
        <property name="url" value="${jdbc.url}"/>
        <!--连接数据库的用户名-->
        <property name="username" value="${jdbc.username}"/>
        <!--连接数据库的密码-->
        <property name="password" value="${jdbc.password}"/>
        <!--最大连接数-->
        <property name="maxTotal" value="${jdbc.maxTotal}"/>
        <!--最大空闲数-->
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <!--初始连接号-->
        <property name="initialSize" value="${jdbc.initialSize}"/>
    </豆>
    <!-- 事务管理器,依赖于数据源 -->
    <bean id="transactionManager"类="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </豆>
    <!--注意事项-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:属性>
            <!--通信行为-->
            <tx:method name="save*" 传播="REQUIRED"/>
            <tx:method name="插入*" 传播="REQUIRED"/>
            <tx:method name="add*" 传播="REQUIRED"/>
            <tx:method name="create*" 传播="REQUIRED"/>
            <tx:method name="delete*" 传播="REQUIRED"/>
            <tx:method name="update*" 传播="REQUIRED"/>
            <tx:method name="find*" 传播="SUPPORTS" 只读="true"/>
            <tx:method name="select*" 传播="SUPPORTS" 只读="true"/>
            <tx:method name="get*" 传播="SUPPORTS" 只读="true"/>
        </tx:属性>
    </tx: 建议>
    <!--切面条-->
    <aop:配置>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ma.core.service.*.*(..))"/>
    </aop:config>
    <!--配置MyBatis工厂SqlSessionFactory-->
    <bean id="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定mapper文件的位置-->
        <property name="mapperLocations" value="classpath:mybatis/sqlmap/*.xml"/>
        <!--指定MyBatis的核心配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </豆>
    <!-- 接口开发,扫描com.itheima.core.dao包,可以扫描到这个包里写的接口 -->
    <bean类="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ma.core.dao"/>
    </豆>
    <!-- 配置扫描@Service注解-->
    <context:component-scan base- package ="com.ma.core.service"/>
</beans>

Mybatis 配置文件 mybatis-config.xml

<?xml 版本="1.0" 编码="UTF-8" ?>
<!DOCTYPE 配置 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<配置>
    <!-- 别名定义-->
    <类型别名>
        <包名="com.ma.core.po"/>
    </typeAliases>
</配置>

Db.properties 文件

jdbc.driver= com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql:localhost:3306/test 
jdbc.username= root
jdbc.password = 
jdbc.maxTotal =30 
jdbc.maxIdle =10 
jdbc.initialSize =5

Log4j.properties 文件

# 全局日志配置
log4j.rootLogger =错误,标准输出
# MyBatis 日志配置...
log4j.logger.com.ma.core =调试
# 控制台输出...
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n

以上是SSM框架的基本配置文件。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>