1. 程式人生 > >SSM(Spring+Spring MVC+Mybatis)框架搭建

SSM(Spring+Spring MVC+Mybatis)框架搭建

一. SSM框架架構及流程介紹

​ SSM框架,通常是指將spring mvc+spring+mybatis三個框架整合在一起進行工作,spring mvc本身就是spring的一部分,所以這兩者之間不用整合,這裡主要做的事情就是將spring與mybatis框架進行整合,實際上就是將SqlSessionFactory物件交由spring來管理,事務方面也交由spring來管理。

 

1、SSM架構

​ SSM框架主要分為bean/entity層,mapper/dao層,service層,controller層。

(1)bean層(也叫entity/pojo/ model層) 用於存放我們的實體類,與資料庫中的屬性值基本保持一致。在bean建立具體的實體類物件,便於其他層對實體類物件進行直接操作

(2)dao層(mapper層) 負責與資料庫進行聯絡的一些任務都封裝在此,具體到對於某個表、某個實體的增刪改查

(3)service層 service層主要負責業務模組的邏輯應用設計,可以細分為service介面和serviceImpl實現類

(4)controller層

​ 控制器,匯入service層,負責具體的業務模組流程的控制,在此層裡面要呼叫Service層的介面來控制業務流程

 

2、SSM流程

​ 呼叫流程: 前端JSP /html——> Controller ——> Service ——> Dao ——> 資料庫

    ​ controller層使用ajax+json

與前端的html檔案進行互動,獲取前端請求和返回資料給前端。前端頁面使用者請求通過ajax傳到controller層呼叫相應的方法處理,然後反饋資料到html檔案,把處理後資料顯示在頁面上,反饋給使用者。

​     controller 層在執行相應的方法時,實際上是去呼叫service層對應的介面,從而service層獲取實現類中的方法去完成對應業務,service層一般用事務來進行方法操作,保證操作的一致性。

​     service層實現類包含業務實現方法,也包含一些關於資料庫處理的操作,但不是直接和資料庫打交道。service層的方法如果需要操作資料庫,那麼就需要去呼叫dao層的介面。

    dao層中mybatis框架常用mapper動態代理,所以dao層介面會對應mapper對映檔案裡面相應的sql語句,從而實現對資料庫的操作。

    ​ 這裡還有bean層,bean層通常存放我們的實體類,與資料庫中的屬性值基本保持一致。在bean建立具體的實體類物件,便於其他層對實體類物件進行直接操作。最終在dao層中這些實體類物件的操作會直接對映到對應的sql語句,操作對應的資料庫裡面的資料。

​     反過來理解:啟動專案的時候會解析mapper.xml檔案,xml中有一個名稱空間namespace指向dao,用動態代理和反射技術會生成一個dao層的實現類。dao層用來操作資料庫,返回資料,service呼叫dao層獲取的資料,根據具體的業務做一些處理,controller層呼叫service層,一般用來接收前臺傳來的資料,再將從service層獲取的資料傳給前臺。

 

二. SSM框架搭建

​ 1、建立一個maven專案,在裡面新建常用的包,bean、controller、service、dao等;

​ 2、完成各配置檔案配置

​ (1)新增依賴,在工程的pom.xml檔案中新增專案需要的jar包(主要是spring mvc+spring+mybatis3個框架涉及的jar包)

<?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">
    <parent>
        <artifactId>learn</artifactId>
        <groupId>test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>_11_ssm_crud</artifactId>
    <packaging>war</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
            <version>2.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
            <version>2.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.4</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>_11_ssm_crud</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

​ (2)完成spring-mvc部分的配置

​ a. resouce資料夾下新增spring-mvc.xml檔案,完成spring-mvc部分配置檔案配置。在spring-mvc.xml完成註冊元件掃描器、檢視解析器、內部檢視解析器、靜態資源、解決返回json資料亂碼問題的配置。

