文章转自 http://blog.csdn.net/jimolangge123/article/details/49210363
首先说明一下Web.xml中配置项的执行过程: <context-param> ? <listenser> ?<filter> ? <servlet>
首先初始化Spring的bean工厂,通过加载beans.xml初始化,其次通过spring的listener来监听,再次当请求时拦截并编码为UTF-8,最后由DispatcherServlet处理并分发到controller上
一、 项目结构图如下:
此项目主要目的是在于集成,主要包括一下步骤:
|
二、具体的详细步骤如下:
1. Jar包的加入,包括:spring、MyBatis、log、spring-mybatis、oracle。具体的jar包如下图所示,并简要的说明每个jar的作用。
2. 需要在Web.xml文件中配置每个框架的入口。如下所示:
[java] view plain copy
- <?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_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>ssm</display-name>
- <!-- 初始化spring的配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:conf/beans.xml</param-value>
- </context-param>
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置springmvc的入口 -->
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:conf/spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!—springMVC配置结束 -->
- <!-- 拦截器,对每个请求处理字符编码为UTF-8 -->
- <filter>
- <filter-name>encodingFilter</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>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/</url-pattern>
- </filter-mapping>
- <!-- 拦截器结束-->
- </web-app>
首先说明一下Web.xml中配置项的执行过程: <context-param> ? <listenser> ?<filter> ? <servlet>
首先初始化Spring的bean工厂,通过加载beans.xml初始化,其次通过spring的listener来监听,再次当请求时拦截并编码为UTF-8,最后由DispatcherServlet处理并分发到controller上
3. springMVC的配置文件
[java] view plain copy
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- 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">
- <!-- 开启注解扫描 -->
- <mvc:annotation-driven/>
- <!-- annotation默认的方法映射适配器 ,下面两个就是 <mvc:annotation-driven/>-->
- <!-- <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->
- <!-- 指定扫描包 -->
- <context:component-scan base-package="com.sm.controller" />
- <!-- 配置视图处理 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/view/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="defaultEncoding" value="utf-8" />
- <property name="maxUploadSize" value="10485760000" />
- <property name="maxInMemorySize" value="40960" />
- </bean>
- </beans>
4. spring的配置文件
[java] view plain copy
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
- 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/util
- http://www.springframework.org/schema/util/spring-util.xsd">
- <context:annotation-config />
- <!-- 扫描service,dao -->
- <context:component-scan base-package="com.sm">
- <context:exclude-filter type="regex"
- expression="com.sm.controller" />
- </context:component-scan>
- <!-- ////////////////////////配置数据源///////////////////////// -->
- <!-- 把.properties文件引入xml文件中 -->
- <!-- 第一种方式,比较方便 -->
- <!-- <util:properties location="classpath*:conf/jdbc.properties" /> -->
- <!-- 第二中方式 -->
- <!-- <context:property-placeholder location="classpath*:conf/jdbc.properties"/> -->
- <!-- 第三种方式 ,稍复杂点 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations" value="classpath*:conf/jdbc.properties" />
- <!-- <property name="locations"> <list> <value>classpath*:conf/jdbc.properties</value>
- </list> </property> -->
- </bean>
- <!-- 将Mybatis的DataSource,sessionFactory以及Transaction受控于Spring容器 -->
- <!-- 数据源有多种:1,dbcp class="" 2,c3p0, class="" 3,alibaba的Druid(德鲁伊)数据源 class="com.alibaba.druid.pool.DruidDataSource"
- 4,spring class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- <!-- 初始化连接大小 -->
- <property name="initialSize" value="0" />
- <!-- 连接池最大使用连接数量 -->
- <property name="maxActive" value="20" />
- <!-- 连接池最大空闲 -->
- <property name="maxIdle" value="20" />
- <!-- 连接池最小空闲 -->
- <property name="minIdle" value="0" />
- <!-- 获取连接最大等待时间 -->
- <property name="maxWait" value="60000" />
- <property name="validationQuery" value="${validationQuery}" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <property name="testWhileIdle" value="true" />
- <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="25200000" />
- <!-- 打开removeAbandoned功能 -->
- <property name="removeAbandoned" value="true" />
- <!-- 1800秒,也就是30分钟 -->
- <property name="removeAbandonedTimeout" value="1800" />
- <!-- 关闭abanded连接时输出错误日志 -->
- <property name="logAbandoned" value="true" />
- <!-- 监控数据库 -->
- <property name="filters" value="mergeStat" />
- </bean>
- <!-- ////////////配置Mybatis/////////////////////////////////// -->
- <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!-- <property name="configLocation" value="classpath*:conf/sqlMapConfig.xml"
- /> -->
- <property name="mapperLocations">
- <list>
- <value>classpath*:conf/com/dao/mapper/*.map.xml</value>
- </list>
- </property>
- </bean>
- <!-- 配置扫描接口 -->
- <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> value指明接口所在的包中
- <property name="basePackage" value="com.sm.dao" /> <property name="sessionFactory"
- ref="sessionFactory" /> </bean> -->
- </beans>
5. properties文件
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=tiger
validationQuery=SELECT 1
6. model
[java] view plain copy
- package com.sm.controller;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.sm.model.User;
- import com.sm.service.UserService;
- @Controller
- @RequestMapping("/user")
- public class UserController {
- private UserService userService;
- @Resource
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
- @RequestMapping("/add")
- public String addUser(User user){
- userService.addUser(user);
- return "success";
- }
- @RequestMapping("/toAddUser")
- public String toAddUser(){
- return "userAdd";
- }
- }
7. service
[java] view plain copy
- package com.sm.service;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Service;
- import com.sm.dao.UserDao;
- import com.sm.model.User;
- @Service
- public class UserService {
- @Resource
- private UserDao userDao ;
- public void addUser(User user){
- userDao.add(user);
- }
- }
8. dao
[java] view plain copy
- package com.sm.dao;
- import javax.annotation.Resource;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.springframework.stereotype.Repository;
- import com.sm.model.User;
- @Repository
- public class UserDao {
- @Resource
- private SqlSessionFactory sessionFactory ;
- public void add(User user){
- System.out.println("添加user"+user.getId()+"成功");
- SqlSession session = sessionFactory.openSession();
- session.insert("add", user);
- }
- }
访问:http://localhost:8080/ssm/user/toAddUser
最重要的环境的配置
水平有限,请多多指教,共同学习共同成长!!转载注明出处!!!!
源码下载:点击打开链接