ssm基本配置文件

来源:互联网 发布:知乎 复制 编辑:程序博客网 时间:2024/06/14 11:31

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>ssm0908</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 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:config/springMVC-servlet.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>
  
  <!-- spring  通过ContextLoaderListener 监听器来创建 IOC容器         执行顺序:监听器->过滤器->servlet  -->
  <!-- 配置spring配置文件 路径 -->
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:config/applicationcontext.xml</param-value>
  </context-param>
  <!-- 应用程序启动则创建了IOC容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

</web-app>

springMVC配置

<?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:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 扫描控制器对象 -->
<context:component-scan base-package="com.etc.controller" />
<mvc:annotation-driven />
<!-- 静态资源访问 -->
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<!-- 视图分解 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

spring配置

<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
">
<!-- 只需扫描 业务逻辑层的类,因为控制层由springMVC配置文件扫描,,实体对象和Dao层数据访问接口由 MyBatis配置文件扫描. -->
<context:component-scan base-package="com.etc.service" />
    <mvc:annotation-driven/>

<!-- 加载数据库的属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"></property>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123123" />
<property name="initialSize" value="5" />
<property name="maxActive" value="50" />    <!-- 最大连接数 -->
<property name="maxIdle" value="10" />   <!-- 最大空闲连接数 -->
<property name="minIdle" value="5" />    <!--最小空闲连接数 -->
<property name="maxWait" value="120000" />  <!-- 最大空闲时间 : 2分钟  毫秒-->
</bean>
<!-- 配置MyBatis SqlSessionFactory SqlSession->数据访问接口 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载数据访问接口映射文件 -->
<property name="mapperLocations" value="classpath:com/etc/dao/*.xml" />
<!-- 加载MyBatis的主配置文件 -->
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<!-- 加载实体类的别名 <property name="typeAliases" value="classpath:com.etc.entity"/> -->
</bean>
<!-- 扫描加载所有的数据访问接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="basePackage" value="com.etc.dao" />
</bean>

<!-- 事务配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans> 

mybatis配置

<?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>
<!-- 给实体对象取另名 -->
<typeAliases>
<package name="com.etc.entity"/>
</typeAliases>
</configuration>

原创粉丝点击