SSM(Spring+SpringMVC+Mybatis)框架環境搭建(整合步驟)(一)


1. 前言

最近在寫畢設過程中,重新梳理了一遍SSM框架,特此記錄一下。

附上源碼:https://gitee.com/niceyoo/jeenotes-ssm

2. 概述

在寫代碼之前我們先了解一下這三個框架分別是干什么的? 

  1. SpringMVC:它用於web層,相當於controller(等價於傳統的servlet和struts的action),用來處理用戶請求。舉個例子,用戶在地址欄輸入http://網站域名/login,那么springmvc就會攔截到這個請求,並且調用controller層中相應的方法,(中間可能包含驗證用戶名和密碼的業務邏輯,以及查詢數據庫操作,但這些都不是springmvc的職責),最終把結果返回給用戶,並且返回相應的頁面(當然也可以只返回json/xml等格式數據)。springmvc就是做前面和后面過程的活,與用戶打交道!!

  2. Spring:太強大了,以至於我無法用一個詞或一句話來概括它。但與我們平時開發接觸最多的估計就是IOC容器,它可以裝載bean(也就是我們java中的類,當然也包括service dao里面的),有了這個機制,我們就不用在每次使用這個類的時候為它初始化,很少看到關鍵字new。另外spring的aop,事務管理等等都是我們經常用到的。

  3. MyBatis:如果你問我它跟鼎鼎大名的Hibernate有什么區別?我只想說,他更符合我的需求。第一,它能自由控制sql,這會讓有數據庫經驗的人(當然不是說我啦~捂臉~)編寫的代碼能搞提升數據庫訪問的效率。第二,它可以使用xml的方式來組織管理我們的sql,因為一般程序出錯很多情況下是sql出錯,別人接手代碼后能快速找到出錯地方,甚至可以優化原來寫的sql。

3. SSM框架整合配置

3.1 開發環境

IDE Eclipse Mars2 

Jdk: 1.7

數據庫: MySQL

注:本例演示采用的開發工具是Eclipse,不要讓開發工具限制了你的學習,按照自己的需要來創建就好,用什么工具就按照什么步驟來創建。

 

3.2 創建Java WEB項目

新建Dynamic Web Project
File->New->Other->Web->Dynamic Web Project

以下是在Package Explorer 視圖下的完整目錄結構,具體內容看圖:

