欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SSM框架基础配置文件

程序员文章站 2022-05-19 10:32:12
SSM框架基础配置文件包括:applicationContext.xml(Spring框架核心配置文件)、dispatcher-servlet.xml(SpringMVC框架核心配置文件)、mybatis-config.xml(Mybatis配置文件)、database.properties(数据源 ......

ssm框架基础配置文件包括:applicationcontext.xml(spring框架核心配置文件)、dispatcher-servlet.xml(springmvc框架核心配置文件)、mybatis-config.xml(mybatis配置文件)、database.properties(数据源配置文件)、log4j.properties(日志打印文件)、web.xml文件。以下为每个文件具体的配置及说明:

applicationcontext.xml文件:

<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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 包浏览 -->
    <context:component-scan base-package="cn.xianqu"></context:component-scan>
    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:database.properties" />
    <!-- 创建数据源 -->
    <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"
        destroy-method="close">
        <property name="driverclassname" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 创建sqlsessionfactory -->
    <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
        <property name="datasource" ref="datasource"></property>
    <!-- 加载mybatis的核心配置文件 -->
        <property name="configlocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!-- 重要的方式mapperscannerconfiger:让spring自动扫描指定包下的mapper,生成mapper实例 -->
    <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
        <property name="basepackage" value="cn.xianqu.mapper"></property>
    </bean>
    <!-- 创建事务管理器 -->
    <bean id="transactionmanager"
        class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
        <property name="datasource" ref="datasource"></property>
    </bean>
    <!-- 让事务的注解生效 -->
    <tx:annotation-driven proxy-target-class="true" />
    <!-- 声明事务管理 -->
    <tx:advice id="myadvice" transaction-manager="transactionmanager">
        <tx:attributes>
            <tx:method name="query*" rollback-for="exception"></tx:method>
            <tx:method name="find*" rollback-for="exception"></tx:method>
            <tx:method name="get*" rollback-for="exception"></tx:method>
            <tx:method name="add*" rollback-for="exception"></tx:method>
            <tx:method name="insert*" rollback-for="exception"></tx:method>
            <tx:method name="del*" rollback-for="exception"></tx:method>
            <tx:method name="remove*" rollback-for="exception"></tx:method>
            <tx:method name="update*" rollback-for="exception"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!-- 织入增强,形成切面 -->
    <aop:config>
        <aop:pointcut expression="execution(public * cn.xianqu.service..*.*(..))"
            id="pointcut" />
        <aop:advisor advice-ref="myadvice" pointcut-ref="pointcut" />
    </aop:config>
</beans>

 

 database.properties文件:

jdbc.driver=oracle.jdbc.driver.oracledriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=
jdbc.password=

 

 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:p="http://www.springframework.org/schema/p" 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">
    <!-- 包浏览 -->
    <context:component-scan base-package="cn.xianqu.controller"></context:component-scan>
    <!-- 让springmvc的注解生效(设置消息解析器,解决转换成json的中文字符乱码问题) -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.stringhttpmessageconverter">
                <property name="supportedmediatypes">
                    <list>
                        <value>text/html;charset=utf-8</value>
                    <value>application/json;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 静态资源映射 -->
    <mvc:resources location="/statics/" mapping="/statics/**"></mvc:resources>
    <!-- 文件上传解析器 -->
    <bean id="multipartresolver"    class="org.springframework.web.multipart.commons.commonsmultipartresolver"></bean>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 全局异常 -->
    <bean class="org.springframework.web.servlet.handler.simplemappingexceptionresolver">
        <property name="exceptionmappings">
            <props>
                <prop key="java.lang.exception">err</prop>
            </props>
        </property>
    </bean>
</beans>

 

 mybatis-config.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "mybatis-3-config.dtd" >
<configuration>
<!-- 设置别名 -->
    <typealiases>
        <package name="cn.xianqu.entity" />
    </typealiases>
</configuration>

 

 log4j.properties文件:

log4j.rootlogger=debug,console,file
#log4j.rootlogger=error,rolling_file

log4j.logger.org.bill=error
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

######################################################################################
# console appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.console=org.apache.log4j.consoleappender
log4j.appender.threshold=debug
log4j.appender.console.target=system.err
log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern= - (%r ms) - [%p] %d %c - %m%n

######################################################################################
# rolling file  \u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6
######################################################################################
#log4j.appender.rolling_file=org.apache.log4j.rollingfileappender
#log4j.appender.rolling_file.threshold=info
#log4j.appender.rolling_file.file=${baojia.root}/logs/log.log
#log4j.appender.rolling_file.append=true
#log4j.appender.rolling_file.maxfilesize=5000kb
#log4j.appender.rolling_file.maxbackupindex=100
#log4j.appender.rolling_file.layout=org.apache.log4j.patternlayout
#log4j.appender.rolling_file.layout.conversionpattern=%d{yyyy-m-d hh:mm:ss}%x[%5p](%f:%l) %m%n

######################################################################################
# dailyrolling file  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.dailyrollingfileappender
log4j.appender.file.datepattern=yyyy-mm-dd
log4j.appender.file.file=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

#dwr \u65e5\u5fd7
#log4j.logger.org.directwebremoting = error

#\u663e\u793ahibernate\u5360\u4f4d\u7b26\u7ed1\u5b9a\u503c\u53ca\u8fd4\u56de\u503c
#log4j.logger.org.hibernate.type=debug,console 

#log4j.logger.org.springframework.transaction=debug
#log4j.logger.org.hibernate=debug
#log4j.logger.org.acegisecurity=debug
#log4j.logger.org.apache.myfaces=trace
#log4j.logger.org.quartz=debug

#log4j.logger.com.opensymphony=info  
#log4j.logger.org.apache.struts2=debug  
log4j.logger.com.opensymphony.xwork2=debug

 

 web.xml文件:

<!doctype web-app public
 "-//sun microsystems, inc.//dtd web application 2.3//en"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>archetype created web application</display-name>
    <!-- 加载spring的配置文件 -->
    <context-param>
        <param-name>contextconfiglocation</param-name>
        <param-value>classpath:applicationcontext*.xml</param-value>
    </context-param>
    <!--解决中文字符集乱码的问题 -->
    <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>
        <init-param>
            <param-name>forceencoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterencodingfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 添加监听器,自动读取spring的配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
    </listener>
    <!-- 配置springmvc -->
    <servlet>
        <servlet-name>dispatcherservlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
        <init-param>
            <param-name>contextconfiglocation</param-name>
            <param-value>classpath: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>