主頁 > 後端開發 > SpringMVC學習筆記 - 第二章 - SSM整合案例 - 技術整合、統一結果封裝、統一例外處理、前后聯調、攔截器

SpringMVC學習筆記 - 第二章 - SSM整合案例 - 技術整合、統一結果封裝、統一例外處理、前后聯調、攔截器

2023-01-31 07:10:16 後端開發

【前置內容】Spring 學習筆記全系列傳送門:

  • Spring學習筆記 - 第一章 - IoC(控制反轉)、IoC容器、Bean的實體化與生命周期、DI(依賴注入)

  • Spring學習筆記 - 第二章 - 注解開發、配置管理第三方Bean、注解管理第三方Bean、Spring 整合 MyBatis 和 Junit 案例

  • Spring學習筆記 - 第三章 - AOP與Spring事務

SpingMVC 學習筆記全系列傳送門:

  • SpringMVC學習筆記 - 第一章 - 作業流程、Bean加載控制、請求與回應(引數接收與內容回傳)、RESTful
  • 【本章】SpringMVC學習筆記 - 第二章 - SSM整合案例 - 技術整合、統一結果封裝、統一例外處理、前后聯調、攔截器

目錄
  • 1、SSM整合
    • 1.1 流程分析
    • 1.2 整合配置
    • 1.3 功能模塊開發
    • 1.4 單元測驗
    • 1.5 PostMan 測驗(細節不表)
  • 2、統一結果封裝(前后端資料通信協議)
    • 2.1 表現層與前端資料傳輸協議定義
    • 2.2 表現層與前端資料傳輸協議實作
      • 2.2.1 思路分析
      • 2.2.2 結果封裝
  • 3、統一例外處理
    • 3.1 問題描述
    • 3.2 例外處理器的使用
      • 3.2.1 環境準備
      • 3.2.2 使用步驟
      • 3.2.3 相關知識點
        • 3.2.3.1 @RestControllerAdvice
        • 3.2.3.2 @ExceptionHandler
    • 3.3 專案例外處理方案
      • 3.3.1 例外分類
      • 3.3.2 例外解決方案
      • 3.3.3 例外解決方案的具體實作
  • 4、前后臺協議聯調
    • 4.1 環境準備
    • 4.2 串列功能
    • 4.3 添加功能
    • 4.4 添加、修改、洗掉功能狀態處理
    • 4.5 修改功能
    • 4.6 洗掉功能
  • 5、攔截器
    • 5.1 攔截器概念
    • 5.2 攔截器入門案例
      • 5.2.1 環境準備
      • 5.2.2 攔截器開發
      • 5.2.3 攔截器攔截規則
      • 5.2.4 簡化SpringMvcSupport的撰寫
    • 5.3 攔截器引數
      • 5.3.1 前置處理方法
      • 5.3.2 后置處理方法
      • 5.3.3 完成處理方法
    • 5.4 攔截器鏈運行順序

1、SSM整合

1.1 流程分析

1.2 整合配置

  1. 創建 Maven - web 專案

  2. 添加依賴

    <?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>priv.dandelion</groupId>
        <artifactId>08_ssm</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.10.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.2.10.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.2.10.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.6</version>
            </dependency>
    
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.0</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.16</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <port>80</port>
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
  3. 創建專案包結構

  4. SpringConfig配置類

    @Configuration
    @ComponentScan({"priv.dandelion.service"})
    @PropertySource("classpath:jdbc.properties")
    @Import({JdbcConfig.class,MybatisConfig.class})
    // 開啟事務
    @EnableTransactionManagement
    public class SpringConfig {
    }
    
  5. JdbcConfig配置類

    public class JdbcConfig {
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
    
        @Bean
        public DataSource dataSource(){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            return dataSource;
        }
    
        // 事務控制管理器,資料源使用自動裝配(由Spring管理)
        @Bean
        public PlatformTransactionManager transactionManager(DataSource dataSource){
            DataSourceTransactionManager ds = new DataSourceTransactionManager();
            ds.setDataSource(dataSource);
            return ds;
        }
    }
    
  6. Mybatis 配置類

    public class MybatisConfig {
        @Bean
        public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setTypeAliasesPackage("priv.dandelion.entity");
            return factoryBean;
        }
    
        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer(){
            MapperScannerConfigurer msc = new MapperScannerConfigurer();
            msc.setBasePackage("priv.dandelion.dao");
            return msc;
        }
    }
    
  7. jdbc.properties 組態檔

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm_db
    jdbc.username=root
    jdbc.password=123456
    
  8. SpringMvc 配置類

    @Configuration
    @ComponentScan("priv.dandelion.controller")
    @EnableWebMvc
    public class SpringMvcConfig {
    }
    
  9. web 專案入口配置類

    public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
        //加載Spring配置類
        protected Class<?>[] getRootConfigClasses() {
            return new Class[]{SpringConfig.class};
        }
        //加載SpringMVC配置類
        protected Class<?>[] getServletConfigClasses() {
            return new Class[]{SpringMvcConfig.class};
        }
        //設定SpringMVC請求地址攔截規則
        protected String[] getServletMappings() {
            return new String[]{"/"};
        }
    
        //設定post請求中文亂碼過濾器
        @Override
        protected Filter[] getServletFilters() {
                CharacterEncodingFilter filter = new CharacterEncodingFilter();
                filter.setEncoding("utf-8");
                return new Filter[]{filter};
        }
    }
    

