一. 前言
学习了有一段时间了,总算是把后端三大框架ssm学完了,今天就来整合一下ssm。
二. 整合步骤
- 创建web工程
- 导入相应的jar包,特别注意:Spring整合Mybatis需要导入jar包(mybatis-spring-1.3.0.jar)
- 配置外部属性文件 db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8&useUnicode=true
user=root
password=123456
maxSize=10
initSize=5
- 编写Spring配置文件 applicationContext.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"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.nhkj">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--
配置数据源,导入外部属性文件
-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="maxPoolSize" value="${maxSize}"></property>
<property name="initialPoolSize" value="${initSize}"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven />
<!-- mybatis相关配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:Mybatis.xml"></property>
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注册sql映射文件 -->
<property name="mapperLocations" value="classpath:com/nhkj/dao/*.xml"></property>
<!-- 配置别名处理器 -->
<!-- <property name="typeAliasesPackage" value="com.wanbangee.entities"></property> -->
</bean>
<!-- 扫描所有的DAO接口中的@Mapper注解,并将DAO接口的代理实现类加入到SPring的IOC容器中 -->
<!-- <bean id="configure" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.nhkj.dao"></property>
</bean>
-->
<mybatis-spring:scan base-package="com.nhkj.dao"/>
</beans>
- 编写SpringMVC配置文件springmvc-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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-4.0.xsd">
<!-- 配置自動掃描的包 SpringMVC只掃描 Controller註解和ControllerAdvice註解-->
<context:component-scan base-package="com.nhkj" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- SpringMVC配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler />
<!--
mvc:annotation-driven : 是万能的注解驱动
conversion-service : 配置SpringMVC上下文中中的类型转换器
- 不配置:使用默认的
- 配置:默认的+配置的
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置国际化资源文件的bean -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
<!-- 配置本地化解析器 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
<!-- 本地化拦截器 -->
<!-- 配置拦截器 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<!-- 配置MultipartResolver 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 文件上传的最大字节数 1024*1024*5 -->
<property name="maxUploadSize" value="6000000"></property>
</bean>
</beans>
- 配置web.xml
<!-- Spring的配置 -->
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
浏览器只支持Post和get的方式,想要实现delete和put的方式,需要使用过滤器HiddenHttpMethodFilter-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
至此为止,ssm就整合完毕了,接下来我们就可以去舔砖java了。
小结
讲一下学习完ssm的感受吧,其实框架的知识点并不是很多,像Spring,虽然它做了很多事情,但真正学习Spring就三个东西(IOC,AOP,声明式事务);对于SpringMVC来说如果你能搞懂它的执行流程,基本上就学会了(具体的SpringMVC流程在之前的博客中有写到);Mybatis相对来说的话就简单很多了,用到的就是全局配置文件(配置一次就可以了),sql映射文件(每一个dao接口对应一个sql映射文件),还有就是动态sql。
现在写三层架构的代码真的方便了很多了(哈哈,真香!)。