SSM整合方式之一

来源:互联网 发布:windows管道命令 编辑:程序博客网 时间:2024/05/24 05:52

首先在web.xml中配置Spring MVC的前端控制器,以及spring对于ORM/DAO的支持

<?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" 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>springmvcch09</display-name>
  <!-- spring 核心转换器 -->
  <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 让spring 核心转换器启动顺序 -->
  <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 拦截什么路径 -->
  <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- springJDBC 配置 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:bean.xml</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 字符编码拦截器 -->
  <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>forceRequestEncoding</param-name>
  <param-value>true</param-value>
  </init-param>
  <init-param>
  <param-name>forceResponseEncoding</param-name>
  <param-value>true</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

注意:如果在前端控制器不配置

    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>

(这里param-value的名字xxx.xml可以随便取)的时候,需要在WEB-INF中添加spring MVC的配置文件,并且名字必须为springMVC-servlet

在WEB-INF中配置springMVC-servlet,具体如下:

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 配置包扫描  base-package="com.zx.controller" 指定哪一个包-->
<context:component-scan base-package="com.zx.controller"></context:component-scan>

<!-- Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler/>

<!-- 开启注解模式驱动 -->
<!-- <mvc:annotation-driven conversion-service="conversionService"/> -->
<mvc:annotation-driven/>

<!-- 配置静态资源  Spring MVC就不会做拦截 -->
<mvc:resources location="/" mapping="*.png"/>
<mvc:resources location="/" mapping="*.jpg"/>
<mvc:resources location="/" mapping="*.css"/>
<mvc:resources location="/" mapping="*.js"/>
<mvc:resources location="/" mapping="*.ico"/>
<mvc:resources location="/" mapping="*.gif"/>

<!-- 日期转换工厂可选 -->
<!-- <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.converter.StringDate">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
</bean>
</list>
</property>
</bean> -->

<!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<list>
<bean class="com.converter2.StringDate2">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
</bean>
</list>
</property>
</bean> -->

<!-- 配置国际化资源    -->
<!-- <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
指定国际化资源的路径
<property name="basename" value="/WEB-INF/resources/messages"></property>
配置国际化资源字符编码格式
<property name="defaultEncoding" value="UTF-8"></property>
配置缓存   每过120秒刷新一次
<property name="cacheSeconds" value="120"></property>
</bean>
-->
<!-- 配置返回视图的统一解析的前后缀    WEB-INF/jsp/index.jsp-->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

在src目录底下配置bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<!-- 开启注解模式 -->
<context:annotation-config></context:annotation-config>

<!-- 开启包扫描 -->
<context:component-scan base-package="com.zx.service.impl"></context:component-scan>
<context:component-scan base-package="com.zx.dao.impl"></context:component-scan>


<!-- 读取数据库配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${usernamex}"></property>
<property name="password" value="${passwordx}"></property>
</bean>

<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 开启注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 任务调度处理类工厂 -->
<!-- <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.timer4.QuartzTimer"></property>
</bean>
表达式触发器工厂
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="cronExpression" value="0/5 13/1 0-23 * * ? *"></property>
</bean>
任务调度计划工厂
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean> -->

<!-- 配置sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 给 com.zx.enity  这个包下面的所有的类取别名    别名就是类的简单名称    com.zx.enity.User  User   -->
<property name="typeAliasesPackage" value="com.zx.enity"></property>
<!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com/zx/dao/*.xml"></property> 
</bean>

<!--扫描mybatis的sql配置文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zx.dao"></property>
</bean>
</beans>

相应的在src目录地下配置对应的数据库的配置文件信息jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
usernamex=root
passwordx=root

原创粉丝点击