1.3 功能模塊開發

  1. SQL

    create database ssm_db character set utf8;
    use ssm_db;
    create table tbl_book(
      id int primary key auto_increment,
      type varchar(20),
      name varchar(50),
      description varchar(255)
    )
    
    insert  into `tbl_book`(`id`,`type`,`name`,`description`) values (1,'計算機理論','Spring實戰 第五版','Spring入門經典教程,深入理解Spring原理技術內幕'),(2,'計算機理論','Spring 5核心原理與30個類手寫實踐','十年沉淀之作,手寫Spring精華思想'),(3,'計算機理論','Spring 5設計模式','深入Spring原始碼刨析Spring原始碼中蘊含的10大設計模式'),(4,'計算機理論','Spring MVC+Mybatis開發從入門到專案實戰','全方位決議面向Web應用的輕量級框架,帶你成為Spring MVC開發高手'),(5,'計算機理論','輕量級Java Web企業應用實戰','原始碼級刨析Spring框架,適合已掌握Java基礎的讀者'),(6,'計算機理論','Java核心技術 卷Ⅰ 基礎知識(原書第11版)','Core Java第11版,Jolt大獎獲獎作品,針對Java SE9、10、11全面更新'),(7,'計算機理論','深入理解Java虛擬機','5個緯度全面刨析JVM,大廠面試知識點全覆寫'),(8,'計算機理論','Java編程思想(第4版)','Java學習必讀經典,殿堂級著作!贏得了全球程式員的廣泛贊譽'),(9,'計算機理論','零基礎學Java(全彩版)','零基礎自學編程的入門圖書,由淺入深,詳解Java語言的編程思想和核心技術'),(10,'市場營銷','直播就這么做:主播高效溝通實戰指南','李子柒、李佳奇、薇婭成長為網紅的秘密都在書中'),(11,'市場營銷','直播銷講實戰一本通','和秋葉一起學系列網路營銷書籍'),(12,'市場營銷','直播帶貨:淘寶、天貓直播從新手到高手','一本教你如何玩轉直播的書,10堂課輕松實作帶貨月入3W+');
    
  2. 物體類

    public class Book {
        private Integer id;
        private String type;
        private String name;
        private String description;
        //getter...setter...toString省略
    }
    
  3. Dao 介面

    public interface BookDao {
    
        //    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
        @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
        public void save(Book book);
    
        @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
        public void update(Book book);
    
        @Delete("delete from tbl_book where id = #{id}")
        public void delete(Integer id);
    
        @Select("select * from tbl_book where id = #{id}")
        public Book getById(Integer id);
    
        @Select("select * from tbl_book")
        public List<Book> getAll();
    }
    
  4. Service

    • 介面

      // 宣告該類需要被事務管理
      @Transactional
      public interface BookService {
      
          public boolean save(Book book);
      
          public boolean update(Book book);
      
          public boolean delete(Integer id);
      
          public Book getById(Integer id);
      
          public List<Book> getAll();
      }
      
    • 實作類

      @Service
      public class BookServiceImpl implements BookService {
      
          @Autowired
          private BookDao bookDao;
      
          @Override
          public boolean save(Book book) {
              bookDao.save(book);
              return true;
          }
      
          @Override
          public boolean update(Book book) {
              bookDao.update(book);
              return true;
          }
      
          @Override
          public boolean delete(Integer id) {
              bookDao.delete(id);
              return true;
          }
      
          @Override
          public Book getById(Integer id) {
              return bookDao.getById(id);
          }
      
          @Override
          public List<Book> getAll() {
              return bookDao.getAll();
          }
      }
      
  5. Controller

    package priv.dandelion.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import priv.dandelion.dao.BookDao;
    import priv.dandelion.entity.Book;
    import priv.dandelion.service.BookService;
    
    import java.util.List;
    
    @Service
    public class BookServiceImpl implements BookService {
    
        @Autowired
        private BookDao bookDao;
    
        @Override
        public boolean save(Book book) {
            bookDao.save(book);
            return true;
        }
    
        @Override
        public boolean update(Book book) {
            bookDao.update(book);
            return true;
        }
    
        @Override
        public boolean delete(Integer id) {
            bookDao.delete(id);
            return true;
        }
    
        @Override
        public Book getById(Integer id) {
            return bookDao.getById(id);
        }
    
        @Override
        public List<Book> getAll() {
            return bookDao.getAll();
        }
    }
    

