文章目录
前言
SSM框架是目前企业JavaWeb项目开发中使用最多的框架,本篇文章将以IntelliJ IDEA作为IDE环境搭建一个SSM框架。
一、什么是SSM框架
SSM框架就是:SpringMVC + Spring + MyBatis,这里不做详细介绍,有兴趣可自行浏览其它详细资料。
二、SSM框架环境搭建步骤
1.创建Maven工程
2.引入依赖
<dependencies>
<!-- Junit,单元测试使用 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- 数据库驱动,可更换成自己使用的数据库驱动 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<!--lombok插件,这个可以在实体类中使用注解省略getter和setter -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<!-- jsp,JavaWeb项目如果使用jsp页面,则为必要的依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- servlet,JavaWeb项目必要依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- SpringMVC依赖包,已包括核心包的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<!-- Spring-jdbc依赖包,整合MyBatis的必要依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<!-- aspectjweaver,AOP织入必要依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- MyBatis-Spring依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- log4j依赖,用于打印MyBatis的日志信息,可自行替换其它日志类工具 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 阿里的Druid数据源依赖,可自行替换其它数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<!-- 阿里的fastjson,用于json转换 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<!-- 文件上传的必要依赖,web项目如果没有该业务,可以不引入 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
3.SpringMVC配置文件
该文件命名为springmvc-servlet.xml,可以随意命名,但后续web.xml配置中必须以该文件名配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="info.lostred" use-default-filters="false">
<!-- 扫描@Controller -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 开启注解支持 -->
<mvc:annotation-driven/>
<!-- 静态资源交给默认的servlet处理 -->
<mvc:default-servlet-handler/>
<!-- 静态资源 -->
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:resources mapping="/WEB-INF/pages/**" location="/WEB-INF/pages/"/>
<!--视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
</bean>
<!-- 配置文件解析器,用于文件上传,若web项目中没有该业务,可以不配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<!-- 配置拦截器,在这里配置需要权限控制的servlet地址 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/personal*/**"/>
<bean class="info.lostred.interceptor.LoginInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/doc/uploadFile"/>
<bean class="info.lostred.interceptor.LoginInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/doc/downloadFile"/>
<bean class="info.lostred.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
4.Spring配置文件
该文件命名为applicationContext.xml,可以随意命名,但后续web.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
https://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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 启用注解扫描,并定义组件查找规则 ,除了@Controller,扫描所有的Bean -->
<context:component-scan base-package="info.lostred">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 使用阿里的druid数据源 -->
<!-- 导入数据库配置文件的占位符 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${oracle.driver}"/>
<property name="url" value="${oracle.url}"/>
<property name="username" value="${oracle.username}"/>
<property name="password" value="${oracle.password}"/>
</bean>
<!-- 注册MyBatis的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 绑定数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定MyBatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置Mapper包扫描 -->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="info.lostred.mapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启AOP注解支持,也可以直接使用配置文件配置 -->
<aop:aspectj-autoproxy/>
<!-- 配置事务的切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有方法都会开启事务,报错时回滚 -->
<tx:method name="*"/>
<!-- 查询方法不使用事务,这里使用的是AntPath匹配规则 -->
<tx:method name="select*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的切入点 -->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut id="txPointcut" expression="execution(* info.lostred.service.impl.*.*(..))"/>
<!-- 绑定切入点的切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
5.MyBatis配置文件
该文件命名为mybatis-config.xml,可以随意命名,但在之前Spring配置中必须以该文件名配置。由于MyBatis已经整合到Spring中,所以MyBatis的配置文件只需要保留settings设置和typeAliases别名就可以了。
<?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>
<properties resource="db.properties"/>
<settings>
<!-- 设置驼峰与下划线命名的转换规则 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 设置MyBatis中的日志实现类为log4j -->
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<!-- 设置实体类别名,简化MyBatis中type和resultType指定的全限定类名 -->
<package name="info.lostred.pojo"/>
</typeAliases>
</configuration>
6.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 欢迎页 -->
<welcome-file-list>
<welcome-file>自行定义</welcome-file>
</welcome-file-list>
<!-- 编码转换字符编码过滤器 -->
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置DispatcherServlet调度,并将该Servlet随项目启动 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置监听,在项目启动时加载Spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
总结
到这里SSM框架就算是搭建完毕了。可以看出这些配置还是比较繁琐的,而且一些设置又不得不配置,所以之后才有了Spring Boot来简化新Spring应用的初始搭建以及开发过程。