天天看點

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

SSM整合記錄

提示:本文是自己測試ssm的環境搭建

文章目錄

  • SSM整合記錄
  • 前言
  • 一、建構項目結構
    • 1. 建立一個項目
    • 2. 完善目錄結構
  • 二、配置檔案
    • 1. 導入依賴檔案
    • 2. web.xml編寫
    • 3.編寫applicationContext.xml
    • 4. 編寫springmvc.xml
  • 三、編寫測試環境
    • 1. 資料庫建立
    • 2. 編寫實體類
    • 3. 編寫持久層接口
    • 4. 編寫業務層接口及其實作類
    • 5.編寫controller
    • 6.編寫前端html
  • 四、運作測試
    • 1. 部署項目
    • 2. 運作項目
  • 總結

前言

ssm整合是學習java開發過程中比較重要的學習點,通過spring架構整合springMVC與mybatis

使用工具:IDEA

一、建構項目結構

1. 建立一個項目

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

2. 完善目錄結構

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

在main下建立java和resources 并分别标記為源碼包和資源包

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

注意:在resources下建立兩個xml檔案,檔案名分别為:applicationContext.xml springmvc.xml

WEB-INF下建立views檔案夾

二、配置檔案

1. 導入依賴檔案

在pom.xml中添加依賴

内容如下:

<properties>
    <spring.version>5.0.2.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <mysql.version>5.1.6</mysql.version>
    <mybatis.version>3.4.5</mybatis.version>
  </properties>

  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</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-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-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-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <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>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 -->
    <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>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
  </dependencies>
           

引入:

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

完成後檢視外部庫

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

說明依賴添加成功

2. web.xml編寫

代碼如下(示例):

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
<display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
    <!-- 配置Spring的監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 配置加載類路徑的配置檔案 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>

           

3.編寫applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.kaier"> <!--base-package:注意與自己的保持一緻-->
        <!-- 配置要忽略的注解 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///ssm_test2" /> <!--資料庫名注意與自己的保持一緻-->
        <property name="username" value="root" /><!--資料庫賬戶注意與自己的保持一緻-->
        <property name="password" value="root" /><!--資料庫密碼注意與自己的保持一緻-->
    </bean>

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

    <!-- 配置掃描dao的包 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.kaier.dao"/><!--value:注意與自己的保持一緻-->
    </bean>

</beans>
           

4. 編寫springmvc.xml

提醒: 請按自己的情況修改配置檔案中的包名(注釋中已标出需要注意的地方)

代碼如下(示例):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--開啟注解掃描 (指定掃描的包)-->
    <context:component-scan base-package="com.kaier"/><!--base-package:注意與自己的保持一緻-->
    <!--配置視圖解析器對象(尋找到pages下的頁面)-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--解析之後尋找的頁面路徑-->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <!--檔案字尾名-->
        <property name="suffix" value=".html"></property>
    </bean>

    <!--開啟靜态資源-->
    <mvc:default-servlet-handler/>
    <!--開啟SpringMVC注解支援-->
    <mvc:annotation-driven/>
</beans>
           

三、編寫測試環境

1. 資料庫建立

代碼如下(示例):

CREATE DATABASE ssm_test2;
USE ssm_test2;
CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
age INT
);
           
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

2. 編寫實體類

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

代碼如下(示例):

import java.io.Serializable;
public class Account implements Serializable{
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

           

3. 編寫持久層接口

在dao下建立AccountDao接口

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

代碼如下(示例):

@Repository
public interface AccountDao {

    @Insert(value="insert into account (name,age) values (#{name},#{age})")
    public void saveAccount(Account account);

    @Select("select * from account")
    public List<Account> findAll();
}

           

4. 編寫業務層接口及其實作類

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

1. 接口部分(AccountService)代碼如下(示例):

public interface AccountService {
    public void saveAccount(Account account);
    public List<Account> findAll();
}

           

2. 接口實作類部分(AccountServiceImpl)代碼如下(示例):

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;


    public void saveAccount(Account account) {

    }

    public List<Account> findAll() {
        System.out.println("-----//業務層//----");

        return accountDao.findAll();
    }
}

           

5.編寫controller

在controller檔案夾下建立AccountController類

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

代碼如下(示例):

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accoutService;
    /**
     * 查詢所有的資料
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll() {
        System.out.println("表現層...");
        List<Account> list = accoutService.findAll();
        for (Account account : list) {
            System.out.println(account);
        }
        return "list";
    }
}

           

提醒: 粘貼時不要覆寫自己生成的package ;粘貼代碼後需要自己導包哦(快捷鍵alt+enter)

6.編寫前端html

注意不是jsp檔案,需将預設生成的jsp改為html
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
  1. 在index.html中添加查詢a标簽
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<a href="account/findAll">查詢使用者</a>
</body>
</html>

           
  1. 在views檔案夾下建立list.html(擷取回報)
    SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

           

四、運作測試

1. 部署項目

![在這裡插入圖檔描述](https://img-blog.csdnimg.cn/2020090413561034.png#pic_left)
           
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

2. 運作項目

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

控制台輸出:

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

浏覽器:

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

點選查詢後

SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結
SSM整合(快速搭建ssm環境)SSM整合記錄前言一、建構項目結構二、配置檔案三、編寫測試環境四、運作測試總結

總結

提示:這裡對文章進行總結:

以上就是今天複習的内容,本文是個人為了快速搭建ssm環境而記錄的,如果對你有幫助,點一個免費的贊呗!