1. 程式人生 > >[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)

[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)

博文裡紅底白字的,為注意修改的地方,在這裡先宣告

開發環境:

MySQL 5.0.11-dev(官網下載需要賬號登入,故不提供,請自行百度)

1、基本概念

1.1、Spring

Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是為了解決企業應用開發的複雜性而建立的。Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限於伺服器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用都可以從Spring中受益。 簡單來說,Spring是一個輕量級的控制反轉(IoC)和麵向切面(AOP)的容器框架。

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、開發環境搭建


3、Maven Web專案建立

參看博文:(作者:shu_lin)(博文最後一步可以不用設定,Maven->Update Project...會自動設定)

補充:(可跳過)

新建的Maven,Java Resources只有resource

可以新增Server Library自動生成

右擊專案->Properties->Java Build Path->Add Library...->Server Library


4、SSM整合

下面主要介紹三大框架的整合,至於環境的搭建以及專案的建立,參看上面的博文。這次整合我分了2個配置檔案,分別是spring-mybatis.xml,包含spring和mybatis的配置檔案,還有個是spring-mvc的配置檔案,此外有2個資原始檔:jdbc.propertis和log4j.properties。

使用框架都是較新的版本:

Spring 4.0.2 RELEASE(PS:無需手動下載,Maven會自動下)

Spring MVC 4.0.2 RELEASE(PS:無需手動下載,Maven會自動下)

完整目錄結構如下:

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>
<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
複製程式碼

4.2、Spring與MyBatis的整合

所有需要的JAR包都引入以後,首先進行Spring與MyBatis的整合,然後再進行JUnit測試,先看一個專案結構圖:

 

4.2.1、建立JDBC屬性檔案

jdbc.properties(檔案編碼修改為utf-8)

複製程式碼
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
#定義初始連線數
initialSize=0
#定義最大連線數
maxActive=20
#定義最大空閒
maxIdle=20
#定義最小空閒
minIdle=1
#定義最長等待時間
maxWait=60000
複製程式碼

4.2.2、Log4j的配置(跳過此步也沒影響)

為了方便除錯,一般都會使用日誌來輸出資訊,Log4j是Apache的一個開放原始碼專案,通過使用Log4j,我們可以控制日誌資訊輸送的目的地是控制檯、檔案、GUI元件,甚至是套介面伺服器、NT的事件記錄器、UNIX Syslog守護程序等;我們也可以控制每一條日誌的輸出格式;通過定義每一條日誌資訊的級別,我們能夠更加細緻地控制日誌的生成過程。
Log4j的配置很簡單,而且也是通用的,下面給出一個基本的配置,換到其他專案中也無需做多大的調整,如果想做調整或者想了解Log4j的各種配置,參看我轉載的一篇博文,很詳細: (作者:shu_lin)


log4j.properties(檔案編碼修改為utf-8)

複製程式碼
#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File
#定義日誌輸出目的地為控制檯
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日誌輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#檔案大小到達指定尺寸的時候產生一個新的檔案
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File = logs/springmvcMybist/ssm.log
#定義檔案最大大小
log4j.appender.File.MaxFileSize = 10MB
#輸出所以日誌,如果換成DEBUG表示輸出DEBUG以上級別日誌
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
複製程式碼

4.2.3、建立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-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自動掃描 -->
    <context:component-scan base-package="com.ssm" />

    <!-- 引入配置檔案 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化連線大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 連線池最大數量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 連線池最大空閒 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 連線池最小空閒 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 獲取連線最大等待時間 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置對映檔案 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自動掃描mapping.xml檔案 -->
        <property name="mapperLocations" value="classpath:com/ssm/mapping/*.xml"></property>
    </bean>

    <!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>
複製程式碼

4.2.4、JUnit測試

經過以上步驟(log4j不配也沒影響),我們已經完成了Spring和mybatis的整合,這樣我們就可以編寫一段測試程式碼來試試是否成功了。

4.2.4.1、建立測試用表

既然我們需要測試,那麼我們就需要建立在資料庫中建立一個測試表,這個表建的很簡單,SQL語句為:

MySQL

按 Ctrl+C 複製程式碼 按 Ctrl+C 複製程式碼

