SSM整合(環境搭建)
阿新 • • 發佈:2021-11-03
SSM整合(環境搭建)
以一個web專案為例子。
需要的jar
1.先搭建一個Springmvc(先測試可以連線得上)
在resource資料夾下建立一個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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 設定使用註解的掃描controller類所在的包 --> <context:component-scan base-package="com.etc.controller" /> <!-- 定義跳轉的檔案的前後綴 ,檢視模式配置,預設在跳轉時新增字首字尾--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這裡的配置我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 檔案上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <!-- 設定檔案上傳的大小 --> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="1048567"/> </bean> </beans>
2.在WebContent下建立web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- post編碼過濾器 --> <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> <!-- 配置一個springmvc的攔截器 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
到此mvc配置好了、保證測試能通過。(建立controller,測試攔截器)
2.整合Spring和mybatis
1.建立myabtis-config.xml(我們有要註冊資料來源,工廠和、獲取session(三種自己補充))
<?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> <!--放myabtis的配置 --> </configuration>
把配置資訊搬到spring裡面
建立applicationcontent.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:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 這樣框架就會自動幫我們判斷請求型別,請求靜態檔案時就不會去尋找handler,而是直接找檔案。
是當我們同時使用註解開發時,就會把所有的請求都轉到default-servlet-handler進行處理,此時訪問就會報錯404
解決方法就是在springmvc.xml中新增如下配置 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="747452" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:Mybatis-Config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<context:component-scan base-package="com.etc">
</context:component-scan>
<!-- 針對產生了通用型會批量生產 -->
<!-- DAO介面所在包名,Spring會自動查詢其下的類 ,包下的類需要使用@MapperScan註解,否則容器注入會失敗,產生的是開頭小寫於xml同名的的bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.etc.dao.user" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
測試整合無誤
回到web.xml新增整合配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 整合SSM時這個配置會幫你開啟容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContent.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- post編碼過濾器 -->
<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>
<!-- 配置一個springmvc的攔截器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
現在可以開始業務了。
在此之前需要配置日誌:(直接懟進去即可)
### 設定Logger輸出級別和輸出目的地 ###
#日誌記錄器輸出級別:fatal > error > warn > info >debug#
log4j.rootLogger=debug,stdout,logfile
### 把日誌資訊輸出到控制檯 ###
#日誌資訊輸出到控制檯#
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#資訊列印到System.err上 #
log4j.appender.stdout.Target=System.err
#指定日誌佈局型別#
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### 把日誌資訊輸出到檔案:etc.log ###
#日誌資訊寫到檔案中 #
log4j.appender.logfile=org.apache.log4j.FileAppender
#指定日誌輸出的檔名#
log4j.appender.logfile.File=./power_company.log
#指定轉換模式#
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#指定日誌佈局型別#
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %l %F %p %m%n