1. 程式人生 > 實用技巧 >使用IDEA整合SSM框架

使用IDEA整合SSM框架

一、SSM的基本說明和概念

1.1 什麼是SSM

SSM是SpringMVC+Spring+Mybatis的簡稱。其中SpringMVC用來處理和使用者請求,返回請求,是一個MVC框架;Mybatis是一個持久層框架,用來將資料儲存到資料庫;
Spring則是一個輕量級的java框架,主要功能是用來實現IOC和Aop,在這裡他用來當做兩個框架的粘合劑。

1.2 整合的思路

從上面的分析可以得到一個簡單的框架示意圖

下面我們就按照這個思路進行整合。

二、建立專案,並搭建專案架構

3.1 建立專案

建立一個maven的基架專案,如下圖所示:

然後填寫自己的groupid,然後一直next就可以了,然後專案就建立完畢了,如圖所示:

3.2 在pom檔案中引用包,填寫專案目錄

(1)引用的所有包如下:

  <properties>
    <spring.version>5.2.6.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.4</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.5</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-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.11.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.11.1</version>
    </dependency>
  </dependencies>

(2)搭建檔案目錄
按照java的約定,將java資料夾設定為原始碼目錄,然後在該資料夾下建立我們的包名;接著分別建立dao、service、controller、domain等資料夾,將我們的專案分層;如下圖所示:

三、搭建SpringMVC框架,並整合spring

3.1 搭建SpringMVC框架

修改webapp/WEB-INF資料夾下的web.xml檔案;
(1)配置servlet,servlet用來攔截使用者的所有請求,將使用者所有的請求轉到我們的controller中處理

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--載入spring-mvc配置檔案-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

在resource資料夾下建立spring-mvc配置檔案,該檔案配置了開啟mvc支援,controller所在的類,檢視檔案所在資料夾等;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--啟用mvc支援-->
    <mvc:annotation-driven/>

    <!--配置掃描包,只掃描controller註解-->
    <context:component-scan base-package="org.study.controller">
    </context:component-scan>
    
    <!--配置檢視解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix"  value=".jsp"/>
    </bean>
    <!--配置mvc靜態資源-->
    <mvc:resources mapping="/js/**" location="/js/"/>
</beans>

(2)配置filter,解決中文亂碼問題

  <!--配置中文亂碼-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

(3)驗證mvc的執行
修改一個HelloController中程式碼,如下:

package org.study.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(path = "/test")
public class HelloController {
    @RequestMapping(path = "/hello")
    public String hello(){
        return "hello";
    }
}

在WEB-INF資料夾下新建一個pages資料夾(因為spring-mvc.xml配置了檢視所在的資料夾),並建立一個hello.jsp檔案,如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>hello mvc</h3>
</body>
</html>

執行專案,可以看到mvc執行成功:

3.2 搭建Spring框架

上面我們已經成功的讓專案執行起來,接下來我們整合spring框架,讓spring框架去管理我們的dao、service層
(1)在web.xml中載入spring-batis.xml
在web.xml新增listener,載入spring的配置檔案,如下:

  <!--配置listener,載入spring的配置檔案-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>

(2)建立spring-mybatis.xml
在resources資料夾下建立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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:ex="http://www.springframework.org/schema/tx"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--配置spring管理的包,管理service層的包,不管理controller-->
    <context:component-scan base-package="org.study.service">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

此步驟配置掃描的包為dao層、和service層,不管理controller層;
至此我們編寫一個簡單的測試方法,讓controller層呼叫service層,示例程式碼如下:
service程式碼:

@Service(value = "accountService")
public class AccountServiceImpl implements IAccountService {
    @Override
    public List<Account> getAccountList() {
        System.out.println("getAccountList service層呼叫了");
        return null;
    }
}

controller程式碼:

@Controller
@RequestMapping(path = "/test")
public class HelloController {
    @Autowired
    private IAccountService accountService;
    @RequestMapping(path = "/hello")
    public String hello() {
        accountService.getAccountList();
        return "hello";
    }
}

執行結果如下:

至此,我們的spring和springmvc的整合已經完成;

四、使用spring框架,整合mybatis框架

4.1 在spring-mybatis.xml中配置mybatis,配置檔案如下:

    <!--讀取資料庫的配置檔案-->
    <context:property-placeholder location="classpath:jdbcConfig.properties"/>
    <!--配置連線池-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置sqlsessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置dao所在的包-->
    <bean name="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="org.study.dao"/>
    </bean>

    <!--配置事務-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

在resources資料夾下建立jdbcConfig.properties檔案,裡面配置資料庫相關內容:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testDB?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

至此我們的spring整合mybatis已經完成,接下來我們查詢資料庫資料,並展示到頁面上;

4.2 查詢資料並驗證

最後,整個專案的目錄結構如下

(1)dao層使用mybaits查詢,程式碼如下

@Repository("accountDao")
public interface IAccountDao {
    @Select("select  * from userAccount")
    List<Account> getAccountList();
}

(2)service層程式碼如下:

@Service(value = "accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;
    @Override
    public List<Account> getAccountList() {
        System.out.println("getAccountList service層呼叫了");
        return accountDao.getAccountList();
    }
}

(3)controller和頁面如下

@Controller
@RequestMapping(path = "/test")
public class HelloController {
    @Autowired
    private IAccountService accountService;
    @RequestMapping(path = "/hello")
    public String hello(Model model) {
        List<Account> accounts = accountService.getAccountList();
        model.addAttribute("accounts",accounts);
        return "hello";
    }
}

頁面如下:

<%@ page contentType="text/html;charset=UTF-8" language="java"  %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>hello mvc</h3>
<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>money</td>
    </tr>
<c:forEach items="${accounts }" var="account">
    <tr>
        <td>${account.id }</td>
        <td>${account.name }</td>
        <td>${account.money }</td>
    </tr>
</c:forEach>
</table>
</body>
</html>

(4)驗證訪問結果

最終,我們的三個框架整合完畢,可以正常的工作了。

五、小結

1、整個專案的請求流程圖如下

通過上圖,我們可以清晰的看到各個流程對應程式碼中的層;
2、SSM框架主要是整合springmvc和mybatis
3、參考文章:
(1)https://www.cnblogs.com/wmyskxz/p/8916365.html