1.4 單元測驗

  1. 新建測驗類

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfig.class)
    public class BookServiceTest {   
    }
    
  2. 注入 Service 類,撰寫測驗方法

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfig.class)
    public class BookServiceTest {
    
        @Autowired
        private BookService bookService;
    
        @Test
        public void testGetById() {
            Book byId = bookService.getById(1);
            System.out.println(byId);
        }
    
        @Test
        public void testGetAll() {
            List<Book> all = bookService.getAll();
            System.out.println(all);
        }
    }
    

1.5 PostMan 測驗(細節不表)

2、統一結果封裝(前后端資料通信協議)

2.1 表現層與前端資料傳輸協議定義

  • 目前程式回傳的資料型別(回傳的資料型別太多,未來可能會更加混亂)

    • Controller 增刪改回傳給前端的是 Boolean 型別的資料

      true
      
    • Controller 查詢單個回傳給前端的是物件

      {
          "id":123,
          "type":"xxx",
          "name":"xxx",
          "description":"xxx"
      }
      
    • Controller 查詢所有回傳給前端的是物件的集合

      [
          {
              "id":123,
              "type":"xxx",
              "name":"xxx",
              "description":"xxx"
      	},
          {
              "id":124,
              "type":"xxx",
              "name":"xxx",
              "description":"xxx"
      	}
      ]
      
  • 將回傳的結果資料統一的方案

    • 為了封裝回傳的結果資料:創建結果模型類,封裝資料到data屬性中

      便于知道什么內容是資料部分

    • 為了封裝回傳的資料是何種操作及是否操作成功:封裝操作結果到code屬性中

      多個相同的回傳型別可能是不同操作,便于區分不同操作;另外,可以對code進行規定,如末位為0代表失敗,為1代表成功

    • 操作失敗后為了封裝回傳的錯誤資訊:封裝特殊訊息到message(msg)屬性中

      必要的錯誤資訊

  • 統一結果封裝的格式

    // 1.
    {
    	"code":20031,
    	"data":true
    }
    
    // 2.
    {
    	"code":20040,
    	"data":null,
    	"msg":"查詢失敗"
    }
    
    // 3.
    {
    	code:20041,
    	"data":[
            {
            "id":123,
            "type":"xxx",
            "name":"xxx",
            "description":"xxx"
            },
            {
            "id":124,
            "type":"xxx",
            "name":"xxx",
            "description":"xxx"
            }
    	]
    }
    

2.2 表現層與前端資料傳輸協議實作

2.2.1 思路分析

  • 統一資料回傳結果物體類

  • 統一回傳結果所需的狀態碼定義

2.2.2 結果封裝

  1. 創建 Result 類

    public class Result{
    	private Object data;
    	private Integer code;
    	private String msg;
        
        // 構造、getter、setter略...
    }
    
  2. 定義回傳碼 Code 類

    public class Code {
        public static final Integer SAVE_OK = 20011;
        public static final Integer DELETE_OK = 20021;
        public static final Integer UPDATE_OK = 20031;
        public static final Integer GET_OK = 20041;
    
        public static final Integer SAVE_ERR = 20010;
        public static final Integer DELETE_ERR = 20020;
        public static final Integer UPDATE_ERR = 20030;
        public static final Integer GET_ERR = 20040;
    }
    
  3. 修改 Conteoller 類的回傳值

    @RestController
    @RequestMapping("/books")
    public class BookController {
    
        @Autowired
        private BookService bookService;
    
        @PostMapping
        public Result save(@RequestBody Book book) {
            boolean flag = bookService.save(book);
            return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
        }
    
        @PutMapping
        public Result update(@RequestBody Book book) {
            boolean flag = bookService.update(book);
            return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
        }
    
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
            boolean flag = bookService.delete(id);
            return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
        }
    
        @GetMapping("/{id}")
        public Result getById(@PathVariable Integer id) {
    
            Book book = bookService.getById(id);
    
            Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
            String msg = book != null ? "" : "查詢的資料不存在,請重試";
            return new Result(code, book, msg);
        }
    
        @GetMapping
        public Result getAll() {
    
            List<Book> all = bookService.getAll();
    
            Integer code = all != null ? Code.GET_OK : Code.GET_ERR;
            String msg = all != null ? "" : "資料查詢失敗,請重試";
            return new Result(code, all, msg);
        }
    }
    

3、統一例外處理

3.1 問題描述

當出現例外時,回應500等錯誤代碼,回傳錯誤頁面,前端獲取到有效資訊,無法處理

  • 可能的例外的種類及其原因

    • 框架內部拋出的例外:因使用不合規導致
    • 資料層拋出的例外:因外部服務器故障導致(例如:服務器訪問超時)
    • 業務層拋出的例外:因業務邏輯書寫錯誤導致(例如:遍歷業務書寫操作,導致索引例外等)
    • 表現層拋出的例外:因資料收集、校驗等規則導致(例如:不匹配的資料型別間導致例外)
    • 工具類拋出的例外:因工具類書寫不嚴謹不夠健壯導致(例如:必要釋放的連接長期未釋放等)
  • 統一處理方案

    • 所有例外拋出到表現層進行處理

      • 各級均可能出現例外,為保證統一處理,需要向上拋出,直至表現層統一處理
      • 關于MVC模式與三層架構的關系可以參考:三層架構
    • 例外分類

      • 例外的種類有很多,對其分類以保證都能處理到
    • 使用AOP

      • 表現層處理例外,每個方法中單獨書寫,代碼書寫量巨大且意義不強,可以使用AOP以提高耦合

3.2 例外處理器的使用

為實作統一的例外處理,Spring提供了例外處理器

3.2.1 環境準備

  • 在表現層中創建統一例外處理類 ProjectExceptionAdvice
  • 不一定非要寫在表現層對應的 controller 包下,但是一定要保證 SpringMVC 控制類的包掃描配置能掃描到例外處理器類

3.2.2 使用步驟

  1. 創建例外處理器類

    // 宣告這個類用于Rest風格對應的統一例外處理器類
    @RestControllerAdvice
    public class ProjectExceptionAdvice {
    
        // 攔截例外
        @ExceptionHandler(Exception.class)
        public void doException(Exception ex) {
            System.out.println("捕獲例外");
        }
    }
    
  2. 例外處理器類回傳結果到前端

    // 宣告這個類用于Rest風格對應的統一例外處理
    @RestControllerAdvice
    public class ProjectExceptionAdvice {
    
        // 攔截例外
        @ExceptionHandler(Exception.class)
        // 修改回傳值型別,向前端回傳例外資訊
        public Result doException(Exception ex) {
            String msg = "例外捕獲";
            System.out.println(msg);
            return new Result(00000, null, msg);
        }
    }
    

3.2.3 相關知識點

3.2.3.1 @RestControllerAdvice
名稱 @RestControllerAdvice
型別 類注解
位置 Rest風格開發的控制器增強類定義上方
作用 為Rest風格開發的控制器類做增強
說明 此注解自帶@ResponseBody注解與@Component注解,具備對應的功能
3.2.3.2 @ExceptionHandler
名稱 @ExceptionHandler
型別 方法注解
位置 專用于例外處理的控制器方法上方
作用 設定指定例外的處理方案,功能等同于控制器方法,
出現例外后終止原始控制器執行,并轉入當前方法執行
說明 此類方法可以根據處理的例外不同,制作多個方法分別處理對應的例外

3.3 專案例外處理方案

3.3.1 例外分類

因為例外的種類有很多,如果每一個例外都對應一個@ExceptionHandler,那得寫多少個方法來處理各自的例外,所以在處理例外之前,需要對例外進行一個分類

  • 業務例外(BusinessException)

    • 規范的用戶行為產生的例外:如用戶在頁面輸入內容的時候未按照指定格式進行資料填寫,如在年齡框輸入字串
    • 不規范的用戶行為操作產生的例外:如故意傳遞錯誤資料
  • 系統例外(SystemException)

    • 專案運行程序中可預計但無法避免的例外:如資料庫或服務器宕機
  • 其他例外(Exception)

    • 開發人員未預期到的例外:如用到的檔案不存在

3.3.2 例外解決方案

  • 業務例外(BusinessException)

    • 向用戶發送對應訊息,提醒其規范操作
  • 系統例外(SystemException)

    • 向用戶發送訊息內容,告知當前系統狀態及可選操作
    • 發送特定訊息給運維人員提醒維護
    • 記錄日志
  • 其他例外(Exception)

    • 向用戶發送訊息內容,告知當前系統狀態
    • 發送特定訊息給開發人員,提醒維護
    • 記錄日志

