SSM框架基础配置文件
Posted czar1996
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架基础配置文件相关的知识,希望对你有一定的参考价值。
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 u65e5u5fd7u5728u63a7u5236u8f93u51fau914du7f6e ###################################################################################### 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 u6587u4ef6u5927u5c0fu5230u8fbeu6307u5b9au5c3au5bf8u7684u65f6u5019u4ea7u751fu4e00u4e2au65b0u7684u6587u4ef6 ###################################################################################### #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 u6bcfu5929u4ea7u751fu4e00u4e2au65e5u5fd7u6587u4ef6uff0cu6587u4ef6u540du683cu5f0f: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 u65e5u5fd7 #log4j.logger.org.directwebremoting = ERROR #u663eu793aHibernateu5360u4f4du7b26u7ed1u5b9au503cu53cau8fd4u56deu503c #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>
以上是关于SSM框架基础配置文件的主要内容,如果未能解决你的问题,请参考以下文章