文章目錄
- 🎉SSM整合流程
- 一、兩個容器整合?
- 1、先準備好數據庫config.properties連接、mybatis-config.xml🎊
- 2、容器一:優先配置spring.xml文件🎊
- 3、容器二:配置springMVC.xml文件🎊
- 4、Tomcat整合spring容器、springMVC容器,webapp文件下的web.xml文件🎊
- 二、一個容器整合
- 1、 現在去掉了spring.xml文件,將spring.xml文件的內容放到springMVC🎊
- 2、Tomcat整合springMVC容器,webapp文件下的web.xml文件🎊
🎉SSM整合流程
分為兩種:兩個容器整合
一個容器整合
本項目的路徑參考:
??注意:優先maven項目的pom.xml文件里導入jar包依賴:
<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><aaa>4.3.8.RELEASE</aaa></properties><dependencies><dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><exclusions><exclusion><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></exclusion></exclusions><version>3.19.2</version></dependency><dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.0.0</version><type>pom</type><!--依賴的類型,聲明引入的依賴的類型,值一般有jar、war、pom--></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- spring的webmvc的依賴 --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${aaa}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${aaa}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${aaa}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.8.RELEASE</version></dependency><!-- Mysql的依賴 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- mybatis的依賴 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><!--數據庫連接池的依賴 --><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><!-- spring-mybatis的依賴 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.2</version></dependency><!-- springjdbc的依賴 --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.8.RELEASE</version></dependency><!-- aspectj的依賴 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version></dependency><!-- 添加日志log4j的依賴 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- jackson的依賴 --><!-- <dependency>--><!-- <groupId>com.fasterxml.jackson.core</groupId>--><!-- <artifactId>jackson-databind</artifactId>--><!-- <version>2.9.7</version>--><!-- </dependency>--><!-- 依賴 --><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-servlet-api</artifactId><version>8.0.28</version><scope>provided</scope><!-- 生命周期:test --></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency><!-- json依賴 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.71</version></dependency><!--導入lombok依賴 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.8</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.3.2</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version></dependency><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.5</version></dependency><!-- https://mvnrepository.com/artifact/org.jdom/jdom2 --><dependency><groupId>org.jdom</groupId><artifactId>jdom2</artifactId><version>2.0.6</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.71</version></dependency><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>RELEASE</version><scope>compile</scope></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><!-- redis客戶端jar --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.7.3</version></dependency><!-- spring-redis實現 --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.7.2.RELEASE</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-api</artifactId><version>0.11.2</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-impl</artifactId><version>0.11.2</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-jackson</artifactId><version>0.11.2</version></dependency></dependencies>
一、兩個容器整合?
1、先準備好數據庫config.properties連接、mybatis-config.xml🎊
(1)數據庫config.properties連接
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/emp?serverTimeZone=Asia/ShangHai&useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=nigx=0128
(2)mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- <properties resource="config.properties"> --><!-- </properties> --><settings><!--將表中字段的下劃線自動轉換為駝峰--><setting name="mapUnderscoreToCamelCase" value="true"/><setting name="logImpl" value="STDOUT_LOGGING"/><!-- 如果需要更改一級緩存的范圍,可以在Mybatis的配置文件中,在下通過localCacheScope指定。 --><!-- <setting name="localCacheScope" value="STATEMENT"/> --></settings><!-- 類型別名配置各個標簽時,需要遵守前后順序--><typeAliases><package name="com.pojo"/></typeAliases><!-- <typeAliases>--><!-- <typeAlias alias="user" type="com.pojo.User"></typeAlias>--><!-- <typeAlias type="com.pojo.Employee"></typeAlias>--><!-- <typeAlias type="com.pojo.Log"></typeAlias>--><!-- <typeAlias type="com.pojo.Dept"></typeAlias>--><!-- </typeAliases>--><!-- environments:配置數據庫的連接環境信息,可以配置多個environment,通過default屬性切換不同的environment --><!-- <environments default="development"> --><!-- <environment id="development"> --><!-- <transactionManager type="JDBC"/> --><!-- <!–數據庫連接池–> --><!-- <dataSource type="POOLED"> --><!-- <!–數據庫的連接信息–> --><!-- <property name="driver" value="${driver}"/> --><!-- <property name="url" value="${url}"/> --><!-- <property name="username" value="${username}"/> --><!-- <property name="password" value="${password}"/> --><!-- </dataSource> --><!-- </environment> --><!-- </environments> --><mappers><!--加載SQL映射文件--><!--<mapper resource="nigaoxian\mapper\UserMapper.xml"/>--><!-- Mapper 代理方式,在spring容器映射過就不需要配置了--><!-- <package name="com.dao"/> --></mappers>
</configuration>
2、容器一:優先配置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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 2*3+1 --><!--掃描組件(除控制層)--><context:component-scan base-package="com.service,com.service.serviceImpl,com.interceptor,com.exception"><!-- 按照注解進行排除,標注了指定注解的組件不要掃描 --><!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>--><!-- 指定排除某個具體的類,按照類排除 --><!-- <context:exclude-filter type="assignable" expression="com.controller.EmpController"/> --></context:component-scan><!-- 配置數據源 --><!--引入config.properties--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config.properties</value></list></property></bean><!-- 配置數據參數與連接屬性 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="${driver}"></property><property name="maxPoolSize" value="100"></property><property name="minPoolSize" value="30"></property><property name="jdbcUrl" value="${url}"></property><property name="user" value="${username}"></property><property name="password" value="${password}"></property></bean><!-- 配置spring事務管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--開啟事務的注解驅動,將使用注解 @Transactional 標識的方法或類中所有的方法進行事務關聯--><tx:annotation-driven transaction-manager="transactionManager"/><!-- 配置SqlSessionFactoryBean,可以直接在Spring的IOC中獲取SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 設置MyBatis的核心配置文件的路徑 --><property name="configLocation" value="classpath:mybatis-config.xml"/><!-- 設置數據源 --><property name="dataSource" ref="dataSource"/><!-- 設置類型別名,默認為類名 --><!-- <property name="typeAliasesPackage" value="com.pojo"/> --><!-- 設置映射文件所在的包,只有在映射文件的包和mapper接口的包不一致時需要設置 --><property name="mapperLocations" value="classpath:com/dao/*.xml"/><!--引入分頁插件--><!-- <property name="plugins"> --><!-- <array> --><!-- <bean class="com.github.pagehelper.PageInterceptor"/> --><!-- </array> --><!-- </property> --></bean><!--配置mapper接口的掃描配置,由mybatis-spring提供,可以將指定包下所有的mapper接口創建動態代理,并將這些動態代理作為IOC容器的bean管理,可以直接在Spring的IOC中獲取Mapper接口了。--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="com.dao"/></bean>
</beans>
3、容器二:配置springMVC.xml文件🎊
<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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 啟動mvc注解驅動 --><mvc:annotation-driven/><!-- 配置掃描的包 --><context:component-scan base-package="com.controller"/><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><mvc:cors><mvc:mapping path="/**" allowed-origins="*" allowed-headers="*" allowed-methods="*" allow-credentials="false"max-age="3600"/></mvc:cors><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><bean id="corsInterceptor" class="com.interceptor.CORSInterceptor"/></mvc:interceptor><mvc:interceptor><mvc:mapping path="/**"/><mvc:exclude-mapping path="/login"/><mvc:exclude-mapping path="/register"/><bean id="loginInterceptor" class="com.interceptor.LoginInterceptor"/></mvc:interceptor></mvc:interceptors>
</beans>
4、Tomcat整合spring容器、springMVC容器,webapp文件下的web.xml文件🎊
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><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"><!-- tomcat整合spring --><!--配置Spring的編碼過濾器--><!--配置Spring的監聽器,在服務器啟動的時候加載Spring的配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--設置spring配置文件自定義的位置和名稱--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param><!-- <filter> --><!-- <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> --><!-- <url-pattern>/*</url-pattern> --><!-- </filter-mapping> --><!-- <!– 配置處理請求方式PUT和DELETE的過濾器–> --><!-- <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> --><!-- tomcat整合spring --><!-- 配置SpringMVC的前端控制器 --><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 設置SpringMVC的配置文件的位置和名稱 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springMVC.xml</param-value></init-param><!-- 將DispatcherServlet的初始化時間提前到服務器啟動時--><load-on-startup>1</load-on-startup></servlet><!-- 為什么使用/,并且這里為什么會默認放行jsp?設置/是為了兜底的,因為只有一個servlet,就是默認為DispatcherServlet的servlet,需要使用這個servlet攔截請求。因為jsp底層本身就是servlet,設置多個servlet此時只映射的是DispatcherServlet--><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
二、一個容器整合
1、 現在去掉了spring.xml文件,將spring.xml文件的內容放到springMVC🎊
springMVC.xml文件如下:
<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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 2*3+1 --><!--掃描組件(除控制層)--><context:component-scan base-package="com.service,com.service.serviceImpl,com.interceptor,com.exception"><!-- 按照注解進行排除,標注了指定注解的組件不要掃描 --><!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>--><!-- 指定排除某個具體的類,按照類排除 --><!-- <context:exclude-filter type="assignable" expression="com.controller.EmpController"/> --></context:component-scan><!-- 配置數據源 --><!--引入config.properties--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config.properties</value></list></property></bean><!-- 配置數據參數與連接屬性 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="${driver}"></property><property name="maxPoolSize" value="100"></property><property name="minPoolSize" value="30"></property><property name="jdbcUrl" value="${url}"></property><property name="user" value="${username}"></property><property name="password" value="${password}"></property></bean><!-- 配置spring事務管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--開啟事務的注解驅動,將使用注解 @Transactional 標識的方法或類中所有的方法進行事務關聯--><tx:annotation-driven transaction-manager="transactionManager"/><!-- 配置SqlSessionFactoryBean,可以直接在Spring的IOC中獲取SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 設置MyBatis的核心配置文件的路徑 --><property name="configLocation" value="classpath:mybatis-config.xml"/><!-- 設置數據源 --><property name="dataSource" ref="dataSource"/><!-- 設置類型別名,默認為類名 --><!-- <property name="typeAliasesPackage" value="com.pojo"/> --><!-- 設置映射文件所在的包,只有在映射文件的包和mapper接口的包不一致時需要設置 --><property name="mapperLocations" value="classpath:com/dao/*.xml"/><!--引入分頁插件--><!-- <property name="plugins"> --><!-- <array> --><!-- <bean class="com.github.pagehelper.PageInterceptor"/> --><!-- </array> --><!-- </property> --></bean><!--配置mapper接口的掃描配置,由mybatis-spring提供,可以將指定包下所有的mapper接口創建動態代理,并將這些動態代理作為IOC容器的bean管理,可以直接在Spring的IOC中獲取Mapper接口了。--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="com.dao"/></bean><!-- 啟動mvc注解驅動 --><mvc:annotation-driven/><!-- 配置掃描的包 --><context:component-scan base-package="com.controller"/><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><mvc:cors><mvc:mapping path="/**" allowed-origins="*" allowed-headers="*" allowed-methods="*" allow-credentials="false"max-age="3600"/></mvc:cors><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><bean id="corsInterceptor" class="com.interceptor.CORSInterceptor"/></mvc:interceptor><mvc:interceptor><mvc:mapping path="/**"/><mvc:exclude-mapping path="/login"/><mvc:exclude-mapping path="/register"/><bean id="loginInterceptor" class="com.interceptor.LoginInterceptor"/></mvc:interceptor></mvc:interceptors>
</beans>
2、Tomcat整合springMVC容器,webapp文件下的web.xml文件🎊
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><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"><!-- tomcat整合springMVC --><!-- 配置SpringMVC的前端控制器 --><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 設置SpringMVC的配置文件的位置和名稱 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springMVC.xml</param-value></init-param><!-- 將DispatcherServlet的初始化時間提前到服務器啟動時--><load-on-startup>1</load-on-startup></servlet><!-- 為什么使用/,并且這里為什么會默認放行jsp?設置/是為了兜底的,因為只有一個servlet,就是默認為DispatcherServlet的servlet,需要使用這個servlet攔截請求。因為jsp底層本身就是servlet,設置多個servlet此時只映射的是DispatcherServlet--><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>