1. 程式人生 > >最新版的SSM框架spring5.0搭建教程(附原始碼)

最新版的SSM框架spring5.0搭建教程(附原始碼)

用SSM框架已經有一段時間了,但都沒有完整地搭建過一次工程。前段時間比較有時間就自己試著搭建了一下,差不多都是用的最新的spring版本了,然後就在這個基礎上做了很多的實驗測試。最近想著還是記錄一下整個搭建的過程,以便於自己日後參考。所以就重新翻新了一下,為了寫得儘可能地詳細點所以內容有點多。

GitHub的原始碼地址:SSMDemo GitHub原始碼

我是用的idea這個軟體搭建的,我也一直使用的這個軟體,非常好用,很適合做Java的朋友。

1.建立一個maven的web工程。

為了方便我儘可能地上截圖了。

建立maven的web工程

專案名稱

配置maven

上面的操作就完成了建立工程的基本操作了。

2.建立工程的包結構

建立好各層的測試類和常規的配置檔案

整個工程的包結構就建立完成了。

3.導包,這是非常重要的一步,需要將我們使用的spring、springMVC和MyBatis的相關包都導進來,直接使用maven依賴。

直接上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.viking.SSDemo</groupId>
  <artifactId>SSMDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SSMDemo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <!--spring版本號,5.0.0RELEASE是2017年9月釋出的新版本-->
    <spring.version>5.0.0.RELEASE</spring.version>
    <!--mybatis版本號,3.4.5是2017年8月釋出的新版本-->
    <mybatis.version>3.4.5</mybatis.version>
    <!--<slf4j.version>1.7.25</slf4j.version>-->
    <slf4j.version>1.7.25</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核心包——Start-->
    <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>
      <scope>test</scope>
    </dependency>
    <!--spring核心包——End-->
    <!--MyBatis核心包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.6</version>
    </dependency>
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper</artifactId>
      <version>3.3.8</version>
    </dependency>
    <!--mybatis/spring包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!--javaEE版本8.0-->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>8.0</version>
      <scope>provided</scope>
    </dependency>
    <!--mysql資料庫的jdbc連線包-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <!--新版本使用最多的是6.0.6  但在我的配置中無法使用該版本,報一個很無解的異常-->
      <version>5.1.39</version>
    </dependency>
    <!-- 匯入dbcp的jar包,用來在applicationContext.xml中配置資料庫 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!--jstl標籤包-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!--日誌檔案管理包——logStart-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
      <!--<scope>test</scope>-->
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!--日誌檔案管理包——logEnd-->
    <!--格式化物件,方便輸出日誌-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.42</version>
    </dependency>
    <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.3</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.11</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>SSMDemo</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.0.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.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</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>

然後等待idea自動下載一堆jar的包,如果沒有自動下載的可以點這裡:

所有的包下載完成後控制檯會有提示的。

*4.配置spring、spring和MyBatis的整合配置檔案,最重要的一步也是最麻煩的一步同樣也是最難的一步。不過不要怕,我們一步一步慢慢來。