3.3.3 例外解決方案的具體實作

  1. 自定義例外類

    • 系統例外(SystemException)

      // 繼承RuntimeException,可以不做處理自動上拋
      public class SystemException extends RuntimeException{
      
          private Integer code;
      
          public SystemException(Integer code, String message) {
              super(message);
              this.code = code;
          }
      
          public SystemException(Integer code, String message, Throwable cause) {
              super(message, cause);
              this.code = code;
          }
      
          public Integer getCode() {
              return code;
          }
      
          public void setCode(Integer code) {
              this.code = code;
          }
      }
      
    • 業務例外(BusinessException)

      public class BusinessException extends RuntimeException{
          private Integer code;
      
          public BusinessException(Integer code, String message) {
              super(message);
              this.code = code;
          }
      
          public BusinessException(Integer code, String message, Throwable cause) {
              super(message, cause);
              this.code = code;
          }
      
          public Integer getCode() {
              return code;
          }
      
          public void setCode(Integer code) {
              this.code = code;
          }
      }
      
  2. 將其他例外包成自定義例外(本案例在Service中進行)

    @Override
    public Book getById(Integer id) {
        // 將可能出現的例外進行包裝,轉換成自定義例外
        if (id <= 0) {
            throw new BusinessException(Code.BUSINESS_ERR, "資料不合法");
        }
    
        // 將可能出現的例外進行包裝,轉換成自定義例外
        try{
            int i = 1/0;
            return bookDao.getById(id);
        }catch (ArithmeticException ae){
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服務器訪問超時");
        }
    }
    
  3. 處理器類中處理自定義例外

    • 原先的public Result doException(Exception ex){}仍然保留,用于處理其他例外

    • 此處為了方便新增了錯誤代碼

      public static final Integer SYSTEM_ERR = 50001;
      public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
      public static final Integer SYSTEM_UNKNOWN_ERR = 59999;
      
      public static final Integer BUSINESS_ERR = 60001;
      
    // 宣告這個類用于Rest風格對應的統一例外處理
    @RestControllerAdvice
    public class ProjectExceptionAdvice {
    
        // 攔截例外
        @ExceptionHandler(SystemException.class)
        public Result doException(SystemException ex) {
            // 記錄日志
            // 發送訊息給運維
            // 郵件發送ex的物件給開發
            // 回傳訊息內容
            return new Result(ex.getCode(), null, ex.getMessage());
        }
    
        // 攔截例外
        @ExceptionHandler(BusinessException.class)
        public Result doException(BusinessException ex) {
            // 記錄日志
            // 發送郵件給開發
            // 回傳訊息內容
            return new Result(ex.getCode(), null, ex.getMessage());
        }
    
        // 仍然保留,用于處理其他例外
        @ExceptionHandler(Exception.class)
        // 修改回傳值型別,向前端回傳例外資訊
        public Result doException(Exception ex) {
            // 記錄日志
            // 發送訊息給運維
            // 郵件發送ex的物件給開發
            // 回傳訊息內容
            return new Result(Code.SYSTEM_UNKNOWN_ERR, null, "系統繁忙請稍后再試");
        }
    }
    

4、前后臺協議聯調

