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

SSM项目中的配置文件

程序员文章站 2022-04-29 23:53:09
...

共分为五大配置文件:

1.mybatis-config.xml:

配置持久化类的别名,并指定SQL映射文件的位置
举例:

<!-- 类型别名设置 -->
<typeAliases>
	
	<typeAlias alias="Auser" type="com.po.Auser"></typeAlias>
	<typeAlias alias="Buser" type="com.po.Buser"></typeAlias>
	<typeAlias alias="GoodsType" type="com.po.GoodsType"></typeAlias>
	<typeAlias alias="Goods" type="com.po.Goods"></typeAlias>
	<typeAlias alias="Notice" type="com.po.Notice"></typeAlias>
	<typeAlias alias="Order" type="com.po.Order"></typeAlias>
</typeAliases> 

<!--映射文件的设置  -->
<mappers>
	<!-- 使用类路径引入 -->
	<mapper resource="com/mybatis/admin/AdminGoodsMapper.xml"/> 
	<mapper resource="com/mybatis/admin/AdminMapper.xml"/>
	<mapper resource="com/mybatis/admin/AdminNoticeMapper.xml"/> 
	<mapper resource="com/mybatis/admin/AdminOrderMapper.xml"/> 
	<mapper resource="com/mybatis/admin/AdminTypeMapper.xml"/> 
	<mapper resource="com/mybatis/admin/AdminUserMapper.xml"/> 
	<mapper resource="com/mybatis/before/CartMapper.xml"/>
	<mapper resource="com/mybatis/before/IndexMapper.xml"/>
	<mapper resource="com/mybatis/before/OrderMapper.xml"/>
	<mapper resource="com/mybatis/before/UserCenterMapper.xml"/>
	<mapper resource="com/mybatis/before/UserMapper.xml"/>

2.applicationContext.xml

配置了数据源,事务管理,Mybatis工厂,扫描包等内容
举例:

	<!--配置dbcp数据源  -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="122526"></property>
		<property name="maxTotal" value="30"></property>
		<property name="maxIdle" value="10"></property>
		<property name="initialSize" value="5"></property>
	</bean>
	<!--配置事务管理器  -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--开启事务注解 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	
	<!--配置Mybatis sqlsession工厂,同时指定数据源,并与Mybatis完美结合  -->
	 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	</bean>
	<!--配置Mapper扫描器  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.dao"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	 <context:component-scan base-package="com.service"></context:component-scan>	 	
</beans>

3.log4j.properties

Mybatis默认使用log4j输出日志信息,如果开发者需要查看控制台输出的SQL语句,需要配置日志文件
举例:

## Global logging configuration
#log4j.rootLogger=ERROR, stdout
## Console output...
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p - %m%n
#
#
#log4j.logger.com.zjm.dao=DEBUG

4.springmvc-servlet.xml

配置了控制层的包扫描,静态资源处理,视图解析器,文件上传,统一异常处理

     <!-- 使用扫描机制,扫描包 -->
    	<context:component-scan base-package="com.controller" />
    	<mvc:annotation-driven />
    	
        <!-- 静态资源需要单独处理,不需要dispatcher servlet -->  
        <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> 
        <mvc:resources location="/images/" mapping="/images/**"></mvc:resources> 
        
        <!-- 查看图片时,logos文件夹不需要dispatcher servlet --> 
        <mvc:resources location="/logos/" mapping="/logos/**"></mvc:resources>
        
    	<!-- 配置视图解析器 -->
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    		id="internalResourceViewResolver">
    		<!-- 前缀 -->
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<!-- 后缀 -->
    		<property name="suffix" value=".jsp" />
    	</bean>
    	
    	<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->  
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
            p:defaultEncoding="UTF-8"  
            p:maxUploadSize="5400000"  
            p:uploadTempDir="fileUpload/temp" 
         > 
         
          <!--D:\spring mvc workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\fileUpload  -->  
        </bean> 
        <!-- defaultEncoding="UTF-8" 是请求的编码格式,默认为iso-8859-1
    maxUploadSize="5400000" 是允许上传文件的最大值,单位为字节
    uploadTempDir="fileUpload/temp" 为上传文件的临时路径--> 
    	<!-- 托管MyExceptionHandler -->
    	<bean class="com.exception.MyExceptionHandler"/>

5.web.xml

配置了ApplicationContext容器,Springmvc的DispatcherServlet以及字符编码过滤器
举例:

  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>maven20</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <!--========================spring Ioc 的启动配置  =============================-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  <!--==============================springMVC 的启动配置============================== -->
    <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-config.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
   <!--===========================中文乱码过滤器配置  ====================================-->
  <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> 
</web-app>