ssm框架创建流程

总结了一下搭建SSM框架流程,在以后用到的时候方便回头使用。

使用工具:MyEclipse 2015;Tomcat 8版本;jdk1.8版本。


首先:

1:创建一个WebProject项目,jdk1.8 Tomcat8 最后勾选web.xml配置文件。

这里写图片描述

这里写图片描述

然后: 
2.将相应的Jar包导入lib文件下。总共35个Jar包,将OJBDBC也导入进去。

这里写图片描述

3.配置web.xml文件。

配置2个内容。一个是Spring,一个是Spring MVC的配置。

Spring配置信息

  • 1:通过全局上下文参数来加载Spring配置文件

  • 2:配置监听器。

这里写图片描述

在web.xml中继续配置Spring MVC;

Spring MVC的配置信息。

  • 1:首先配置servlet。通过Servlet标签配置dispatchServlet。需要一个初始化参数 ,加载spring MVC配置文件。
  • 2:配置mapping。

这里写图片描述

然后还需要配置一下中文乱码解决问题。继续在web.xml中配置相关信息。 
这里写图片描述

然后,进行下一步。 
4:加入3个配置文件。Spring,Spring MVC,Mybatis 这三个配置文件需要加入。 
这里写图片描述 
将配置文件放在src根目录下即可。

Spring的扫描包:配置了事物。(applicationContext.xml);

  • 1:自动扫描:根据注解创建实例化,控制反转。(4种方式)
  • 2:引入配置文件。jdbc的驱动包等信息。
  • 3:配置数据源。需要的信息根据第二步中的jdbc中的配置文件来引用。
  • 4:配置MyBatis的SqlSessionFactory:有了它才可以使用MyBatis(1:数据源:第三步配置的数据源。2:自动扫描mappers.xml文件。所有的映射文件。放在一个对应的路径下。3:加载MyBatis的配置文件。)
  • 5:DAO层接口包。该包下的所有都会被实例化。
  • 6:配置事物管理:交由Spring来管理。(1:定义事物传播属性。)
  • 7:配置事物切面。
  • 8:异常处理相关。

  • 关于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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        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/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd  
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">    
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.sys.dao" />
    <context:component-scan base-package="com.sys.service" />
    <context:component-scan base-package="com.sys.entity"/>
    <!-- 引入配置文件,可以使用${}语法,location:指定读取文件的路径 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        p:driverClass="${jdbc.driverClassName}"
        p:jdbcUrl="${jdbc.url}"
        p:user="${jdbc.username}"
        p:password="${jdbc.password}"
        p:initialPoolSize="${jdbc.initialSize}"
        p:maxPoolSize="${jdbc.maxActive}"/>
    <!-- 配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mappers.xml文件 -->
        <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"></property>
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
    </bean>
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sys.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>
    <!-- 配置事务通知属性 -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定义事务传播属性 -->  
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置事务切面 -->  
    <aop:config>  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.sys.service.*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    </aop:config>  
    <!-- 异常统一处理 -->
    <!-- <bean id="exceptionResolver" class="com.sys.util.HandlerException"/> -->
</beans>

Spring MVC:(servlet-mvc.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        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/jee 
        http://www.springframework.org/schema/jee/spring-jee.xsd  
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">    

    <!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.sys.controller" />
    <!-- 添加数据转换的注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 上传组件 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传的编码格式 -->
        <property name="defaultEncoding" value="utf-8"/>
        <!-- 设置最大上传大小 -->
        <property name="maxUploadSize" value="5242880"/>
    </bean>
    <!-- 静态资源配置设置:除了控制器一概不管理 -->
    <mvc:default-servlet-handler/>
</beans>  

MyBatis 配置文件(mybatis-config.xml)放置在src目录下的mabatis文件夹内。

<?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.sys.entity"/>
    </typeAliases>
</configuration>

JDBC文件与Log4j配置文件。(日志文件,将错误信息保存在日志文件,前台不能显示错误文件,也可保存用户访问信息,以及数据库操作的信息)。

log4j.properties配置源码:

log4j.rootLogger=info,appender1,appender2

log4j.appender.appender1=org.apache.log4j.ConsoleAppender 

log4j.appender.appender2=org.apache.log4j.FileAppender 
log4j.appender.appender2.File=D:/logs/news/logFile.txt

log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout 

在jdbc配置文件中修改相关信息。(需要自己修改;#代表注释)。 
jdbc.properties配置源码

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver    //Oracle数据库
jdbc.url=jdbc:oracle:thin:@localhost:1521:jredu   //数据库名
jdbc.username=OnlineTest    //数据库表
jdbc.password=Jredu12345    //数据库密码

jdbc.initialSize=0  

jdbc.maxActive=20  

jdbc.maxIdle=20  

jdbc.minIdle=1  

jdbc.maxWait=60000  

至此,关于SSM框架搭建已经成功,在下一个博客之中,会实现一个SSM搭建框架,实现一个简单的登录功能。

原文地址:https://www.cnblogs.com/yr0215/p/8025567.html