使用Spring、SpringMvc、MyBatis搭建SSM框架
阿新 • • 發佈:2019-02-16
SpringMvc架構流程圖
部落格中使用到的jar檔案 在文末Demo中會給出
1、帶你一步步使用IntelliJ IDEA
開發神器建立一個空白的Web Application
專案 配置SSM web專案框架
2、我們需要新增必要的jar(包括:Spring(包括SpringMvc)、MyBatis、MyBatis-Spring整合包、資料庫驅動、第三方連線池)
在與
src
同級的目錄下建立一個libs
目錄用來放.jar
檔案, 對於web專案感覺就是jar包一頓拷貝 貼上。
jackson-annotations-2.4.0
、jackson-core-2.4.2
、jackson-databind-2.4.2
返回json資料支援。
mysql-connector-java-5.1.7-bin
資料庫驅動包。
log4j-1.2.17
、log4j-api-2.0-rc1
、log4j-core-2.0-rc1
、slf4j-api-1.7.5
、slf4j-log4j12-1.7.5
log4j 日誌框架包。
依賴jar 如下圖:右鍵Add As Library
當 依賴jar包之後(或者移除一個jar包) 記得進入專案設定 檢視依賴的jar是否需要新增至:artifact;只要點選Fix 選擇第一個即可
3、建立專案的目錄結構 其中 controller、dao、pojo(簡單的Java物件(Plain Old Java Object)、service 為必需的目錄結構 其他的就看你自己建立了。
4、接下來就是配置 配置 配置 建立一堆配置檔案;在src
同級目錄下建立一個config
目錄專門用來存放配置檔案。
這裡需要將這個資料夾設為資原始檔夾,在資料夾上右鍵如下操作:
首先配置Dao層:
- MyBatis配置
sqlMapConfig.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">
- 1
- 2
- 3
- 4
- 5
- 配置資料庫連線
db.properties
jdbc驅動 地址 使用者名稱 密碼
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.0.3:3306/azhon?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
- 1
- 2
- 3
- 4
- 配置
applicationContextDao.xml
,讓spring管理sql session factory 使用mybatis和spring整合包中的、配置Mapper掃描器
<?xml version="1.0" encoding="UTF-8" ?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 載入配置檔案 --> <context:property-placeholderlocation="classpath:db.properties" /> <!-- 資料庫連線池 --> <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <propertyname="driverClassName"value="${jdbc.driver}" /> <propertyname="url"value="${jdbc.url}" /> <propertyname="username"value="${jdbc.username}" /> <propertyname="password"value="${jdbc.password}" /> <propertyname="maxActive"value="10" /> <propertyname="maxIdle"value="5" /> </bean> <!-- mapper配置 --> <!-- 讓spring管理sql session factory 使用mybatis和spring整合包中的 --> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 資料庫連線池 --> <propertyname="dataSource"ref="dataSource" /> <!-- 載入mybatis的全域性配置檔案 --> <propertyname="configLocation"value="classpath:sqlMapConfig.xml" /> </bean> <!-- 配置Mapper掃描器 value 改為你的對應包名--> <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"> <propertyname="basePackage"value="com.azhon.dao" /> </bean> </beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- MyBatis配置
配置Service層:
applicationContextService.xml
配置包掃描器,掃描@Service註解的類。
<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- @Service掃描 base-package 改為你的對應包名--> <context:component-scanbase-package="com.azhon.service" /> </beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
applicationContextTrans.xml
配置事務
<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 事務管理器 --> <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 資料來源 --> <propertyname="dataSource"ref="dataSource" /> </bean> <!-- 通知 --> <tx:adviceid="txAdvice"transaction-manager="transactionManager"> <tx:attributes> <!-- 傳播行為 --> <tx:methodname="save*"propagation="REQUIRED" /> <tx:methodname="insert*"propagation="REQUIRED" /> <tx:methodname="delete*"propagation="REQUIRED" /> <tx:methodname="update*"propagation="REQUIRED" /> <tx:methodname="find*"propagation="SUPPORTS"read-only="true" /> <tx:methodname="get*"propagation="SUPPORTS"read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 com.azhon.service 改為你的對應包名--> <aop:config> <aop:advisoradvice-ref="txAdvice"pointcut="execution(* com.azhon.service.*.*(..))" /> </aop:config> </beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
配置表現層
springMvc.xml
自動配置最新版的處理器對映器和處理器介面卡、配置@Controller註解掃描
<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 註解驅動:作用:替我們自動配置最新版的處理器對映器和處理器介面卡 有log4j時也會自動配置--> <mvc:annotation-driven> <!--解決返回字串亂碼問題--> <mvc:message-convertersregister-defaults="true"> <beanclass="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-argvalue="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--配置 @Controller 掃描 base-package 改為你對應的包名--> <context:component-scanbase-package="com.azhon.controller" /> <!-- 配置檢視解析器 作用:在Controller中指定頁面路徑的時候就不用寫頁面的完整路徑名稱了,可以直接寫頁面去掉副檔名的名稱 --> <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 真正的頁面路徑 = 字首 + 去掉字尾名的頁面名稱 + 字尾 --> <!-- 字首 --> <propertyname="prefix"value="/jsp/" /> <!-- 字尾 --> <propertyname="suffix"value=".jsp" /> </bean> </beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 檢視解析器 可以配置 也可以不配置、當你專案用的都是同一種檔案型別的時候你可以配置 可以很方便的使用。當你同時用到了 .html .jsp檔案的時候這個就沒什麼用了,所以我在專案中就直接註釋了。
配置
log4j2.properties
日誌,更多的配置可以前往官網或者百度查詢。
log4j.rootLogger=DEBUG, CONSOLE, FILE
## for console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] - %m%n
- 1
- 2
- 3
- 4
- 5
5、來看下config
的最終樣子
6、配置前端控制器web.xml
檔案位於專案的/web/WEB-INF/
下
<?xml version="1.0" encoding="UTF-8"?>
<web-appxmlns="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_3_1.xsd"version="3.1">
<!--配置為 / 直接接url 預設為*.action-->
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--spring mvc 前端控制器-->
<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:springMvc.xml</param-value>
</init-param>
<!--在tomcat啟動的時候就載入這個servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--因為配置了 / 攔截所有的請求導致無法訪問靜態資源 配置靜態資源訪問-->
<!--http://blog.csdn.net/wangyi201212/article/details/50404101-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--載入spring 容器 載入配置檔案-->
<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>
<!--配置專案啟動的主介面-->
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<!--配置為UTF-8-->
<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