3.3 導入所需jar包

  1. spring(包括springmvc
  2. mybatis
  3. mybatis-spring整合包
  4. 數據庫驅動
  5. 第三方連接池。
  6. Json依賴包Jackson

   jar包最后會提供下載地址。

 

3.4 整合思路

1、Dao層:
Mybatis的配置文件:mybatis-config.xml
不需要配置任何內容,需要有文件頭。文件必須存在。
spring-dao.xml:mybatis整合spring,通過由spring創建數據庫連接池,spring管理SqlSessionFactory、mapper代理對象。需要mybatis和spring的整合包。
2、Service層:
spring-service.xml:所有的service實現類都放到spring容器中管理。並由spring管理事務。
3、表現層:
Springmvc框架,由springmvc管理controller。
Springmvc的三大組件。

 

3.5 加入配置文件

首先來張圖,有圖有真相,打馬賽克部分暫時用不到,在src下創建resources文件夾... 如下圖所示依次創建。

mybatis——mybatis配置

spring——spring+springmvc+spring和mybatis配置

jdbc.properties——數據庫配置文件

log4j.properties——log日志

補充spring包下的文件配置有個知識點需要注意一下,因為spring的配置太多,在這里分為spring-dao、spring-service、spring-mvc(相當於spring-web,本身springmvc就是起到web層的作用)三層,通常我們在別人的項目里看到的就只有spring-context、spring-mvc,其實你可以理解成:sprig-context = spring-dao + spring-service ,我拆分開只是為了更加直觀,無論是拆成幾個,本身內容是不會變的,只要最后在web.xml把文件配置進去能夠實例化即可,無須糾結。

 

如下xml順序不分排名,按照上方截圖依次提供

1、mybatis-config(mybatis配置)

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5 
 6     <!-- 別名 -->
 7     <!-- 起別名后,不用在mappring resultType 填寫全類名 -->
 8     <typeAliases>
 9         <package name="com.jeenotes.ssm.pojo"/>
10     </typeAliases>
11 
12 </configuration>
View Code

2、spring-dao(持久層,spring和mybatis的結合)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8     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
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10 
11     <!-- 配置 讀取properties文件 jdbc.properties -->
12     <context:property-placeholder location="classpath:resources/jdbc.properties" />
13     
14     <!-- 配置 數據源 -->
15     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16         <property name="driverClassName" value="${jdbc.driver}" />
17         <property name="url" value="${jdbc.url}" />
18         <property name="username" value="${jdbc.username}" />
19         <property name="password" value="${jdbc.password}" />
20     </bean>
21     
22     <!-- 配置SqlSessionFactory -->
23     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
24         <!-- 設置MyBatis核心配置文件 -->
25         <property name="configLocation" value="classpath:resources/mybatis/mybatis-config.xml" />
26         <!-- 設置數據源 -->
27         <property name="dataSource" ref="dataSource" />
28         <!-- 它表示我們的Mapper文件存放的位置,當我們的Mapper文件跟對應的Mapper接口處於同一位置的時候可以不用指定該屬性的值。 -->
29         <property name="mapperLocations" value="classpath:/mappings/**/*.xml" />
30         <!-- 那么在Mapper文件里面就可以直接寫對應的類名 而不用寫全路徑名了  -->
31         <!-- 跟mybatis中<typeAliases>作用一樣 -->
32         <!-- <property name="typeAliasesPackage" value="com.jeenotes.ssm.pojo"/> -->
33     </bean>
34     
35     <!-- 配置Mapper掃描 -->
36     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
37         <!-- 設置Mapper掃描包 -->
38         <property name="basePackage" value="com.jeenotes.ssm.dao" />
39     </bean>
40     
41 </beans>
View Code

3、spring-service(配置service掃描)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8     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
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10 
11     <!-- 配置Service掃描 -->
12     <context:component-scan base-package="com.jeenotes.ssm.service" />
13 </beans>
View Code

4、spring-mvc

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9     <!-- 配置Controller掃描 -->
10     <context:component-scan base-package="com.jeenotes.ssm.controller" />
11     
12     <!-- 配置注解驅動 -->
13     <mvc:annotation-driven />
14     
15     <!-- 對靜態資源放行  -->
16     <mvc:resources location="/css/" mapping="/css/**"/>
17     <mvc:resources location="/js/" mapping="/js/**"/>
18     <mvc:resources location="/fonts/" mapping="/fonts/**"/>
19     <mvc:resources location="/frame/" mapping="/frame/**"/>
20     <mvc:resources location="/images/" mapping="/images/**"/>
21     <mvc:resources location="/style/" mapping="/style/**"/>
22     <!-- 配置視圖解析器 -->
23     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
24         <!-- 前綴 -->
25         <property name="prefix" value="/WEB-INF/jsp/" />
26         <!-- 后綴 -->
27         <property name="suffix" value=".jsp" />
28     </bean>
29 </beans>
30     
View Code

5、jdbc.properties

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
3 jdbc.username=root
4 jdbc.password=****

6、log4j.properties

1 # Global logging configuration
2 log4j.rootLogger=DEBUG, stdout
3 # Console output...
4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

3.6 試搭建環境

到這里,其實整個框架的搭建已經基本了,畢竟整點就是上邊這堆配置文件嘛,接下來,就是測試一下了。

是時候讓你看一下部分馬賽克了,參照如下架構可自行創建包

com.jeenotes.ssm:存放control、dao、pojo、service

 

隨便創建一個control,可參考如下:

@Controller
@RequestMapping(value = "user")
public class LoginControl {
    
    @RequestMapping("login")
    public String dologin() {
        return "index";
    }

}

然后再創建一個index.jsp,jsp文件最好放在WEB-INF目錄下,至於為何,自行百度。

在這我就直接放在外面了,其實盡管不去請求url,項目運行同樣會進入index.jsp,原因就在於web.xml配置文件中這段配置,

welcome-file-list是一個配置在web.xml中的一個歡迎頁,用於當用戶在url中輸入工程名稱或者輸入web容器url(如http://localhost:8080/jeenotes-ssm/)時直接跳轉的頁面.

<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>

程序運行后,在瀏覽器訪問http://localhost:8080/jeenotes-ssm/user/login 同樣可以訪問到index.jsp證明測試成功。

3.7 SSM框架應用實例

如上只是對web層的一次請求測試,接下來就是對整個ssm環境進行測試了,在這我們通過一個例子繼續。

待補充

SSM(Spring+SpringMVC+Mybatis)框架環境搭建(應用實例)(二)

 

項目地址:https://gitee.com/niceyoo/jeenotes-ssm 

Maven版本改造地址https://www.cnblogs.com/niceyoo/p/12960903.html

我創建了一個java相關的公眾號,用來記錄自己的學習之路,感興趣的小伙伴可以關注微信公眾號:niceyoo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM