2023-01-22 一、SSM整合 1、Spring + SpringMVC (1)容器管理對象,由DispatcherServlet管理 (2)Spring容器對象,由ContextLoaderListener管理 2、解決組件掃描的衝突問題 (1)SpringMVC只掃描Controller層 ...
2023-01-22
一、SSM整合
1、Spring + SpringMVC
(1)容器管理對象,由DispatcherServlet管理
(2)Spring容器對象,由ContextLoaderListener管理
2、解決組件掃描的衝突問題
(1)SpringMVC只掃描Controller層
(2)Spring掃描排除Controller層
2、Spring+Mybatis
(1)關於數據源、事務管理的代碼衝突問題
同意交給Spring管理
(2)Spring管理Mybatis核心對象
①SqlSessionFactory
②Mapper代理對象
二、SSM整合步驟
1、Spring + SpringMVC
(1)導入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
(2)配置文件
①web.xml
a.註冊CharacterEncodingFilter,解決請求亂碼問題
b.註冊HiddenHttpMethodFilter,支持PUT&DELETE提交【REST風格】
c.註冊DispatcherServlet(前端控制器),管理springMVC容器對象
d.註冊一個上下文參數(contextConfigLocation),設置spring.xml配置文件路徑
e.註冊ContextLoaderListener,管理spring容器對象(目的是為了載入spring的配置文件,幫我們初始化IOC容器)
<?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"> <!-- d.註冊一個上下文參數(contextConfigLocation),設置spring.xml配置文件路徑--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- e.註冊ContextLoaderListener,管理spring容器對象--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- a.註冊CharacterEncodingFilter,解決請求亂碼問題--> <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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- b.註冊HiddenHttpMethodFilter,支持PUT&DELETE提交【REST風格】--> <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> <!-- c.註冊DispatcherServlet(前端控制器),管理springMVC容器對象--> <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.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> </web-app>
②springMVC.xml
a.開啟組件掃描(只掃描Controller)
b.裝配視圖解析器
c.裝配視圖控制器(view-controller)
d.裝配"default-servlet-handler",解決靜態資源載入問題
e.裝配"annotation-driven",解決後續問題
解決view-controller問題、解決default-servlet-handler問題、解決Jackon配置消息轉換器問題
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- a.開啟組件掃描(只掃描Controller)--> <context:component-scan base-package="com.hh" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- b.裝配視圖解析器--> <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="characterEncoding" value="UTF-8"></property> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="characterEncoding" value="UTF-8"></property> <property name="prefix" value="WEB-INF/pages/"></property> <property name="suffix" value=".html"></property> </bean> </property> </bean> </property> </bean> <!-- c.裝配視圖控制器(view-controller)--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!-- d.裝配"default-servlet-handler",解決靜態資源載入問題--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!-- e.裝配"annotation-driven",解決後續問題--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
③spring.xml
開啟組件掃描(排除Controller層)
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啟組件掃描--> <context:component-scan base-package="com.hh"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
2、Spring+Mybatis
(1)導入jar包
①spring的jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.10</version> </dependency>
②mybatis的jar包
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>
③spring與mybatis整合jar包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency>
(2)配置文件
①spring.xml
a.開啟組件掃描(排除Controller層)
b.載入外部屬性文件
c.裝配數據源(DruidDataSource)
d.裝配事務管理器(DataSourceTransactionManager)
e.開啟聲明式事務管理註解支持
f.裝配SqlSessionFactoryBean,管理SqlSessionFactory
g.裝配MapperScannerConfigurer,管理Mapper代理對象
<?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" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <!-- a.開啟組件掃描(排除Controller層)--> <context:component-scan base-package="com.hh"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- b.載入外部屬性文件--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- c.裝配數據源(DruidDataSource)--> <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> <!-- d.裝配事務管理器(DataSourceTransactionManager)--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- e.開啟聲明式事務管理註解支持--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <!-- f.裝配SqlSessionFactoryBean,管理SqlSessionFactory--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 設置數據源--> <property name="dataSource" ref="dataSource"></property> <!-- 設置mybatis-config.xml核心配置文件路徑--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 設置類型別名--> <property name="typeAliasesPackage" value="com.hh.pojo"></property> <!-- 設置映射文件路徑--> <property name="mapperLocations" value="classpath:/mapper/*.xml"></property> </bean> <!-- g.裝配MapperScannerConfigurer,管理Mapper代理對象--> <mybatis-spring:scan base-package="com.hh.mapper"></mybatis-spring:scan> </beans>
②springmvc.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- a.開啟組件掃描(只掃描Controller)--> <context:component-scan base-package="com.hh" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- b.裝配視圖解析器--> <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="characterEncoding" value="UTF-8"></property> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="characterEncoding" value="UTF-8"></property> <property name="prefix" value="WEB-INF/pages/"></property> <property name="suffix" value=".html"></property> </bean> </property> </bean> </property> </bean> <!-- c.裝配視圖控制器(view-controller)--> <mvc:view-controller path="/" view-name="index"></