低耦合搭建SSM框架專案
阿新 • • 發佈:2018-11-23
低耦合搭建SSM框架專案(springMVC、spring、MyBatis)
1.SSM框架專案需要的jar包
2.一些關鍵的配置檔案
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"> <!-- 自定義404和500錯誤頁 --> <error-page> <error-code>404</error-code> <location>/resources/error/404.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/resources/error/500.html</location> </error-page> <!-- 自定義起始頁面 --> <display-name>SSM測試</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 例項化ApplicationContext容器 --> <context-param> <!-- 載入classpath下的applicationContext.xml檔案 --> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <!-- 指定以ContextLoaderListener方式啟動Spring容器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!--配置DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 讀取classpath下的springmvc.xml檔案 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 配置DispatcherServlet優先被啟動 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 設定url-pattern為/,則DispatcherServlet處理所有前端請求, 也可以配置為*.do、*.action、*.shtml,這樣配DispatcherServlet只處理指定字尾的請求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 避免中文亂碼 --> <filter> <filter-name>characterEncodingFilter</filter-name> <!-- 使用spring自帶的編碼過濾器 --> <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> </web-app>
spring的配置檔案applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 將jdbc.properties檔案讀取進來 --> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 指定jdbc.properties存放的位置 --> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!-- 配置資料來源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 最大連線數 --> <property name="maxTotal" value="30"/> <!-- 最大空閒連線數 --> <property name="maxIdle" value="10"/> <!-- 初始化連線數 --> <property name="initialSize" value="5"/> </bean> <!-- 新增事務支援 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啟事務註解--> <tx:annotation-driven transaction-manager="txManager" /> <!-- 配置MyBatis工廠,同時指定資料來源,並與MyBatis完美整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- configLocation的屬性值為MyBatis的核心配置檔案 --> <property name="configLocation" value="classpath:com/mybatis/mybatis-config.xml"/> </bean> <!--Mapper代理開發,使用Spring自動掃描MyBatis的介面並裝配 (Spring將指定包中所有被@Mapper註解標註的介面自動裝配為MyBatis的對映介面) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mybatis-spring元件的掃描器 --> <property name="basePackage" value="com.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 指定需要掃描的包(包括子包),使註解生效。dao包在mybatis-spring元件中已經掃描,這裡不再需要掃描--> <context:component-scan base-package="com.service"/> </beans>
springMVC的配置檔案springmvc-servlet.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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 使用掃描機制,掃描包 --> <context:component-scan base-package="com.controller.*" /> <!-- 配置檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 字首 --> <property name="prefix" value="/view/" /> <!-- 字尾 --> <property name="suffix" value=".jsp" /> </bean> <!-- 配置springMVC特有的驅動 --> <mvc:annotation-driven /> <mvc:resources mapping="/resources/css/**" location="/resources/css/" ></mvc:resources> <mvc:resources mapping="/resources/js/**" location="/resources/js/" ></mvc:resources> <mvc:resources mapping="/resources/images/**" location="/resources/images/" ></mvc:resources> <mvc:resources mapping="/resources/error/**" location="/resources/error/" ></mvc:resources> <!-- 配置404和500頁面 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <map> <entry key="ResourceNotFoundException" value="common/error/resourceNotFoundError" /> <entry key="DataAccessException" value="common/error/dataAccessError" /> </map> </property> <property name="statusCodes"> <map> <entry key="common/error/resourceNotFoundError" value="404" /> <entry key="common/error/dataAccessError" value="500" /> </map> </property> </bean> </beans>
資料庫配置檔案jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8
username=root
password=123456
日誌配置檔案log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis核心配置檔案mybatis-config.xml
<?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>
<mappers><!-- 對映器,告訴 MyBatis到哪裡去找對映檔案-->
<mapper resource="com/mybatis/UserMapper.xml"/>
</mappers>
</configuration>
注意:為了降低專案耦合度,將配置檔案(如xml,properties)放到一個獨立的資料夾config中
---------------------------------------------------------------------------------------------------------------------------------------------------------
專案下載地址連結:https://pan.baidu.com/s/1acakwEMtb9w0n7AbhXa1_w
提取碼:576l
複製這段內容後開啟百度網盤手機App,操作更方便哦