4.2.4.2、利用MyBatis Generator自動建立程式碼

隨便找個位置建資料夾(路徑最好都是英文),然後在裡面建個src資料夾,下載下面那兩個包,將generatorConfig.xml拷進去,最後開啟cmd,進到資料夾,執行下面的命令,程式碼就會生成到src資料夾裡

generatorConfig.xml

按 Ctrl+C 複製程式碼按 Ctrl+C 複製程式碼

cmd命令

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

可做成.bat檔案備用,放在目錄執行即可

cmd命令

set current_path="%cd%"
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
pause
exit

這個可根據表自動建立實體類、MyBatis對映檔案以及DAO介面,完成後將檔案複製到工程中。如圖:

IUserService.java

複製程式碼
package com.ssm.service;

import com.ssm.pojo.User;

public interface IUserService {

    public User getUserById(int userId);
}
複製程式碼

UserService.java

複製程式碼
package com.ssm.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssm.dao.UserMapper;
import com.ssm.pojo.User;
import com.ssm.service.IUserService;

@Service("userService")
public class UserService implements IUserService {

    @Resource
    private UserMapper userDao;

    public User getUserById(int userId) {
        return this.userDao.selectByPrimaryKey(userId);
    }

}
複製程式碼

4.2.4.4、建立測試類

測試類在src/test/java中建立,下面測試類中註釋掉的部分是不使用Spring時,一般情況下的一種測試方法;如果使用了Spring那麼就可以使用註解的方式來引入配置檔案和類,然後再將service介面物件注入,就可以進行測試了。如果測試成功,表示Spring和Mybatis已經整合成功了。輸出資訊使用的是Log4j列印到控制檯。

TestMyBatis.java

複製程式碼
package com.ssm.testmybatis;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

            
           

相關推薦

[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)

博文裡紅底白字的,為注意修改的地方,在這裡先宣告 開發環境: MySQL 5.0.11-dev(官網下載需要賬號登入,故不提供,請自行百度) 1、基本概念 1.1、Spring Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的J

Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(轉發)

... with attr 輕量 過程 logger support 執行 model [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 來源:http://blog.csdn.net/zhshulin/

Maven搭建SSM框架(Spring+SpringMVC+MyBatis)

核心 suffix clas you info org 好項目 package span 一、概述:   Spring是一個輕量級開發框架,好比一個大工廠(容器),可以將所有對象的創建和依賴關系交給spring管理。   SpringMVC是一個基於mvc的web框架。sp

SSM框架Spring+SpringMVC+MyBatis——詳細整合教程

servle aps files framework l數據庫 建立 blank onf pin 摘要: 包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了幾乎所有的JDBC代碼和參數的手工設置以及結果集的... 摘要:

SSM框架——Spring+SpringMVC+Mybatis的搭建教程

一:概述SSM框架在專案開發中經常使用到,相比於SSH框架,它在僅幾年的開發中運用的更加廣泛。Spring作為一個輕量級的框架,有很多的拓展功能,最主要的我們一般專案使用的就是IOC和AOP。SpringMVC是Spring實現的一個Web層,相當於Struts的框架,但是比

SSM框架整合Maven工程整合Spring+Springmvc+Mybatis(詳細教程,附程式碼)

一、基本概念 1、Spring Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分

基於Maven工具搭建Spring+SpringMVC++MyBatis(ssm)框架步驟

目錄 1.使用Ecplise建立Maven工程: 2.構建SSM框架 3.日誌資訊:log4j 4.應用例項:使用者登入 5 資料庫MySQL 1.使用Ecplise建立Maven工程: 1.1 File -> New -> Other,在New視

maven構建ssmspring+springmvc+mybatis框架

建立maven專案使用idea或者eclipes建立maven web專案(具體步驟請參考其他部落格)pom.xml檔案1.在pom檔案中的url標籤下加入下面程式碼,方便以後更改jar包版本 <properties> <springframework

使用maven,實現ssmspring+springmvc+mybatis)三大框架的整合DEMO

剛進一家新公司,要求使用ssm三大框架,而且這個組合是現在的主流,所以在整合的同時將步驟一步一步記錄下來,方便以後的再次使用。 1.首先是建立一個Maven-project,具體操作請參考我的另一