4.1 環境準備

  • 頁面準備

    該模塊使用到了 Axios 和 ElementUI

    <!DOCTYPE html>
    
    <html>
    
        <head>
    
            <!-- 頁面meta -->
    
            <meta charset="utf-8">
    
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
            <title>SpringMVC案例</title>
    
            <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    
            <!-- 引入樣式 -->
    
            <link rel="stylesheet" href="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/plugins/elementui/index.css">
    
            <link rel="stylesheet" href="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/plugins/font-awesome/css/font-awesome.min.css">
    
            <link rel="stylesheet" href="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/css/style.css">
    
        </head>
    
        <body >
    
            <div id="app">
    
                <div >
    
                    <h1>圖書管理</h1>
    
                </div>
    
                <div >
    
                    <div >
    
                        <div >
    
                            <el-input placeholder="圖書名稱" v-model="pagination.queryString" style="width: 200px;" ></el-input>
    
                            <el-button @click="getAll()" >查詢</el-button>
    
                            <el-button type="primary"  @click="handleCreate()">新建</el-button>
    
                        </div>
    
                        <el-table size="small" current-row-key="id" :data="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/30/dataList" stripe highlight-current-row>
    
                            <el-table-column type="index" align="center" label="序號"></el-table-column>
    
                            <el-table-column prop="type" label="圖書類別" align="center"></el-table-column>
    
                            <el-table-column prop="name" label="圖書名稱" align="center"></el-table-column>
    
                            <el-table-column prop="description" label="描述" align="center"></el-table-column>
    
                            <el-table-column label="操作" align="center">
    
                                <template slot-scope="scope">
    
                                    <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">編輯</el-button>
    
                                    <el-button type="danger" size="mini" @click="handleDelete(scope.row)">洗掉</el-button>
    
                                </template>
    
                            </el-table-column>
    
                        </el-table>
    
                        <!-- 新增標簽彈層 -->
    
                        <div >
    
                            <el-dialog title="新增圖書" :visible.sync="dialogFormVisible">
    
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label->
    
                                    <el-row>
    
                                        <el-col :span="12">
    
                                            <el-form-item label="圖書類別" prop="type">
    
                                                <el-input v-model="formData.type"/>
    
                                            </el-form-item>
    
                                        </el-col>
    
                                        <el-col :span="12">
    
                                            <el-form-item label="圖書名稱" prop="name">
    
                                                <el-input v-model="formData.name"/>
    
                                            </el-form-item>
    
                                        </el-col>
    
                                    </el-row>
    
    
                                    <el-row>
    
                                        <el-col :span="24">
    
                                            <el-form-item label="描述">
    
                                                <el-input v-model="formData.description" type="textarea"></el-input>
    
                                            </el-form-item>
    
                                        </el-col>
    
                                    </el-row>
    
                                </el-form>
    
                                <div slot="footer" >
    
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
    
                                    <el-button type="primary" @click="handleAdd()">確定</el-button>
    
                                </div>
    
                            </el-dialog>
    
                        </div>
    
                        <!-- 編輯標簽彈層 -->
    
                        <div >
    
                            <el-dialog title="編輯檢查項" :visible.sync="dialogFormVisible4Edit">
    
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label->
    
                                    <el-row>
    
                                        <el-col :span="12">
    
                                            <el-form-item label="圖書類別" prop="type">
    
                                                <el-input v-model="formData.type"/>
    
                                            </el-form-item>
    
                                        </el-col>
    
                                        <el-col :span="12">
    
                                            <el-form-item label="圖書名稱" prop="name">
    
                                                <el-input v-model="formData.name"/>
    
                                            </el-form-item>
    
                                        </el-col>
    
                                    </el-row>
    
                                    <el-row>
    
                                        <el-col :span="24">
    
                                            <el-form-item label="描述">
    
                                                <el-input v-model="formData.description" type="textarea"></el-input>
    
                                            </el-form-item>
    
                                        </el-col>
    
                                    </el-row>
    
                                </el-form>
    
                                <div slot="footer" >
    
                                    <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
    
                                    <el-button type="primary" @click="handleEdit()">確定</el-button>
    
                                </div>
    
                            </el-dialog>
    
                        </div>
    
                    </div>
    
                </div>
    
            </div>
    
        </body>
    
        <!-- 引入組件庫 -->
    
        <script src="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/js/vue.js"></script>
    
        <script src="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/plugins/elementui/index.js"></script>
    
        <script type="text/javascript" src="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/js/jquery.min.js"></script>
    
        <script src="https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/js/axios-0.18.0.js"></script>
    
        <script>
            var vue = new Vue({
    
                el: '#app',
                data:{
                    pagination: {},
    				dataList: [],//當前頁要展示的串列資料
                    formData: {},//表單資料
                    dialogFormVisible: false,//控制表單是否可見
                    dialogFormVisible4Edit:false,//編輯表單是否可見
                    rules: {//校驗規則
                        type: [{ required: true, message: '圖書類別為必填項', trigger: 'blur' }],
                        name: [{ required: true, message: '圖書名稱為必填項', trigger: 'blur' }]
                    }
                },
    
                //鉤子函式,VUE物件初始化完成后自動執行
                created() {
                    this.getAll();
                },
    
                methods: {
                    //串列
                    getAll() {
                    },
    
                    //彈出添加視窗
                    handleCreate() {
                    },
    
                    //重置表單
                    resetForm() {
                    },
    
                    //添加
                    handleAdd () {
                    },
    
                    //彈出編輯視窗
                    handleUpdate(row) {
                    },
    
                    //編輯
                    handleEdit() {
                    },
    
                    // 洗掉
                    handleDelete(row) {
                    }
                }
            })
    
        </script>
    
    </html>
    
  • SpringMVC 攔截時放行頁面請求

    需要在 SpringMVC配置類中配置包掃描

    @Configuration
    public class SpringMvcSupport extends WebMvcConfigurationSupport {
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
            registry.addResourceHandler("/css/**").addResourceLocations("/css/");
            registry.addResourceHandler("/js/**").addResourceLocations("/js/");
            registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
        }
    }
    

