【SSM】Maven創建web項目:SpringMVC+Mybatis

IDEA14創建Maven管理的SpringMVC+Mybatis,web項目

項目構建步驟

1、File->New->Project

勾選Create from archetype


點擊Next

2、輸入GroupId、ArtifactId


點擊Next


3、繼續點擊Next,輸入Project name


點擊Finish,完成基本項目創建


4、在src/main下添加java目錄作爲源文件目錄

在main上右鍵new Directory並命名爲java;

同時在Project Structure中,將java目錄設置爲Sources,然後Apply,點擊OK。


5、配置pom.xml

可以在maven倉庫進行相關依賴搜索:http://www.mvnrepository.com/

利用<dependency>元素進行項目所依賴的jar包配置,maven通過對pom.xml的配置使得不再需要導入jar包那麼麻煩


6、配置tomcat




 

7、資源文件編譯時一起打包到輸出目錄

    IDEA的maven項目中,默認源代碼目錄下的xml等資源文件並不會在編譯的時候一塊打包進classes文件夾,而是直接捨棄掉。

    項目中可能包含一些靜態資源如:mapping/*.xml,又或者一些spring的配置文件:spring.xml、spring-mvc.xml等等。這些靜態資源都要配置到target目錄下才能保證項目運行,否則會報找不到運行所需的這些文件。

target目錄設置,如下圖:


pom.xml在resources中配置靜態資源文件的目錄:

<resources>
    <!-- 該配置很重要,用於將directory下的資源複製到classpath下面。即target/classes下面-->
    <resource>
        <targetPath>whu/edu/irlab/mapping/</targetPath>
        <directory>src/main/java/whu/edu/irlab/mapping/</directory>
        <includes>
            <include>*.xml</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

在pom.xml配置後可以看見target目錄如下,配置的靜態資源文件被包含進來了


 

 

MavenDemo簡單實例

1、項目結構


 

注:  在WEB-INFO/lib下存放有jstl.jar和standard.jar,需要將其導入項目的Libraries中(一般使用Maven不這樣做,直接在pom.xml裏面配置響應的jar包依賴就行,當時自動填補依賴有些問題故採取比較麻煩的方式做)

如圖:


2、pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>MavenDemo</name>
  <groupId>MavenDemo</groupId>
  <artifactId>MavenDemo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
      <!-- spring start-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>
      <!-- spring end-->

      <!-- mybatis start-->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.2.8</version>
      </dependency>

      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
      </dependency>
      <!-- mybatis end-->

      <!-- mysql connector start-->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.34</version>
      </dependency>
      <!-- mysql connector end-->

      <!-- junit start-->
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
      </dependency>
      <!-- junit end-->

      <!-- 阿里巴巴數據源包 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.0.2</version>
      </dependency>
  </dependencies>
</project

發佈了96 篇原創文章 · 獲贊 218 · 訪問量 64萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章