多工程:基於MavenSSMSpring,SpringMvc,Mybatis)整合的web工程(中)

png 開始 版本 war mage ont 右鍵 調用 web工程 上篇用了單工程創建了SSM整合的web工程(http://www.cnblogs.com/yuanjava/p/6748956.html),這次我們把上篇的單工程改造成為多模塊工程 一:創建

SSMSpring+SpringMVC+Mybatis框架搭建詳細教程【附源代碼Demo】

oid rep images end 訪問靜態文件 into *** 寫到 where http://www.cnblogs.com/qixiaoyizhan/p/7751732.html 【前言】   應某網絡友人邀約,需要一個SSM框架的Demo作為基礎學習資料,於

ssmspring + Springmvc + mybatis框架整合 · 筆記

一、環境配置 材料準備: JDK1.8 Maven Tomcat7 Eclipse MySQL 1、下載完後的maven配置: (1)配置本地倉庫 :開啟conf資料夾中的 settings.xml 將藍下滑線中的內容複製出來填寫自己的本地倉庫地址 <

手把手教你如何從零開始在eclipse上搭起一個ssmspring+springMVC+myBatis框架

1.新建一個Maven專案 直接點選next: 選擇這個,這個是指javaWeb專案 輸入maven專案的座標 點選finish,建立專案完成 2.新增maven依賴並下載 找到剛建的maven專案下的pom.xml配置檔案,開啟: 接下來,在url和depe

使用maven搭建SSM專案(spring+springmvc+Mybatis)

本文介紹使用eclipse+maven搭建Spring+SpringMvc+Mybatis專案,以登陸為例: 1、建立maven專案 2、把maven專案變為動態網站 3、搭建spring+springmvc+Mybatis專案 1 建立maven專案 請點選

SSMSpring+SpringMVC+MyBatis)三大框架整合及遇到的各種問題

關於Maven安裝,很多詳細的配置在這裡不多說,更詳細的請參考http://www.tuicool.com/articles/Fru26n,這裡從使用Maven新建web專案開始,並解決各種問題,主要是本人遭遇的問題太多,認真記錄下,以防以後忘記。 第一步:新建Maven專

碼不停蹄(六):從零開始Java後臺開發,跳出寫Servlet+JDBC的坑,使用輕量級框架Spring+SpringMVC+MyBatis (SSM)

寫在前面:這篇文章是寫給剛剛接觸或者準備學習web/APP應用開發的同學的,分享我的後臺開發經驗,如果你對後臺開發沒有什麼概念,甚至剛剛學完Java的基礎語法,那麼請一定要仔細看我接下來提供的開發方案,這會幫你跳出很多坑,迅速找到入門後臺開發的路。 一、學點底

新巴巴運動網專案:SSMSpring+SpringMVC+mybatis框架的配置

新學的框架配置,先把配置過程記錄下來,有些不懂的地方以後再慢慢理解,本專案採用IDEA+Maven的方式建立,具體建立過程不再細說,下面從具體的配置檔案寫起。1.首先在web.xml裡配置spring監聽器:     方法:找包,在org.org.springframewor

基於mavenSSMspring+springMVC+mybatis)的例項

1、環境搭建 搭建maven環境,DOC下輸入mvn -v檢視是否配置成功maven環境。新建mvn工程選擇webapp,雙擊進入 工程結構: 2、自動生成程式碼 檔案結構: 生成方法: 本地路徑 D:\generator下

STS(Spring Tool Suite)下SSMSpring+SpringMVC+Mybatis框架搭建

wid eight ssm ima height 項目 環境 index 寫代碼 最近在用SSM框架做一個網站,就順便把自己要做的筆記需要了解的東西都寫了下來,看看對大家學習SSM框架有沒有幫助。 開發環境: 1、win10 64位 2、spring-tool-sui

STS(Spring Tool Suite)下SSMSpring+SpringMVC+Mybatis框架搭建(二)

搭建 div 圖片 control 網盤 ext lmap wid 方便 繼完成controller配置並使用controller實現頁面跳轉,現連接數據庫進行登錄。 在SSM框架中,使用Mybatis與數據庫連接,因此需要配置關於mybatis的配置。 廢話少說直接開始