Spring、SpringMVC、Mybaits搭建ssm框架

本人于2016年毕业,小白一枚,经验不足。各位看官如有问题,请百度。小白我也是一步一步百度而来。小白QQ群571669420,静请打扰;
一、准备工具和环境
(1)PC一台(系统不限,只要能运行Java环境即可)。
(2)eclisp(版本看个人喜好)。
(3)jar包若干(具体哪些jar包,我也不知道,反正本人将spring所有jar包全部导入)。
二、环境搭建
(1)eclisp新建web功能,命名看个人喜好。以下为本人创建工程目录结构,仅供参考;

(2)导入jar包。
(3)在web-inf目录下的lib目录下导入jar包。
(4)同样在web-inf下的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>Matton</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>
.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml </param-value>
</context-param>

<welcome-file-list>
<welcome-file>/jsp/Reception/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ExcelServlet</display-name>
<servlet-name>ExcelServlet</servlet-name>
<servlet-class>com.matton.tools.ExcelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExcelServlet</servlet-name>
<url-pattern>/ExcelServlet</url-pattern>
</servlet-mapping>
</web-app>

(5)在项目工程下添加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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

    <!-- 1)加载JDBC;配置文件 -->
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties" />  
    </bean>  
    
    <!-- 2)定义数据源 -->
    <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>  

    <!-- 3)定义sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="configLocation" value="classpath:mybatis-config.xml" />  
    </bean>
    
    <!-- DAO接口所在包名,Spring会自动查找其下的类   @controller -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.matton.mapper" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>  
    <!-- 扫描Service -->
    <context:component-scan base-package="com.matton.service"></context:component-scan>

</beans>

(6)添加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-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- @Controller扫描 -->
    <context:component-scan base-package="com.matton.Controller"></context:component-scan>

    <!-- 默认的注解映射的支持 -->
    <!-- <mvc:annotation-driven /> -->
    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
<property name="messageConverters">  
     <list>  
         <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  
            <property name = "supportedMediaTypes">  
                 <list>  
                     <value>text/plain;charset=UTF-8</value>  
                 </list>  
            </property>  
         </bean>  
     </list>  
</property>  

</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <!-- 视图解析组件 -->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 上传配置 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="maxUploadSize" value="2097152"></property> 
        <property name="defaultEncoding" value="UTF-8" />  
    </bean> 

</beans>

(7)添加mybaits配置文件,配置文件如下:
<?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>
<properties resource="jdbc.properties"></properties>

    <!—为表结构及实体床架别名  -->
<typeAliases>
    <!-- 用户表  -->
     <typeAlias type="com.matton.model.LoginUserModel"  alias="LoginUser"/>                   
</typeAliases>
    <!—映射xml配置,与数据库交互  -->

<mappers>
<mapper resource="com/matton/mapper/LoginUserMapper.xml"/>
</mappers>

</configuration>

(8)配置数据库连接,配置如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/matton?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
username=root
password=admin
initialSize=0

\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570

maxActive=20

\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2

maxIdle=20

\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2

minIdle=1

\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4

maxWait=60000

(9)配置log4j,配置如下:

log4j.rootLogger=debug, console,file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%5p [%t] - %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=E:/logs/log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdou

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n
og4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

(10)配置结束。

推荐阅读更多精彩内容