如何快速搭建一个SSM框架

4 篇文章 0 订阅

SSM简介:

     Spring MVC是一个优秀的Web框架,MyBatis是一个ORM数据持久化框架,它们是两个独立的框架,之间没有直接的联系。但由于Spring框架提供了Ioc和AOP等相当实用的功能,若把Spring MVC 和 MyBatis的对象交给Spring容器进行解耦合管理,不仅能大大增强系统的灵活性、便于功能扩展,还能通过Spring提供的服务简化编码、减少开发的工作量、提高开发效率。SSM框架整合就是分别实现Spring与Spring MVC 、Spring与MyBatis的整合,而实现整合的主要工作就是把Spring MVC 、MyBatis中的对象配置到Spring容器中,交给Spring来管理。接下来我们通过例子来实现SSM框架搭

 

 

架构设计:

  1. 数据存储:采用MySQL数据库进行数据存储。
  2. ORM: 采用MyBatis框架,实现数据的持久化操作。
  3. Spring Core: 基于IoC和AOP的处理方式,统一管理所有的JavaBean。
  4. WEB框架:采用Spring MVC进行Web请求的接收与处理。
  5. 前端框架:以JSP为页面载体,使用jQuery框架以及HTML5、CSS3实现页面的展示与交互。

框架整合:

    • 新建Web Project并导入相关的jar文件

SSM框加所需jar文件如下图:

lib

二、配置web.xml,在web.xml中配置Spring MVC的核心控制器DispatcherServlet、字符编码过滤器、以及指定的Spring配置文件所在的位置并配置ContextLoaderListener等,具体代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- 配置ContextLoaderListener 在启动Web容器时,自动装配Spring applicationContext.xml的配置信息 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- 配置dispatcherServlet 前端控制器 -->
	<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:springcontext-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 配置字符编码过滤器 -->
	<filter>
		<filter-name>encoding</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>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

三.配置文件(/resources):

(1).applicationContext-mybatis.xml的配置,该文件是Spring的配置文件,该文件的主要配置信息有数据源对象、事务管理,以及MyBatis的配置信息等。 相关配置如下:

<?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:aop="http://www.springframework.org/schema/aop"
	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.xsd
         http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.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">
	<context:component-scan base-package="cn.smbms.service"/> 
    <context:component-scan base-package="cn.smbms.dao"/>  
	<!-- 读取数据库配置文件 -->
	<context:property-placeholder location="classpath:database.properties" />

	<!-- 获取数据源(使用dbcp连接池) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
	destroy-method="close" scope="singleton">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${user}" />
		<property name="password" value="${password}" />

		<!-- 数据库连接池初始化连接时创建的连接个数配置 默认为0 -->
		<property name="initialSize" value="${initialSize}" />

		<!-- 连接池中同时连接的最大连接数 默认为8 表示单机并发处理能力 -->
		<property name="maxActive" value="${maxActive}" />

		<!-- 连接池中可允许的最大空闲连接数,默认为8 超过设置的连接装被释放掉 设为负数表示不受限制 -->
		<property name="maxIdle" value="${maxIdle}" />

		<!-- 连接池中的最小空闲连接数,默认连接数为0 低于该数值的连接池将会创建新连接 -->
		<property name="minIdle" value="${minIdle}" />

		<property name="maxWait" value="${maxWait}" />
		<property name="removeAbandoned" value="${removeAbandoned}" />
		<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />

		<!-- sql心跳 -->
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="validationQuery" value="select 1" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="numTestsPerEvictionRun" value="${maxActive}" />

	</bean>
	<!-- 配置MyBatis的SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
    	<property name="configLocation" value="classpath:mybatis-config.xml"/>
	</bean>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- AOP事务处理 -->
	<aop:aspectj-autoproxy />
	<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(* *cn.smbms.service..*(..))"
			id="transService" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="transService" />
	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="smbsm*" propagation="REQUIRED"
				rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>

	

	<!-- 配置MyBatis的MapperScannerConfigurer Mapper接口扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.smbms.dao"></property>
	</bean>
</beans>

(2).springmvc-servlet.xml相关配置

      1.配置<mvc:annotation-driven/>标签(包括消息转换器配置)

      2.配置静态文件访问 <mvc:resources/>

      3.配置支持文件上传-------MultipartResolver

      4.配置多视图解析器-------ContentNegotiatingViewResolver

      5.配置拦截器-------------Interceptors

相关代码如下:

<?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">
        
	<context:component-scan base-package="cn.smbms.controller"/>
	<!-- 静态文件访问配置 -->
	<mvc:resources mapping="/statics/**" location="/statics/" />

	<mvc:annotation-driven>
		<!-- 装配消息转换器StringHttpMessageConverter 解新决json数据传递中文乱码的问题 -->
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>
			</bean>
			<bean
				class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=utf-8</value>
						<value>application/json</value>
					</list>
				</property>
				<property name="features">
					<list>
						<!-- 输出Date的日期转换器 -->
						<value>WriteDateUseDateFormat</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	<!-- 配置系统拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/sys/**"/>
			<bean class="cn.smbms.interceptor.SysInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors>
	<!-- 多视图解析器配置 -->
	<bean
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="favorParameter" value="true"></property>
		<property name="defaultContentType" value="text/html"></property>
		<property name="mediaTypes">
			<map>
				<entry key="html" value="text/html;charset=UTF-8"></entry>
				<entry key="json" value="application/json;charset=UTF-8"></entry>
				<entry key="xml" value="application/xml;charset=UTF-8"></entry>
			</map>
		</property>
		<property name="viewResolvers">
			<list>
				<bean
					class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="prefix" value="/WEB-INF/jsp/"></property>
					<property name="suffix" value=".jsp"></property>
				</bean>
			</list>
		</property>
	</bean>


	<!-- 配置MultipartResolver 用于上传文件 使用Spring的CommonsMultipartResolver -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="500000"></property>
		<property name="defaultEncoding" value="UTF-8"></property>
	</bean>
</beans>

(3).database.properties   数据库连接信息 代码如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root
minIdle=45
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true

(4).log4j-properties   log4j日志输出文件  从网上找到相关文件拷入即可,不是必须的

(5).mybatis-config.xml   mybatis的配置信息  配置typeAlicases  给实体取别名,代码如下:

<?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>
	<!-- 设置延迟加载 -->
	<settings>
		<setting name="lazyLoadingEnabled" value="false" />
	</settings>
	<!-- 给实体类取别名 -->
	<typeAliases>
		<package name="cn.smbms.pojo" />
	</typeAliases>
</configuration>

四、数据对象模型(cn.smbms.pojo) 所在的数据对象模型放置在此包下

五.DAO数据访问接口(cn.smbms.dao)所有数据操作全部在dao包下,在dao包下再按照功能模块划分规则进行分包命名

六.系统服务接口(cn.smbms.service)系统服务接口负责系统的业务逻辑处理,基于接口的编程方式,接口和接口实现类近照 功能模块放置在同一包下。

七.前端控制层Controller (cn.smbms.controller) 前端控制器全部在controller包下

八.系统工具类 (cn.smbms.tools) 放置系统所有的公共对象、资源以及工具类,例如:分页、常量等

九.前端页面(/WEB-INF/jsp)和静态资源文件(/WebRoot/statics)

 

通过上面九大步骤,SSM框架已经搭建完成,后续便可以进行各模块的功能开发了

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿松哥哥2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值