這裡內容有點多,就不多說啥了,直接粘程式碼,需要的複製過去就OK了。

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: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/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--載入配置檔案-->
    <context:property-placeholder location="classpath:config/jdbc.properties"/>
    <!--配置資料來源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--spring和mybatis整合-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.**.model"/>
        <property name="mapperLocations" value="classpath*:mapper/UserMapper.xml"/>
        <!--<property name="configLocation" value="classpath:config/mybatis-config.xml"/>-->
    </bean>
    <!--掃描mapper介面包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.**.mapper"/>
        <property name="sqlSessionFactoryBeanName" >
            <idref bean="SqlSessionFactory"/>
        </property>
    </bean>
    <!--service實現類掃描-->
    <context:component-scan base-package="com.**.service"/>
    <!--事務管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--啟用spring的一些annotation -->
    <context:annotation-config/>
    <!--註冊驅動-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 配置Fastjson支援 -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <!--<value>WriteMapNullValue</value>-->
                        <value>WriteDateUseDateFormat</value>
                        <value>QuoteFieldNames</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list><value>text/html;charset=UTF-8</value></list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--<mvc:default-servlet-handler/>-->
    <!--  自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器  -->
    <context:component-scan base-package="com.ssm.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--配置檢視解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <mvc:resources mapping="/lib/**" location="/lib"/>

    <!-- 對模型檢視名稱的解析,即在模型檢視名稱新增前後綴(如果最後一個還是表示資料夾,則最後的斜槓不要漏了) 使用JSP-->
    <!-- 預設的檢視解析器 在上邊的解析錯誤時使用 (預設使用html)- -->
    <!--<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
        <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
        <!--<property name="prefix" value="/WEB-INF/jsp/"/><!–設定JSP檔案的目錄位置–>-->
        <!--<property name="suffix" value=".jsp"/>-->
    <!--</bean>-->

    <!--  配置檔案上傳,如果沒有使用檔案上傳可以不用配置,當然如果不配,那麼配置檔案中也不必引入上傳元件包  -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--  預設編碼  -->
        <property name="defaultEncoding" value="utf-8"/>
        <!--  檔案大小最大值  -->
        <property name="maxUploadSize" value="10485760000"/>
        <!--  記憶體中的最大值  -->
        <property name="maxInMemorySize" value="40960"/>
    </bean>

</beans>

web.xml檔案也是需要配置的,原始碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <display-name>Archetype Created Web Application</display-name>
    <!--spring和mybatis的配置檔案-->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/config/spring-mybatis.xml</param-value>
    </context-param>

    <!-- 日誌記錄 -->
    <context-param>
      <!-- 日誌配置檔案路徑 -->
      <param-name>log4jConfigLocation</param-name>
      <param-value>classpath:config/log4j.properties</param-value>
    </context-param>
    <context-param>
      <param-name>log4jRefreshInterval</param-name>
      <param-value>60000</param-value>
    </context-param>

    <!-- Spring字符集過濾器 -->
    <filter>
      <filter-name>SpringEncodingFilter</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>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>SpringEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--spring監聽器-->
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--防止spring記憶體溢位監聽器-->
    <listener>
      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- springMVC核心配置 -->
    <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath:config/spring-mvc.xml
        </param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>

  <!--需要訪問js檔案時使用-->
  <!--<servlet-mapping>-->
    <!--<servlet-name>default</servlet-name>-->
    <!--<url-pattern>*.js</url-pattern>-->
  <!--</servlet-mapping>-->

</web-app>

log4j相信大家都不陌生,在專案中我們常常會用到日誌,因此非常有必要配置一下日誌資訊。

log4j.properties原始碼如下:

log4j.rootLogger = DEBUG,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 = %5p %d %C: %m%n

#每天列印一個日誌檔案
log4j.appender.File = org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=E:/logs/SSMDemo.log
log4j.appender.File.Encoding=UTF-8
#列印所有級別的日誌資訊
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d{ABSOLUTE} %5p %c{1}\:%L - %m%n

log4j.logger.com.opensymphony=ERROR
log4j.logger.org.springframework=ERROR

log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.PreparedStatement=INFO
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.java.sql.Statement=INFO

jdbc也是必不可少的一部分,畢竟資料都是來源於資料庫的嘛。

jdbc.properties原始碼如下:

#開發環境
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydatabase_test
jdbc.user=your username
jdbc.password=your password

到這裡我們的配置檔案就算說完了(當然這是最基礎的需要,SSM的功能十分強大,還可以嵌入很多很多的功能...)

5.依葫蘆畫瓢,編寫各層的功能類即可,controller層主要寫http請求的介面方法,service主要寫各種業務操作演算法等,mapper層也可以稱為dao層(我的習慣是寫成mapper層)主要是寫對映到資料庫的方法介面,**Mapper.xml檔案主要是寫mapper層中的介面功能的SQL語句實現。這樣的分層管理應該可以相容各種大大小小的專案了。

下面也貼上我的工程中各層的測試程式碼。

model實體類:

package com.ssm.model;

/**
 * created by viking on 2018/07/04
 * user實體類
 */

public class User {
    private int id;
    private String name;
    private int age;
    private String sex;

    public User() {
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

 

controller層:

 

package com.ssm.controller;

import com.ssm.model.User;
import com.ssm.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * created by viking on 2018/07/04
 * controller層介面類
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private  UserService userService;
    Logger log = Logger.getLogger(UserController.class);

    @RequestMapping("select")
    @ResponseBody
    public Object userTest(int id){
        System.out.println("測試成功~~"+id);
        List<User> user = userService.getUser(id);
        System.out.println(user.toString());
        log.info(user);
        return user;
    }
    @RequestMapping("addUser")
    public Object insertUser(int id,String name,int age,String sex){
            userService.insertUser(id,name,age,sex);
       return "OK";
    }
    @RequestMapping("selectAll")
    public Object selectAll(){
        return userService.selectAll();
    }
    @RequestMapping("update")
    public Object update(){
        return userService.update();
    }

}

service層(service層一般都是比較複雜的,因此我習慣分成介面和實現類兩部分)介面:

package com.ssm.service;

import com.ssm.model.User;

import java.util.List;
/**
 * created by viking on 2018/07/04
 * 業務層介面
 */
public interface UserService {
     List<User> getUser(int id);

