ssm框架配置要点

来源:互联网 发布:编程含金量最高的 编辑:程序博客网 时间:2024/05/16 15:52

1.配置文件放在resource下,web.xml放在WEB-INF下

2.spring-mvc配置

<!-- 启用Spring基于annotation的DI, 使用户可以在Spring MVC中使用Spring的强大功能。 激活 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 --><context:annotation-config /><!-- DispatcherServlet上下文, 只管理@Controller类型的bean, 忽略其他型的bean, 如@Service --><context:component-scan base-package="com.fl.mvc"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 --><mvc:annotation-driven /><!-- 静态资源处理, css, js, imgs --><mvc:resources mapping="/resources/**" location="/resources/" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsps/" /><property name="suffix" value=".jsp" /></bean>

3.spring-mybatis配置

 <!-- 自动扫描 -->      <context:component-scan base-package="com.fl.mvc" />      <!-- 引入配置文件 -->      <bean id="propertyConfigurer"          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location" value="classpath:jdbc.properties" />      </bean>        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"          destroy-method="close">          <property name="driverClassName" value="${driver}" />          <property name="url" value="${url}" />          <property name="username" value="${username}" />          <property name="password" value="${password}" />          <!-- 初始化连接大小 -->          <property name="initialSize" value="${initialSize}"></property>          <!-- 连接池最大数量 -->          <property name="maxActive" value="${maxActive}"></property>          <!-- 连接池最大空闲 -->          <property name="maxIdle" value="${maxIdle}"></property>          <!-- 连接池最小空闲 -->          <property name="minIdle" value="${minIdle}"></property>          <!-- 获取连接最大等待时间 -->          <property name="maxWait" value="${maxWait}"></property>      </bean>        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">          <property name="dataSource" ref="dataSource" />          <!-- 自动扫描mapping.xml文件 -->          <property name="mapperLocations" value="classpath:com/fl/mvc/mapping/*Mapper.xml"></property>      </bean>        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >          <property name="basePackage" value="com.fl.mvc.dao" />          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>      </bean>        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->      <bean id="transactionManager"          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">          <property name="dataSource" ref="dataSource" />      </bean>  

4.web.xml配置

<!-- 加载Spring容器配置 -->     <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>     <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:spring-mybatis.xml</param-value>      </context-param>  
 <!-- DispatcherServlet, Spring MVC的核心 -->  <servlet>    <servlet-name>mvc-dispatcher</servlet-name>    <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml     -->    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring-mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>mvc-dispatcher</servlet-name>    <!-- mvc-dispatcher拦截所有的请求-->    <url-pattern>/</url-pattern>  </servlet-mapping>
5.mapper.xml中的namespace记住要改成对应的dao文件,<resultMap>中type属性要改成对应的实体类,如果方法传入的参数类型是实体类,parameterType也要改。



1 0
原创粉丝点击