前提css
工做環境:JDK 1.八、Mysql 5.7.1八、Intellij IDEA 2018.一、Tomcat 8.五、Mavenhtml
框架版本:Spring 4.2.0.RELEASE、SpringMVC 4.2.0.RELEASE、MyBatis 3.3.0前端
1、項目目錄結構java
目錄結構以下:web
接口實現代碼 cn.javaspring
cn.java.controller 請求接口 cn.java.entity 實體對象 cn.java.service 業務接口層 cn.java.service.impl業務實現層 cn.java.mapper數據庫相關 cn.java.controller.exception 捕獲異常sql
cn.java.filters 過濾器(針對JSP) cn.java.interceptors 攔截器(針對controller) cn.java.tasks 任務數據庫
cn.java.utils 工具集apache
resource配置文件(db/springmvc/log4j等)spring-mvc
applicationContext.xml database.properties log4j.porperties springmvc.xml
前端文件(WebContent)
WebContent/pages/front 存放前端頁面(jsp文件) /WebContent/resources/css 存放css文件
/WebContent/resources/images 存放圖片 /WebContent/resources/js 存放js
/WebContent/resources/upload 上傳 /WebContent/lib/ 存放工程中lib包
/WebContent/WEB-INF/web.xml工程配置文件 /WebContent/index.jsp工程默認首頁jsp
2、項目入口web.xml
JavaEE框架,入口基本都是在web.xml,這一點要記住 ;
那麼Web.xml 文件的做用是什麼呢?
web.xml文件是用來初始化配置信息:好比index頁面、servlet、servlet-mapping、filter、listener、啓動加載級別等。
固然web.xml 也不是必需要有的,當你的web工程沒用到上述配置信息時,你能夠不用web.xml文件來配置你的Application。
web.xml 內容不少,SSM關鍵部分是
1.加載Spring配置文件applicationContext.xml
2.將SpringMVC核心調度器DispatcherServlet註冊爲servlet(配置文件爲springmvc.xml)
web.xml代碼
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!—定義了web應用的名稱--> <display-name>TestPlatform</display-name> <!—指定服務器在收到一個引用目錄名而不是文件名的url時使用哪一個文件--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置過濾器,解決post的亂碼問題 --> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!—配置spring監聽器,能夠在容器啓動時加載contextConfigLocation的context-param節點的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--=============配置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>#表明啓動順序,設置爲>0表示容器在應用啓動時加載並初始化這個servlet </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> <!—指定錯誤404的處理頁面--> <error-page> <error-code>404</error-code> <location>/WEB-INF/pages/error/404.jsp</location> </error-page> </web-app>
3、Spring 框架配置(applicationContext.xml)
主要完成spring與mybatis的整合
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <context:component-scan base-package="cn.java.service.impl" />#注:主容器中不掃描@Controller註解,由於@Controller將會在SpringMVC掃描 <!-- 讀取database.properties文件 --> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 指定properties文件所在路徑 --> <property name="location" value="classpath:database.properties"></property> </bean> <!-- 配置數據源 --> <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 配置driverClassName、url、username、password --> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> <property name="maxActive" value="${maxActive}" /> <property name="minIdle" value="${minIdle}" /> </bean> <!-- 配置掃描保存sql語句的局部xml文件 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定數據源 --> <property name="dataSource" ref="basicDataSource"></property> <!-- 指定局部xml文件的位置 --> <property name="mapperLocations" value="classpath*:cn/java/mapper/*.xml"></property> </bean> <!-- 掃描mapper接口類,而且將接口類與xml文件關聯 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定mapper接口類存放的位置 --> <property name="basePackage" value="cn.java.mapper"></property> </bean> <!-- 配置事務 --> <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="basicDataSource" /> </bean> 事務註解驅動 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> --> </beans>
請注意:
1、多文件配置(一般有如下兩種作法)
(1)在 web.xml配置中的contextConfigLocation節點配置多個值。
(2)在一個application.xml中配置多個import標籤引入其餘文件。
2、mybatis相關配置,主要的就是自動掃描,自動注入,配置數據庫。(引用)
<!-- 1. 數據源配置 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" /> <!-- Druid方式配置數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10" /> <property name="minIdle" value="20" /> <property name="maxActive" value="100" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="6000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 驗證是否回收 --> <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 開啓Druid的監控統計功能 --> <property name="filters" value="stat" /> </bean> <!-- 2. 建立SqlSession的工廠 --> <!-- dataSource:引用數據源,統一加載配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" ></property> <!-- 自動配置別名-做用相似mybatis-config.xml的別名 --> <property name="typeAliasesPackage" value="com.demo.model" /> <!-- 設置別名的類加上父類限定 --> <property name="typeAliasesSuperType" value="com.demo.common.base.BaseEntity"/> <!-- 當mybatis的xml文件和mapper接口不在相同包下時,須要用mapperLocations屬性指定xml文件的路徑 --> <!-- *是個通配符,表明全部的文件,**表明全部目錄下 --> <property name="mapperLocations" value="classpath*:mappings/**/*.xml"/> <!-- 指定mybatis核心配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 3. 自動掃描加載Sql映射文件/接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- sqlSessionFactoryBeanName:表明延遲加載--> <!-- 這個配置的前提條件是:映射接口類文件(.java)和映射XML文件(.xml)須要放在相同的包下(com.demo.mapper)--> <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- basePackage:指定sql映射文件/接口所在的包(自動掃描)--> <property name="basePackage" value="com.demo.mapper"></property> <!-- 掃描basePackage下全部以@MyBatisDao註解的接口 --> <property name="annotationClass" value="com.demo.common.persistence.annotation.MyBatisDao"/> </bean> <!-- 4. 事務管理 --> <!-- dataSource:引用上面定義的數據源 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5. 使用聲明式事務 --> <!-- transaction-manager:引用上面定義的事務管理器 --> <!-- 配置 Annotation 驅動,掃描@Transactional註解的類定義事務 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 定義JdbcTemplate的Bean --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"></bean>
4、SpringMVC 框架配置
這塊配置主要是自動掃描控制器,視圖模式,註解的啓動
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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd"> <!-- 配置包掃描 --> <context:component-scan base-package="cn.java.controller.*" /> <!-- mvc註解驅動 --> <mvc:annotation-driven /> <!-- 定義Spring MVC的攔截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 攔截全部請求 --> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/go2Login.shtml" /> <mvc:exclude-mapping path="/login.shtml" /> <mvc:exclude-mapping path="/loginForm.shtml"/> <!-- 自定義判斷用戶權限的攔截類 --> <bean class="cn.java.interceptors.OneInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!-- 視圖解析器 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置前綴 --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- 配置後綴 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 文件上傳 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 配置默認編碼 --> <property name="defaultEncoding" value="utf-8"></property> <!-- 配置文件上傳的大小 --> <property name="maxUploadSize" value="1048576"></property> </bean> <!-- 數據校驗(hibernate-validator) --> <!-- 定時器 --> </beans>