雖然說現在大部分的項目已經使用上了SpringBoot,分佈式也有Spring Cloud,Dubbo。SSM快要消失在歷史的長河,但終究還是有存活於世的倖存者,在此,僅以總結一下SSM的配置來紀念一下逝去的歲月。
1、項目環境搭建
要進行SSM整合的話,首先你得有一個項目的構架,先創建Maven的web工程,此處就不做演示了。
2、整合Spring框架
2.1、在pom.xml文件中,引入Spring的依賴jar包
該依賴包含了SpringMVC開發時的核心類,也有Spring的核心jar文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
該jar文件包含對Spring對JDBC數據訪問進行封裝的所有類
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
提供對AspectJ的支持
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
2.2、在resources/spring目錄下,創建spring的核心配置文件:applicationContext.xml
mybatis和spring文件夾是我自己創建的
配置內容如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</beans>
3、整合SpringMVC框架
因爲前面在添加spring的jar包同時, 將springmvc的jar包也引入了,所以這裏不再引入springmvc的jar包。
3.1、創建springmvc的核心配置文件
在resources/spring目錄下,創建springmvc的核心配置文件:springmvc-config.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/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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 1.配置前端控制器放行靜態資源(html/css/js等,否則靜態資源將無法訪問) -->
<mvc:default-servlet-handler/>
<!-- 2.配置註解驅動,用於識別註解(比如@Controller) -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3.配置需要掃描的包:spring自動去掃描 base-package 下的類,
如果掃描到的類上有 @Controller、@Service、@Component等註解,
將會自動將類註冊爲bean
-->
<context:component-scan base-package="com.aaa.controller">
</context:component-scan>
<!-- 4.配置內部資源視圖解析器
prefix:配置路徑前綴
suffix:配置文件後綴
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.2、在web.xml中配置springmvc
<!-- 配置springmvc, 將所有請求交給springmvc來處理 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc核心配置文件的位置,默認Springmvc的配置文件是在WEB-INF目錄下,默認的名字爲springmvc-servlet.xml,如果要放在其他目錄,則需要指定如下配置:
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</init-param>
</servlet>
<!-- 其中的斜槓(/)表示攔截所有請求(除JSP以外), 所有請求都要經過springmvc前端控制器 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.3、配置springmvc亂碼處理過濾器
<!-- 亂碼處理過濾器 -->
<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>
4、整合mybatis框架
4.1、在pom.xml文件中,引入mybatis及相關依賴包
<!-- 整合mybatis框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- druid連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
4.2、創建mybatis的核心配置文件
在resources/mybatis目錄下,創建mybatis的核心配置文件:mybatis-config.xml
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置開發環境 -->
<environments default="develop">
<!-- 這裏可以配置多個環境,比如develop,test等 -->
<environment id="develop">
<!-- 1.1.配置事務管理方式:JDBC:將事務交給JDBC管理(推薦) -->
<transactionManager type="JDBC"></transactionManager>
<!-- 1.2.配置數據源,即連接池方式:JNDI/POOLED/UNPOOLED -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xxdb?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml -->
<mappers>
<mapper resource="mybatis/mapper/xxMapper.xml"/>
</mappers>
</configuration>
4.3
創建封裝信息用的實體類pojo,在mapper目錄下創建映射問價xxmapper.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 門店表的映射文件 namespace值爲對應接口的全路徑 -->
<mapper namespace="com.aa.dao.xxMapper">
<!-- 1.查詢所有門店信息,id值爲對應接口中方法的名字
resultType指定將查詢的結果封裝到哪個pojo對象中
-->
<select id="findAll" resultType="com.aa.pojo.pojo">
select * from xx_door
</select>
</mapper>
4.4、在mybatis的全局配置文件中引入xxrMapper.xml映射文件
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置開發環境 -->
...
<!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml -->
<!-- 配置映射文件 -->
<mappers>
<mapper resource="mybatis/mapper/xxMapper.xml"/>
</mappers>
</configuration>
5、整合spring和mybatis
5.1、修改mybatis-config.xml文件,將連接池等配置移除,在spring中配置
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置開發環境 -->
<!-- 1.1.配置事務管理方式:JDBC:將事務交給JDBC管理(推薦) -->
<!-- 1.2.配置數據源,即連接池方式:JNDI/POOLED/UNPOOLED -->
<!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml -->
</configuration>
5.2、在applicationContext.xml中配置druid連接池
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 1.加載jdbc.properties文件的位置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.配置druid連接池 ,id是固定值,class是druid連接池類的全路徑 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 配置連接數據庫的基本信息 -->
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<!-- 3.整合spring和mybatis框架
將SqlSession等對象的創建交給Spring容器
id值(sqlSessionFactory)是固定值
-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 3.1.指定mybatis核心配置文件的位置 -->
<property name="configLocation"
value="classpath:mybatis/mybatis-config.xml"></property>
<!-- 3.2.配置連接池(數據源) ref指向連接池bean對象的id值 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 3.3、掃描所有的 XxxMapper.xml映射文件,讀取其中配置的SQL語句 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
</bean>
<!-- 4、定義mapper接口掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描所有XxxMapper接口,將接口實例的創建交給spring容器 -->
<property name="basePackage"
value="com.aa.dao"/>
</bean>
<!-- 5.配置需要掃描的包(service層):spring自動去掃描 base-package下的類,
如果掃描到的類上有 @Controller、@Service、@Component等註解,
將會自動將類註冊爲bean(即由spring創建實例)
-->
<context:component-scan
base-package="com.aa.service">
</context:component-scan>
</beans>
5.3、在resources目錄下創建jdbc.properties文件,將連接數據庫的基本信息提取到文件中
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///xxdb?characterEncoding=utf-8
db.username=root
db.password=root
再往下就是業務了,哈哈哈
如有問題的話,請提出,一起學習一起進步。