<?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.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">


    <!--解決返回json資料亂碼問題-->
    <bean id="stringHttpMessageConverter"
          class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="stringHttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--靜態資源-->
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/html/**" location="/html/" />

    <!-- 註冊元件掃描器 ,元件掃描器只掃描controller包,不要掃描service等其他包,否則事務配置會失效-->
    <context:component-scan base-package="test.controller"/>

    <!-- 檢視解析器 -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

    <!--內部檢視解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/html/"/>
        <property name="suffix" value=".html"/>
    </bean>

</beans>

     b. 完成WEB-INF/web.xml配置

​ 完成指定spring配置檔案的路徑、註冊spring監聽器、註冊字符集過濾器、註冊spring mvc中央控制器的配置。

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--指定spring配置檔案的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-*.xml</param-value>
    </context-param>

    <!--註冊監聽器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置spring MVC配置項-->
    <!--字元編碼過濾器-->
    <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>

        <!--強制指定字元編碼,即如果在request中指定了字元編碼,那麼也會為其強制指定當前設定的字元編碼-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 註冊spring MVC中央控制器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <!-- spring MVC中的核心控制器 -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath: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>

</web-app>

    (3)完成mybatis部分的配置

​     a. 新增資料庫配置檔案db.properties

#驅動
jdbc.driver=com.mysql.jdbc.Driver  
#資料庫連線地址
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useSSL=false
#使用者名稱及資料庫密碼,這裡需要修改,每個資料庫的密碼都不一樣
jdbc.user=root
jdbc.password=mysql

 

    ​ b. 新增資料庫日誌配置檔案log4j.properties

log4j.rootLogger=debug,console
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{yyyy-MM-dd HH:mm:ss}]%m%n

 

    ​ c. 新增資料庫快取配置檔案ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
</ehcache>

 

   ​ d. 新增mybatis.xml配置檔案

這裡只需要註冊bean即可,mapper 對映檔案的註冊來使用spring的 MapperScannerConfigurer掃描器進行掃描,SqlSessionFactory交由spring管理,資料來源也交由spring管理,所以都不需要配置。

<?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="test.bean"/>
    </typeAliases>

</configuration>

    e. 新增spring-mybatis.xml配置檔案

​ 載入資料來源,將sqlsessionfactory交由spring管理,使用MapperScannerConfigurer將將Mapper介面生成代理物件。需要注意的是,在註冊元件掃描器的時候無需再掃描controller包下的類了,因為已經在springmvc的配置檔案中掃描過了。

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 載入配置檔案 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 註冊元件掃描器 -->
    <context:component-scan base-package="test">
        <!--不再掃描controller註解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"
    </context:component-scan>

    <!-- 資料庫連線池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>

    <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 資料庫連線池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 載入mybatis的全域性配置檔案 -->
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>

    <!-- 自動掃描 將Mapper介面生成代理物件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--Mapper檔案所在路徑-->
        <property name="basePackage" value="monmkey1024.dao" />
    </bean>


</beans>

    (4)完成spring部分的配置

​ 新增spring-tx.xml檔案進行事務的管理,採用AOP註解方式進行事務管理

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


    <!-- 事務管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 資料來源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--開啟註解事務驅動-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

 

​ 3、完成各層介面及實現類

​ (1)建立對應的資料庫表t_user,包含id(主鍵)、name、phone、address、birthday欄位

​ (2)bean層,建立User類--使用者類物件

package test1.bean;

import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;

public class User {
    private int id;
    private String name;
    private  String phone;
    private String address;
    @DateTimeFormat(pattern = "yyyy-mm-dd")
    private LocalDate birthday;

    public User() {
    }

    public User(String name, String phone, String address, LocalDate birthday) {
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.birthday = birthday;
    }

    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 String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }
}

 

    ​ (3)dao層,建立UserDao介面檔案和UserDao對應mapper檔案UserDao.xml

​     UserDao介面檔案

package test.dao;

import test.bean.User;

import java.util.List;

public interface UserDao {
    void addUser(User user);

    void updateUser(User user);

    List<User> selectUsers();

    User selectUserById(int id);

    void deleteUser(int id);
}

​     UserDao.xml(具體對資料庫操作的sql語句在此編輯)

<?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="test.dao.UserDao">

    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

    <insert id="addUser">
        INSERT INTO t_user
        (name,phone,address,birthday)
        VALUES
        (#{name},#{phone},#{address},#{birthday});

        <selectKey resultType="int" keyProperty="id" order="AFTER">
            SELECT @@identity
        </selectKey>
    </insert>

    <delete id="deleteUser">
        DELETE FROM t_user where id=#{id}
    </delete>

    <update id="updateUser">
        UPDATE t_user set name=#{name},phone=#{phone},address=#{address},birthday=#{birthday} where id=#{id}
    </update>

    <select id="selectUsers" resultType="user">
        SELECT id,name,phone,address,birthday FROM t_user
    </select>

    <select id="selectUserById" resultType="user">
        SELECT id,name,phone,address,birthday FROM t_user where id=#{id}
    </select>

</mapper>

    (4)service層:建立UserService介面和實現類

​     UserService介面

package test.service;

import test.bean.User;

import java.util.List;

public interface UserService {
    void addUser(User user);

    void updateUser(User user);

    List<User> selectUsers();

    User selectUserById(int id);

    void deleteUser(int id);
}

    ​ UserServiceImpl實現類()

package test.service.impl;

import test.bean.User;
import test.dao.UserDao;
import test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service  //用於對 Service 實現類進行註解,Spring會去自動建立UserServiceImpl的物件,並且通過註解,容器會查詢UserService型別的例項userService將其注入,也可以用@Component

@Transactional  //使用註解配置事務
public class UserServiceImpl implements UserService {


    @Autowired  //自動注入註解,該註解預設使用按型別自動裝配,即容器會查詢UserDao型別的例項將其注入,初始化其物件,然後可以直接使用物件中的方法如 userDao.addUser(user),不用再用new建立物件
    private UserDao userDao;

    @Override
    public void addUser(User user) {
        userDao.addUser(user);


    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);

    }

    @Override
    public List<User> selectUsers() {
        return userDao.selectUsers();
    }

    @Override
    public User selectUserById(int id) {
        return userDao.selectUserById(id);
    }

    @Override
    public void deleteUser(int id) {
        userDao.deleteUser(id);

    }
}

    (5)controller層:建立controller類,使用restful風格:

package test.controller;

import com.alibaba.fastjson.JSON;
import test.bean.User;
import test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //restful風格中spring mvc常用註解
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")  //該註解用來替代RequestMapping,特點是@GetMapping只處理get方式的請求,//註解後面要加上URL地址
    public String selectUsers(){

        List<User> users = userService.selectUsers();


        return JSON.toJSONString(users);
    }

    @GetMapping("/users/{id}")//註解後面要加上URL地址,地址中包含所需要的資源,比如這裡的id
    public String selectUserById(@PathVariable int id) {

        User user = userService.selectUserById(id);

        return JSON.toJSONString(user);
    }

    @PostMapping("/users")
    public String addUser(@RequestBody User user) {


        try {
            userService.addUser(user);

            return JSON.toJSONString("success");
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("fail");
        }

    }

    @PutMapping("/users/{id}")
    public String updateUser(@PathVariable int id, @RequestBody User user) {


        try {
            user.setId(id);
            userService.updateUser(user);

            return JSON.toJSONString("success");
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("fail");
        }
    }

    @DeleteMapping("/users/{id}")
    public String deleteUser(@PathVariable int id) {

        try {
            userService.deleteUser(id);

            return JSON.toJSONString("success");
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("fail");
        }
    }


}

 

​ 4、最後根據需要在webapp/html資料夾下建立需要的html前端檔案即可。

以上就是搭建SSM框架組合完成簡單增刪改查的功能,大家可以根據這個思路自己去實踐下。

 

demo檔案後續會附上。

參考資料:

https://www.cnblogs.com/wmyskxz/p/8916365.html

相應的