4.2 串列功能

  • 異步請求

    //串列
    getAll() {
        // 發送ajax請求
        axios.get("/books").then((res)=>{
            // 注意第二個data是物體類中封裝的屬性data,二者意義不同
            this.dataList = res.data.data;
        });
    },
    

4.3 添加功能

  • 頁面元素

    //彈出添加視窗
    handleCreate() {
        this.dialogFormVisible = true;
        // 跳出新增視窗時對視窗中輸入框的表單內容進行一次清除
        this.resetForm();
    },
    
    //重置表單,清理之前添加的資料
    resetForm() {
        this.formData = https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/30/{};
    },
    
  • 異步請求

    //添加
    handleAdd () {
        // 發送ajax請求
        axios.post("/books", this.formData).then((res)=>{
            // alert(res.data.code);
            if (res.data.code == 20011) {
                // 如果操作成功,關閉彈層
                this.dialogFormVisible = false;
                this.$message.success("添加成功");
            }else if (res.data.code == 20010) {
                // 此處后臺沒有給出回應的msg,手動補充
                this.$message.error("添加失敗,資料不合法");
            }else {
                this.$message.error(res.data.msg);
            }
        }).finally(()=>{
            // 重新顯示資料
            this.getAll();
        });
    },
    

4.4 添加、修改、洗掉功能狀態處理

  • 存在問題

    需要判斷SQL陳述句執行成功或者失敗條件

  • 解決方案:對Dao和Service部分進行修改

    • Dao

      @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
      // @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
      public int save(Book book);
      
      @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
      public int update(Book book);
      
      @Delete("delete from tbl_book where id = #{id}")
      public int delete(Integer id);
      
    • Service

      @Override
      public boolean save(Book book) {
          return bookDao.save(book) > 0;
      }
      
      @Override
      public boolean update(Book book) {
          return bookDao.update(book) > 0;
      }
      
      @Override
      public boolean delete(Integer id) {
          return bookDao.delete(id) > 0;
      }
      

4.5 修改功能

  • 頁面元素

    //彈出編輯視窗
    handleUpdate(row) {
        // console.log(row);
        // 根據id查詢資料
        axios.get("/books/" + row.id).then((res)=>{
            if (res.data.code == 20041) {
                // 展示彈層,回顯資料
                this.formData = https://www.cnblogs.com/dandelion-000-blog/archive/2023/01/30/res.data.data;
                this.dialogFormVisible4Edit = true;
            } else {
                this.$message.error(res.data.msg);
            }
        });
    },
    
  • 異步請求

    //編輯
    handleEdit() {
        // 發送ajax請求
        axios.put("/books", this.formData).then((res)=>{
            // alert(res.data.code);
            if (res.data.code == 20031) {
                // 如果操作成功,關閉彈層
                this.dialogFormVisible4Edit = false;
                this.$message.success("修改成功");
            }else if (res.data.code == 20030) {
                this.$message.error("修改失敗,資料不合法");
            }else {
                this.$message.error(res.data.msg);
            }
        }).finally(()=>{
            // 重新顯示資料
            this.getAll();
        });
    },
    

4.6 洗掉功能

// 洗掉
handleDelete(row) {
    // 彈出提示框
    this.$confirm("此操作將永久洗掉資料,請確認","注意",{
        type:'info'
    }).then(()=>{
        // 確認洗掉
        // 發送Ajax請求
        axios.delete("/books/" + row.id).then((res)=>{
            if (res.data.code == 20021) {
                this.$message.success("洗掉成功");
            } else {
                this.$message.error("洗掉失敗");
            }
        }).finally(()=>{
            // 重新顯示資料
            this.getAll();
        });
    }).catch(()=>{
        // 取消洗掉
        this.$message.info("洗掉已取消");
    });
}

5、攔截器

5.1 攔截器概念

  • 攔截器概念:攔截器(Interceptor)是一種動態攔截方法呼叫的機制,在SpringMVC中動態攔截控制器方法的執行

  • 攔截器作用:(做增強)

    • 在指定的方法呼叫前后執行預先設定的代碼
    • 阻止原始方法的執行
  • 攔截器和過濾器的區別

    • 歸屬不同:Filter屬于Servlet技術,Interceptor屬于SpringMVC技術
    • 攔截內容不同:Filter對所有訪問進行增強,Interceptor僅針對SpringMVC的訪問進行增強(取決于 web 服務器配置類中 SpringMVC 的訪問內容設定)
  • 攔截器圖示

    • SpringMVC與攔截器:訪問前后執行操作

      SpringMVC與攔截器

    • 作業流程:堆疊順序

      攔截器作業流程

