java後端學習 myeclipse整合SSM框架
使用SSM(Spring、SpringMVC和Mybatis)已經有三個多月了,專案在技術上已經沒有什麼難點了,基於現有的技術就可以實現想要的功能,當然肯定有很多可以改進的地方。之前沒有記錄SSM整合的過程,這次剛剛好基於自己的一個小專案重新搭建了一次,而且比專案搭建的要更好一些。以前解決問題的過程和方法並沒有及時記錄,以後在自己的小專案中遇到我再整理分享一下。這次,先說說三大框架整合過程。個人認為使用框架並不是很難,關鍵要理解其思想,這對於我們提高程式設計水平很有幫助。不過,如果用都不會,談思想就變成紙上談兵了!!!先技術,再思想。實踐出真知。
1、基本概念
1.1、Spring
Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是為了解決企業應用開發的複雜性而建立的。Spring使用基本的JavaBean來完成以前只可能由
1.2、SpringMVC
Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裡面。Spring MVC 分離了控制器、模型物件、分派器以及處理程式物件的角色,這種分離讓它們更容易進行定製。
1.3、MyBatis
MyBatis 本是apache的一個開源專案iBatis, 2010年這個專案由apache software foundation 遷移到了google code,並且改名為MyBatis 。MyBatis是一個基於Java的持久層框架。iBATIS提供的持久層框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了幾乎所有的JDBC程式碼和引數的手工設定以及結果集的檢索。MyBatis 使用簡單的 XML或註解用於配置和原始對映,將介面和 Java 的POJOs(Plain Old Java Objects,普通的 Java物件)對映成資料庫中的記錄。
2、開發環境搭建
如果需要,參看之前的博文:http://blog.csdn.net/zhshulin/article/details/30779873
3、Maven Web專案建立
如果需要,參看之前的博文:http://blog.csdn.net/zhshulin/article/details/37921705
4、SSM整合
下面主要介紹三大框架的整合,至於環境的搭建以及專案的建立,參看上面的博文。這次整合我分了2個配置檔案,分別是spring-mybatis.xml,包含spring和mybatis的配置檔案,還有個是spring-mvc的配置檔案,此外有2個資原始檔:jdbc.propertis和log4j.properties。完整目錄結構如下(最後附上原始碼下載地址,不建議直接使用原始碼,因為此教程已經有了全部程式碼):
使用框架都是較新的版本:
Spring 4.0.2 RELEASE
Spring MVC 4.0.2 RELEASE
MyBatis 3.2.6
4.1、Maven引入需要的JAR包
為了方便後面說的時候不需要引入JAR包,我這裡直接給出所有需要的JAR包,這都是基本的JAR包,每個包的是幹什麼的都有註釋,就不再多說了。
pom.xml
-
<properties>
-
<!-- spring版本號 -->
-
<spring.version>4.0.2.RELEASE
</spring.version>
-
<!-- mybatis版本號 -->
-
<mybatis.version>3.2.6
</mybatis.version>
-
<!-- log4j日誌檔案管理包版本 -->
-
<slf4j.version>1.7.7
</slf4j.version>
-
<log4j.version>1.2.17
</log4j.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>junit
</groupId>
-
<artifactId>junit
</artifactId>
-
<version>4.11
</version>
-
<!-- 表示開發的時候引入,釋出的時候不會載入此包 -->
-
<scope>test
</scope>
-
</dependency>
-
<!-- spring核心包 -->
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-core
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-web
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-oxm
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-tx
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-jdbc
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-webmvc
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-aop
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-context-support
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-test
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<!-- mybatis核心包 -->
-
<dependency>
-
<groupId>org.mybatis
</groupId>
-
<artifactId>mybatis
</artifactId>
-
<version>${mybatis.version}
</version>
-
</dependency>
-
<!-- mybatis/spring包 -->
-
<dependency>
-
<groupId>org.mybatis
</groupId>
-
<artifactId>mybatis-spring
</artifactId>
-
<version>1.2.2
</version>
-
</dependency>
-
<!-- 匯入java ee jar 包 -->
-
<dependency>
-
<groupId>javax
</groupId>
-
<artifactId>javaee-api
</artifactId>
-
<version>7.0
</version>
-
</dependency>
-
<!-- 匯入Mysql資料庫連結jar包 -->
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
<version>5.1.30
</version>
-
</dependency>
-
<!-- 匯入dbcp的jar包,用來在applicationContext.xml中配置資料庫 -->
-
<dependency>
-
<groupId>commons-dbcp
</groupId>
-
<artifactId>commons-dbcp
</artifactId>
-
<version>1.2.2
</version>
-
</dependency>
-
<!-- JSTL標籤類 -->
-
<dependency>
-
<groupId>jstl
</groupId>
-
<artifactId>jstl
</artifactId>
-
<version>1.2
</version>
-
</dependency>
-
<!-- 日誌檔案管理包 -->
-
<!-- log start -->
-
<dependency>
-
<groupId>log4j
</groupId>
-
<artifactId>log4j
</artifactId>
-
<version>${log4j.version}
</version>
-
</dependency>
-
-
-
<!-- 格式化物件,方便輸出日誌 -->
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>fastjson
</artifactId>
-
<version>1.1.41
</version>
-
</dependency>
-
-
-
<dependency>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-api
</artifactId>
-
<version>${slf4j.version}
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-log4j12
</artifactId>
-
<version>${slf4j.version}
</version>
-
</dependency>
-
<!-- log end -->
-
<!-- 映入JSON -->
-
<dependency>
-
<groupId>org.codehaus.jackson
</groupId>
-
<artifactId>jackson-mapper-asl
</artifactId>
-
<version>1.9.13
</version>
-
</dependency>
-
<!-- 上傳元件包 -->
-
<dependency>
-
<groupId>commons-fileupload
</groupId>
-
<artifactId>commons-fileupload
</artifactId>
-
<version>1.3.1
</version>
-
</dependency>
-
<dependency>
-
<groupId>commons-io
</groupId>
-
<artifactId>commons-io
</artifactId>
-
<version>2.4
</version>
-
</dependency>
-
<dependency>
-
<groupId>commons-codec
</groupId>
-
<artifactId>commons-codec
</artifactId>
-
<version>1.9
</version>
-
</dependency>
-
-
-
</dependencies>
4.2、Spring與MyBatis的整合
所有需要的JAR包都引入以後,首先進行Spring與MyBatis的整合,然後再進行JUnit測試,先看一個專案結構圖:
4.2.1、建立JDBC屬性檔案
jdbc.properties(檔案編碼修改為utf-8)
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://10.221.10.111:8080/db_zsl
- username=demao
- password=demao
- #定義初始連線數
- initialSize=0
- #定義最大連線數
- maxActive=20
- #定義最大空閒
- maxIdle=20
- #定義最小空閒
- minIdle=1
- #定義最長等待時間
- maxWait=60000
4.2.2、建立spring-mybatis.xml配置檔案
這個檔案就是用來完成spring和mybatis的整合的。這裡面也沒多少行配置,主要的就是自動掃描,自動注入,配置資料庫。註釋也很詳細,大家看看就明白了。
spring-mybatis.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.sprin