优秀文章
博客:
shu_lin > SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
水滴娃娃 > 如何用Maven创建web项目(具体步骤)
说明
- 此篇文章对接我的上一篇文章“MAVEN建站”;
- 框架的整合在网上可以找到很多相关参考文档,本文也是经过多方参考加上本人亲身实践整理出来结果,可能在操作步骤与配置选择上与他人存在差异,若有读者对此存在疑问请自行取舍或者与我联系交流;
相关框架
- Spring
- Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。——百度百科(Spring)
- Spring是一个容器,它利用Java的反射机制实现对象的动态生产,并根据配置文件来建立容器内对象之间的依赖关系,这种控制权由对象转向容器的模式就是Spring的核心——IOC。
- Spring的另一个核心:面向切面编程(AOP);
- SpringMVC
- SpringMVC是一个基于DispatcherServlet的MVC框架,所以SpringMVC的核心配置就是在web.xml中配置拦截请求的Servlet,配置好之后匹配的请求到达后台就都会给DispatcherServlet拦截并转发给对应的处理器,这里所提到的处理器就是基于SpringMVC提供注解(@Controller)注释的Controller,处理器接收请求之后会根据请求匹配对应的处理方法,这里处理请求的实际操作方法也是由SpringMVC提供的注解(@RequestMapping)所注释好的,@RequestMapping会给所注释的方法设置映射值,若请求与注释上设置的值一致,则完成匹配并接受处理,最后返回处理结果,关于返回结果详情可以参考这里;
- MyBatis
- 半自动化的数据持久层框架;
项目整合
- 引入jar包,在项目的pom.xml文件添加对应jar包信息:
本例只列出SSM所需jar,内容也非完整pom内容,切勿直接copy使用
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<mybatis.version>3.3.1</mybatis.version>
<jackson.version>2.6.3</jackson.version>
<aspectj.version>1.8.8</aspectj.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring and mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- Other -->
<!-- aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- db -->
<!-- mysql
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
-->
<!-- oracle
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
-->
</dependencies>
- 创建配置文件来实现框架的搭建与整合(注意web.xml所填路径必须与框架配置文件所在位置保持一致):
- 修改web.xml文件实现框架的搭建:
以下内容非完整web.xml内容,切勿直接copy使用
- 修改web.xml文件实现框架的搭建:
<!-- spring-mybatis -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring.xml;
classpath:config/spring-mybatis.xml
</param-value>
</context-param>
<!-- spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器,未证实 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- spring mvc -->
<servlet>
<servlet-name>spring mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>spring mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- spring.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"
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">
<description>Spring Configuration</description>
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
<!-- 扫描指定包下的类,注册组件 -->
<context:component-scan base-package="com.*.**" use-default-filters="false">
<!-- 注册@Aspect注解的切面 -->
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
<!-- 注册@Service注解的服务 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
</beans>
- 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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/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">
<description>Spring MVC Configuration</description>
<!-- 代理,实现controller层的aop -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 扫描指定包下的类,注册组件 -->
<context:component-scan base-package="com.*.**" use-default-filters="false">
<!-- 注册@Controller注解的控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 启用注解驱动 -->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="mappingJacksonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</beans>
- 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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<description>Mybatis Configuration</description>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/database/jdbc.properties" />
<!-- 多个配置用下面的方法-->
<!--
<property name="locations">
<list>
<value>classpath:config/database/jdbc.properties</value>
</list>
</property> -->
</bean>
<!-- 数据源,这里的配置与jdbc.properties中的内容需匹配 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc_driverClassName}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<property name="maxActive" value="${jdbc_maxActive}"/>
<property name="maxIdle" value="${jdbc_maxIdle}"/>
<property name="testOnBorrow" value="${jdbc_testOnBorrow}"/>
</bean>
<!-- spring与Mybatis映射 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/**/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhaohua.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 事务 -->
<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="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:attributes>
</tx:advice>
<!-- 使用切面将事物逻辑织入方法 -->
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(public * com.*.*.service..*.*(..))" />
<aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice" />
</aop:config>
</beans>
- jdbc.properties内容如下(<<xx>>部分内容需根据实际情况替换):
jdbc_driverClassName=<<driver>>
validationQuery=SELECT 1
jdbc_url=<<url>>
jdbc_username=<<username>>
jdbc_password=<<password>>
jdbc_maxActive=150
jdbc_maxIdle=5
jdbc_testOnBorrow=true
最后
- 以上的所有配置,均可按照实际情况选择修改。