5.2 攔截器入門案例

5.2.1 環境準備

  • 需要 SpringMVC 配置類和已經配置好的Controller,筆者使用上面的 SSM 整合案例的代碼進行演示

5.2.2 攔截器開發

  1. 創建攔截器類

    • 攔截器一般寫在 controller 包下,一般只給 controller 用
    • 攔截器也可以寫在其他位置,但是要保證 SpringMVC 配置類的包掃描可以掃描到
    package priv.dandelion.controller.interceptor;
    
    // import ...
    
    @Component
    public class ProjectInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("preHandle");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("postHandle");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("afterCompletion");
        }
    }
    
  2. 配置攔截器類

    • 此處寫在了 SpringMvcSupport 中,注意配置類注解
    • 要保證 SpringMVC 配置類的包掃描可以掃描到
    @Configuration
    public class SpringMvcSupport extends WebMvcConfigurationSupport {
    
        @Autowired
        private ProjectInterceptor projectInterceptor;
    
        // 配置攔截器
        @Override
        protected void addInterceptors(InterceptorRegistry registry) {
            // 使用到的兩個引數均為可變引數,可以直接寫多個,addResourceHandlers()中相同,不再贅述
            // registry.addInterceptor(projectInterceptor).addPathPatterns("/books", "/books/*");
    
            // 也可以采用這種形式,攔截/books,/books/*,/books/*/*...
            registry.addInterceptor(projectInterceptor).addPathPatterns("/books/**");
        }
    
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
            registry.addResourceHandler("/css/**").addResourceLocations("/css/");
            registry.addResourceHandler("/js/**").addResourceLocations("/js/");
            registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
        }
    }
    

5.2.3 攔截器攔截規則

  • 執行順序

    1. 執行 preHandle()【回傳 true】
    2. 執行原始方法
    3. 執行 postHandle()
    4. 執行afterCompletion()
  • boolean preHandle()方法

    • preHandle() 回傳 true 時,按照上面的執行順序執行
    • preHandle() 回傳 true 時,中止原始操作的執行,原始操作后的攔截器操作也不執行
  • 添加攔截并設定攔截的訪問路徑時

    • 假設攔截內容為/book,當使用 Rest 風格時,GET /books 與 POST /book/1 不同,/book/1不會被攔截
    • 假設攔截內容為/book*,當使用 Rest 風格時,PUT /books/1 與 POST /book/1 都會被攔截
  • 補充:執行流程圖解

    攔截器執行順序圖解

5.2.4 簡化SpringMvcSupport的撰寫

  • 可以直接在 SpringMvcConfig 中繼承 WebMvcConfigurer 介面,覆寫相應方法,效果相同
  • 相比 SpringMvcSupport 具有一定侵入性
@Configuration
@ComponentScan("priv.dandelion.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ProjectInterceptor projectInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
    }
}

5.3 攔截器引數

攔截器代碼見 5.2.2

5.3.1 前置處理方法

  • request:請求物件,獲取請求資料中的內容,如獲取請求頭的Content-Type

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String contentType = request.getHeader("Content-Type");
        System.out.println("preHandle..."+contentType);
        return true;
    }
    
  • response:回應物件,同 request

  • handler:被呼叫的處理器物件,本質上是一個方法物件,對反射中的Method物件進行了再包裝,可以獲取方法的相關資訊

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod hm = (HandlerMethod)handler;
        String methodName = hm.getMethod().getName();//可以獲取方法的名稱
        System.out.println("preHandle..."+methodName);
        return true;
    }
    

5.3.2 后置處理方法

  • ModelAndView modelAndView:如果處理器執行完成具有回傳結果,可以讀取到對應資料與頁面資訊,并進行調整,目前開發回傳 JSON 資料較多,其使用率不高

5.3.3 完成處理方法

  • Exception ex:如果處理器執行程序中出現例外物件,可以針對例外情況進行單獨處理,如表現層拋出的例外,現在已經有全域例外處理器類,所以該引數的使用率也不高,

5.4 攔截器鏈運行順序

  • 規則

    • 當配置多個攔截器時,形成攔截器鏈
    • 攔截器鏈的運行順序參照攔截器添加順序為準
    • 當攔截器中出現對原始處理器的攔截,后面的攔截器均終止運行
    • 當攔截器運行中斷,僅運行配置在前面的攔截器的 afterCompletion 操作
  • 圖解

    攔截器鏈運行規則圖解

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542580.html

標籤:其他

上一篇:FPGA用ROM輸出正弦波

下一篇:SSL 證書基本概念掃盲

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more