ssm環境搭建(這是一個非常乾淨的smm基礎環境),和Spring底層相關回顧
阿新 • • 發佈:2020-11-14
前言:
環境基礎:tomcat9,maven3.6.1,mysql8.x,idea2020.3,
一,搭建ssm環境(配置)
1.依賴包
<!--依賴:junit,資料庫驅動,連線池,servlet,jsp,mybatis,mybatis-spring,spring --> <dependencies> <!--單元測--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!--連線池,c3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet Jsp--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--spring整合mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--Spring操作資料庫,spring的事務,需要此包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--AOP織入,事務--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> </dependencies>
2.編寫資料庫連線驅動資訊靜態檔案(db.properties)‘
作用:連線資料庫的基本資訊
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123456
3.mybatis核心配置檔案(mybatis-config)
作用:配置mapper的基本環境,註冊mapper,(連線資料庫交給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"> <configuration> <!--配置資料來源交給Spring容器去做--> <settings> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!--配置別名--> <typeAliases> <package name="com.king.pojo"/> </typeAliases> <!--註冊mapper--> <mappers> <mapper class="com.king.dao.BookMapper"/> </mappers> </configuration>
4.spring整合dao層
作用:
1.關聯資料庫配置檔案:db.properties
2.配置連線池:這裡用的是C3p0的連線池
3.建立sqlSessionFactory:
3.1,連線c3p0資料來源
3.2,繫結mybatis核心配置檔案
4.最後配置dao的介面掃描包
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.關聯資料庫配置檔案--> <context:property-placeholder location="classpath:db.properties"/> <!--2.連線池 dbcp:半自動化操作,不能自動連線 c3p0:自動化操作(自動化的載入配置檔案,並且自動設定到物件當中!) druid: hikari: --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--配置連線池屬性--> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--c3p0連線池的私有屬性--> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!--關閉後不自動提交事務--> <property name="autoCommitOnClose" value="false"/> <!--獲取連線超時時間--> <property name="checkoutTimeout" value="10000"/> <!--獲取連線失敗重試次數--> <property name="acquireRetryAttempts" value="2"/> </bean> <!--3.sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--連線c3p0資料來源--> <property name="dataSource" ref="dataSource"/> <!--繫結mybatis配置檔案--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--4.配置dao介面掃描包,動態的實現了Dao介面可以注入到Spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--要掃描的dao包--> <property name="basePackage" value="com.king.dao"/> </bean> </beans>
5.Spring整合service
作用:
1.利用context掃描service層下的包
2.將所有業務介面實現類全部注入spring容器中
2.1,解釋:利用ioc的思想,DI方法(set方法注入)去實現,動態的建立物件
2.2,什麼叫動態建立物件:當用戶想用是,再去建立(ioc思想)
2.3,因為是DI方法注入,所以要在每個介面實現類(xxx.Impl)上寫set方法,否則報錯
3.宣告式事務配置
3.1,什麼是宣告式事務:宣告顧名思義就是在spring容器xml配置檔案中去宣告程式碼的行為,好處就是不必去修改程式碼本身的邏輯結構
4.配置AOP事務支援
AOP:面向切面程式設計(ioc的思想,在spring中利用DI的方法實現的),
設計模式:代理模式(動態代理模式),JDK動態代理和CGLIb動態代理
面試高頻:spring的AOP是用的那種動態代理模式實現的?JDK or CGlib,兩者有什麼區別?
解釋:spring預設先去找(目標)類是否實現了介面,如果沒有再去用CGlib。先JDK後CGlib(可以強制使用其中某一個,僅需去spring配置相關內容)
區別:JDK是基於介面實現的動態代理模式,就是說你的動態代理實現是被所對應的代理介面約束的(介面有什麼方法,你才能實現什麼方法,實現類不能自己新建方法)
CGlib是基於繼承實現的,就是說具有繼承的特性(子承父業)同時自己也可以新建方法
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.掃描service下的包(有了此掃描,註解才能生效)--> <context:component-scan base-package="com.king.service"/> <!--2.將所有業務類,注入到spring,可以通過配置,或者註解實現--> <bean id="BookServiceImpl" class="com.king.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!--3.宣告式事務配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入資料來源--> <property name="dataSource" ref="dataSource"/> </bean> <!--4.AOP事務支援,用到的話可以加--> </beans>
6.Spring整合mvc
1.配置mvc註解驅動<mvc:annotation-driven/>
解釋:可以自動註冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,這兩個都是來自於dispatcherServlet中(使用者請求分發器)的
DefaultAnnotationHandlerMapping:作用在類上,處理請求對映的,相應的使用者請求傳遞給相應的Controller類中
AnnotationMethodHandlerAdapter:作用在方法上,功能與上相似、
2.靜態資源過濾
作用:防止程式碼頁面與靜態頁面矛盾
3.配置掃描包範圍
4.檢視解析器
作用:給所用跳轉頁面加字首和字尾
<?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/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.mvc註解驅動,讓mvc相關注解生效--> <!--整合了HandlerMapping和HandlerAdapter。全自動的dispatcherservlet--> <mvc:annotation-driven/> <!--2.靜態資源過濾--> <mvc:default-servlet-handler/> <!--3.掃描包:controller--> <context:component-scan base-package="com.king.controller"/> <!--4.檢視解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
7.配置總檔案
將各層的spring容器注入一個總容器中,簡化配置檔案結構,增強可讀性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-service.xml"/> <import resource="classpath:spring-mvc.xml"/> </beans>