     void insertUser(int id, String name, int age, String sex);

     List<User> selectAll();

     Object update();
}

service層實現類:

package com.ssm.service.impl;

import com.ssm.mapper.UserMapper;
import com.ssm.model.User;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * created by viking on 2018/07/04
 * 業務層介面實現類
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> getUser(int id) {
        Map map = new HashMap();
        map.put("id",id);
        return  userMapper.getUser(map);
    }

    public void insertUser(int id, String name, int age, String sex) {
        Map param = new HashMap();
        param.put("id",id);param.put("name",name);
        param.put("age",age);param.put("sex",sex);
        userMapper.insertUser(param);
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public Object update() {
        Map param = new HashMap();
        param.put("total",10);
        param.put("name","測試一");
        userMapper.update(param);
        return "OK";
    }
}

mapper層(dao層)介面:

package com.ssm.mapper;

import com.ssm.model.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;
/**
 * created by viking on 2018/07/04
 * dao層介面
 */
public interface UserMapper {
    List<User> getUser(@Param("param") Map map);

    void insertUser(@Param("param") Map param);

    List<User> selectAll();

    void update(@Param("param") Map param);
}

**Mapper.xml MaBatsis對映檔案配置檔案,相當於mapper層介面的實現類了:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.mapper.UserMapper">
    <select id="getUser" parameterType="java.util.Map" resultType="com.ssm.model.User">
        SELECT *
        FROM user
        where id =#{param.id}
    </select>
    <insert id="insertUser" parameterType="java.util.Map">
        INSERT INTO user (id,name,age,sex) values(#{param.id},#{param.name},#{param.age},#{param.sex})
    </insert>
    <select id="selectAll" resultType="com.ssm.model.User">
        SELECT * FROM USER
    </select>
    <update id="update" parameterType="java.util.Map">
        UPDATE USER  SET id=id-#{param.total},age=age+#{param.total}
        WHERE name=#{param.name}
    </update>
</mapper>

為了方便大家拷下程式碼後可以直接啟動,我把資料庫的sql語句和資料也貼上,我用的是mysql資料庫:

/*
Navicat MySQL Data Transfer

Source Server         : mysql
Source Server Version : 50173
Source Host           : localhost:3306
Source Database       : mydatabase_test

Target Server Type    : MYSQL
Target Server Version : 50173
File Encoding         : 65001

Date: 2018-06-15 15:41:23
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('2', '測試二', '22', '男');
INSERT INTO `user` VALUES ('3', '小二郎', '18', '男');
INSERT INTO `user` VALUES ('4', '二郎', '18', '男');
INSERT INTO `user` VALUES ('5', '馬英', '26', '女');
INSERT INTO `user` VALUES ('6', '王尼瑪', '30', '男');
INSERT INTO `user` VALUES ('7', '張三瘋', '109', '男');
INSERT INTO `user` VALUES ('8', '歐陽翻天', '76', '女');
INSERT INTO `user` VALUES ('9', '上官', '16', '女');
INSERT INTO `user` VALUES ('10', '李響', '30', '男');
INSERT INTO `user` VALUES ('11', '測試一', '0', '女');

我本地的資料庫表結構截圖:

到這裡就完全結束了,整個工程就可以直接運行了。

6.順帶提一下在idea中配置tomcat的過程(懂的朋友可以直接忽略~)

配置好了之後再將tomcat新增到server中:

好了~tomcat的配置也完成了。

7.下面貼上我的本機執行效果圖:

tomcat成功啟動:

通過瀏覽器訪問localhost:

再測試下我們的SSM工程裡測試類的正常工作截圖:

控制檯的輸出和日誌資訊:

該有的輸出都是正常的,再看一下我們儲存到本地的日誌檔案:

成功地儲存了我們工程的輸出日誌。

一晚上堅持一口氣寫完整個日誌,到這裡終於完美地結束了,如果大家覺得在下寫的記錄有幫助,可以給下支援和訂閱哦!

附上GitHub的原始碼地址:SSMDemo GitHub原始碼

如果有不對的地方,歡迎各位大佬指教,如果有不懂的地方,也歡迎大家留言,我們一起討論。