SSM框架快速整合搭建(无maven版)

    Java开发中,最为常见的当属web端开发,其中SSM框架又是应用最为广泛的,因其轻量级、速度快、效率高的特点,在项目相对较小时颇受青睐。下面将对SSM框架的快速整合搭建进行通俗易懂的说明,尽量让大家掌握搭建的流程和基本的概念。

 一、SSM的基本概念

    所谓SSM,即spring MVC + spring +mybatis,标准的MVC设计模式,将整个系统划分为表现层、controller层、service层、DAO层四层,因此,在搭建框架时项目的目录结构最好要包含这些,方便管理和查看。当然了,spring MVC、spring、mybatis各司其职,在整个框架中有着不同的作用:

  • Spring是开源框架,是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,主要实现业务对象管理;

  • Spring MVC框架,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离,主要负责请求的转发和视图管理;

  • MyBatis 是一个基于Java的持久层框架,作为数据对象的持久化引擎;

    二、环境配置

    在搭建框架时,配置文件不仅包括各框架的配置文件,还有整合的配置文件,最好规范命名下面是我的命名,大家可以作为参考,主要包括:

  • db.properties:JDBC属性文件,主要配置数据库相关

  • log4j.properties:日志配置文件,主要配置日志的输出

  • spring-mybatis.xml:spring和mybatis整合配置文件

  • spring-mvc.xml:spring MVC配置文件

  • web.xml:框架整合的配置文件

    下面来一步步搭建整合框架:

    1、新建项目

    由于我们未加入maven进行管理,所以这里需要创建一个Dynamic Web Project,在弹出的页面中输入项目名称,其他保持不变,Next,这里可以添加一个resources文件夹(按自己喜好),用来存放一些配置文件,如图所示:

继续Next直到最后时,按图中进行勾选,则会自动生成web.xml文件:

完成后,项目初步创建,基本目录如下:

    

 2、完善项目目录

    这里我们需要完善项目目录来方便管理整个项目,在src和resources中添加包package,WEB-INF中添加文件夹Folder,并添加相关配置文件,如下所示:

其中:

  • src包中是controller层、service层、dao层等相关业务代码,util是存放一些工具类;

  • resources包是资源配置文件:mybatis包存放数据库表xml文件;spring包存放spring和mybatis整合配置文件、springMVC配置文件;

  • jsp文件夹存放jsp页面;

  • lib文件夹存放外部jar包;

因为习惯使然,项目目录也各不相同,不过基本的配置不能少。

    3、添加jar包

    这里我们使用的框架版本为:

  • spring-framework-4.3.6.RELEASE(包含springMVC包)

  • mybatis-3.2.2

    上面的spring框架可通过地址:http://repo.spring.io/release/org/springframework/spring/ 进行版本选择下载,一般认准以dist.zip结尾的。除了这些jar包外,还有些其他必要的jar包,如日志记录、mybatis和spring整合、数据库连接池、mysql连接(若使用mysql数据库的话)等jar包,这里我将搭建的基本jar包整理了下,大家可以访问以下地址下载,至于其他的,大家在使用中需要时添加:

https://pan.baidu.com/s/1GeT12vJmbb-n-UEsPM6qGw  ,密码:qaxr

    4、配置文件

    这里我们使用mysql数据库,其安装配置可参考:一分钟?搞定JavaWeb开发环境和工具配置

    4.1、配置db.properties文件:

#url
url:jdbc:mysql://localhost:3306/ssm
#mysql驱动
driverClassName:com.mysql.jdbc.Driver
#mysql连接用户名及密码
username:root
password:123
#定义初始连接数  
initialSize=0
#定义最大连接数  
maxActive=20
#定义最大空闲  
maxIdle=20
#定义最小空闲  
minIdle=1
#定义最长等待时间
maxWait=60000

    4.2、配置log4j.properties文件:

log4j.rootLogger=INFO,CONSOLE
#log4j.addivity.org.apache=false
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p  %x - %m%n
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=gbk
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

 日志输出配置可参考我的上一篇博客:https://www.imooc.com/article/49245

    4.3、配置spring-mybatis.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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
    ">
     
    <!-- 启用注解 -->
    <context:annotation-config />
    <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
    <context:component-scan base-package="com.yoki">
        <context:exclude-filter type="annotation" 
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
 
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
           <list>
              <value>classpath:db.properties</value>
           </list>
        </property>
    </bean>
    <!-- 阿里 druid数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="driverClassName" value="${driverClassName}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="initialSize" value="${initialSize}" />
        <property name="maxWait" value="${maxWait}" />
        <property name="minIdle" value="${minIdle}" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false"
                    rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" read-only="false"
                    rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false"
                    rollback-for="java.lang.Exception" />
            <tx:method name="save*" propagation="REQUIRED" read-only="false"
                    rollback-for="java.lang.Exception" />
        </tx:attributes>
    </tx:advice>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- 事物处理 -->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.yoki.service..*(..))" />
        <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
    </aop:config>
    <!-- 配置mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> -->
        <property name="mapperLocations" value="classpath:mybatis/*Mapper.xml"></property>
    </bean>
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>
</beans>

    上面是spring和mybatis整合的配置,主要包含注解的启动、组件扫描、数据库连接池(这里使用了阿里的druid,也可替换成其他的)、事务处理、mybatis配置等,其中部分内容需要根据自己项目的实际情况来配置。

     4.4、配置spring-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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
   ">
    
   <!-- 启用注解驱动 -->
   <mvc:annotation-driven />
   <mvc:default-servlet-handler />
   <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
   <context:component-scan base-package="com.yoki.controller" />
   <!-- 静态资源处理  如css、js、image等-->
   <!-- <mvc:resources mapping="/resources/**" location="/,/resources/" /> -->
   <!-- 配置SpringMVC的视图解析器 -->
   <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/" />
       <property name="suffix" value=".jsp" />
   </bean>
</beans>

    上面是springMVC的配置,主要包含自动扫描控制器、视图模式、注解的启动,当然可以根据自己需要添加其他的配置,如文件上传的参数配置。

    4.5、配置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" 
    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>SSM</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-mybatis.xml</param-value>
    </context-param>
    <!-- log4j的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <!-- 编码过滤器配置 -->
    <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>
    <!-- LOG4J监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 配置SpringMVC核心控制器 -->
    <servlet>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--为DispatcherServlet建立映射 -->
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>     
</web-app>

    上面是web.xml的基本配置,开发中有需要再添加,如监听器、其他的servlet等。

    好了,到此SSM框架的搭建基本完成了,其实没有想象的那么难,有些东西并不需要死记硬背,理解就好了。后面就剩下测试了,这个就需要数据库了,后面再说吧。

我有一个微信公众号,经常会分享一些Java技术相关的干货;如果你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注。

猜你喜欢

转载自blog.csdn.net/sd09044901guic/article/details/81567564