通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis)

項目效果預覽:

通過訪問控制層的test/dologin方法進入index.jsp,展示jsp中的“hello,ssm”內容

一、新建項目:

1.打開IDEA,新建一個Maven項目

2.點擊next,新建一個為ssmDemo的Maven項目(自己定義項目GroupId和ArtifactId)

3.配置本地Maven路徑

4.點擊Next,確認項目信息,配置項目名稱和項目路徑,點擊Finish按鈕即可創建項目

5.項目創建完成,目錄結構如下:

註:如項目目錄中未包含src,則項目還在初始化,註意網絡連接,稍等片刻即可

二、補充項目結構:

1.在main文件夾下面創建java和resources文件夾

2.使java文件夾為項目資源文件夾(Sources root)

3.使resources文件夾為Resources root

4.在src/java目錄下面創建com包,在com包創建controller、dao、po、service、service.impl包

三、完善pom.xml(添加項目依賴–完整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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>com.demo</groupId>
 <artifactId>ssmDemo02</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 
 <name>ssmDemo02 Maven Webapp</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>
 
 <properties>
  <!--編譯編碼-->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <!--maven版本管理-->
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <!-- spring版本管理
  當對spring進行版本控制後,
  maven導入的jar包就不需要關心版本的問題,
  因為會自動匹配符合當前版本控制版本的jar包
   ,導入方式如下:${spring.version}-->
  <spring.version>5.0.2.RELEASE</spring.version>
  <!-- log4j日志文件管理包版本 -->
  <slf4j.version>1.6.6</slf4j.version>
  <log4j.version>1.2.12</log4j.version>
  <shiro.version>1.2.3</shiro.version>
  <!-- mysql連接驅動版本號 -->
  <mysql.version>5.1.6</mysql.version>
  <!-- mybatis版本號 -->
  <mybatis.version>3.4.5</mybatis.version>
  <spring.security.version>5.0.1.RELEASE</spring.security.version>
 </properties>
 
 
 <dependencies>
  <!-- Mybatis 和 mybatis 與 spring 的整合 -->
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>${mybatis.version}</version>
  </dependency>
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>1.3.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</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-aop</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <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-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</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-orm</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.6.8</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <!--junit測試-->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  <!--mysql-->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.version}</version>
  </dependency>
  <!--servlet-->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.0</version>
   <scope>provided</scope>
  </dependency>
  <!--jstl-->
  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <!-- log -->
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</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>
  <!--alibaba連接池-->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.9</version>
  </dependency>
 </dependencies>
 
 <build>
  <finalName>ssmDemo02</finalName>
  <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
   <plugins>
    <plugin>
     <artifactId>maven-clean-plugin</artifactId>
     <version>3.1.0</version>
    </plugin>
    <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    <plugin>
     <artifactId>maven-resources-plugin</artifactId>
     <version>3.0.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.0</version>
    </plugin>
    <plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.22.1</version>
    </plugin>
    <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <version>3.2.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-install-plugin</artifactId>
     <version>2.5.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-deploy-plugin</artifactId>
     <version>2.8.2</version>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>
</project>

效果圖:

註1:代碼復制完成後,選擇右下角的Import Changes

註2:如Import Changes後External還未出現jar包,則選中項目,右鍵,選擇maven,Generate Sources…

四、為項目導入jar包

1.選擇項目右鍵 — open Module Setting

2.選擇Artifact

3.選擇項目名,選擇outPut LayOut,選擇Available Elements — 選擇項目名稱 — 右鍵 — put into…

效果圖(WEB-INF中添加瞭lib目錄):

五、完善target文件夾

1.選擇右側側欄的maven

2.選擇Lifecycle — 選擇clean(雙擊)

3.選擇Lifecycle — 選擇install(雙擊)

4.效果圖

六、測試項目

1.添加tomcat(run –> editConfigurations –> 點擊+ –> 選擇tomcat– local)

2.配置Deployment(點擊Deployment — 選擇右下角+ — 選擇Artifact –選擇Artifa)

3.配置訪問路徑:/ssmDemotest

4.啟動tomcat,訪問:http://localhost:8080/ssmDemotest

七、編寫ssm框架所需xml

1.在src/resources文件夾下面添加mybatis包、spring包

2.在src/resources文件夾下面添加log4j.properties、db.properties

3.在mybatis包下添加:mybatis-config.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">
<configuration>
  <!-- 別名 -->
  <typeAliases>
    <package name="com.po" />
  </typeAliases>
</configuration>

4.在spring包下添加:spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    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/context http://www.springframework.org/schema/context/spring-context-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
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 
  <!-- 1.配置component-scan 掃描service,mapper包 -->
  <!-- 如不寫,則在控制層的@Resource註解則指定不到對應的service類 -->
  <context:component-scan base-package="com.service" />
  <context:component-scan base-package="com.dao" />
 
  <!-- 2.配置 讀取properties文件 jdbc.properties -->
  <context:property-placeholder location="classpath:db.properties" />
 
  <!-- 3.配置 數據源 -->
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>
 
  <!-- 4.配置SqlSessionFactory對象 -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 4.1.註入數據庫連接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 4.2.配置MyBaties全局配置文件:mybatis-config.xml -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    <!-- 4.3.掃描entity包 使用別名 -->
    <property name="typeAliasesPackage" value="com.po" />
    <!-- 4.4.掃描sql配置文件:mapper需要的xml文件
    <property name="mapperLocations" value="classpath:com/dao/*.xml" /> -->
  </bean>
 
  <!-- 5.配置Mapper掃描 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 5.1.註入sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <!-- 5.2.配置Mapper掃描 -->
    <property name="basePackage" value="com.dao" />
  </bean>
</beans>

5.在spring包下添加:spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    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/context http://www.springframework.org/schema/context/spring-context-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
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  <!-- 配置Service掃描 -->
  <context:component-scan base-package="com.service" />
</beans>

6.在spring包下添加:spring-mvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
 
  <!-- 1.配置Controller掃描 -->
  <context:component-scan base-package="com.controller" />
 
  <!-- 2.配置註解驅動 -->
  <mvc:annotation-driven />
 
  <!-- 3.配置視圖解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 4.1.前綴 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 4.2.後綴 -->
    <property name="suffix" value=".jsp" />
  </bean>
 
</beans>

7. 完善web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
     version="2.5">
 
 <display-name>Archetype Created Web Application</display-name>
 
 <!-- 1.加載spring容器 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/spring-dao.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!-- 2.springmvc前端控制器 -->
 <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:spring/spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- 3.設置匹配所有的url -->
 <servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
 </servlet-mapping>
 
</web-app>

八、啟動tomcat,測試xml文件是否正確

1.如啟動日志沒有錯誤信息,則xml添加無誤

2.如網址:http://localhost:8080/ssmDemotest 正常展示,則xml添加無誤

註:由於大部分代碼均為復制,可以重啟IDEA

九、controller層

1.在controller包新建類:TestController

2.在類頂部(導包語句下面)添加註解:@Controller,@RequestMapping(value = “test”)

3.編寫方法:

@RequestMapping("dologin")
public String dologin() {
  System.out.println("====test====");
  return "index";
}

十、JSP

1.在web-inf下創建目錄:jsp

2.在jsp文件下創建:index.jsp

3.在index.jsp下添加:hello,ssm

十一、測試

http://localhost:8080/ssmDemotest/test/dologin

到此這篇關於通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis)的文章就介紹到這瞭,更多相關IEAD+Maven搭建SSM項目內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: