最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于SpringBoot的SSMP的整合案例

 更新時(shí)間:2023年05月23日 11:25:05   作者:LuZhouShiLi  
這篇文章主要介紹了SpringBoot整合SSMP的詳細(xì)教程,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下

簡單介紹

在這里插入圖片描述

模塊創(chuàng)建

添加spring WEB 和MYSQL driver的依賴

然后手動(dòng)添加Mybatis-plus和druid的依賴

在這里插入圖片描述

改寫配置文件端口

在這里插入圖片描述

創(chuàng)建實(shí)體類

在domain包下面創(chuàng)建Book類,使用lombook技術(shù)自動(dòng)配置相關(guān)get set操作

Lombok,一個(gè)Java類庫,提供了一組注解,簡化POJO實(shí)體類開發(fā)

添加lombok依賴,不需要添加坐標(biāo),因?yàn)閜arent已經(jīng)包含坐標(biāo)

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

使用@Data注解簡化開發(fā)

package com.ustc.domain;
import lombok.Data;
@Data
public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;
}

導(dǎo)入Mybatis-plus和druid的配置文件

在這里插入圖片描述

server:
  port: 80
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/da?serverTimezone=UTC
      username: root
      password: 123456
mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
# 配置druid

使用junit測試查詢方法

package com.ustc;
import com.ustc.Dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.ustc.domain.Book;
@SpringBootTest
class Sp11ApplicationTests {
    @Autowired
    private BookDao bookDao;
    @Test
    void contextLoads() {
        System.out.println(bookDao.selectById(1));
    }
//    插入操作
    @Test
    void testSave(){
        Book book = new Book();
        book.setId(10);
        book.setType("心理");
        book.setName("111111111111111111");
        book.setDescription("dshf");
        bookDao.insert(book);
    }
    //    更新操作
    @Test
    void testSave1(){
        Book book = new Book();
        book.setId(10);
        book.setType("心理");
        book.setName("如何成為富婆");
        book.setDescription("dshf");
        bookDao.updateById(book);
    }
    // 刪除操作
    @Test
    void testdelete(){
        bookDao.deleteById(1);
    }
    // 查詢?nèi)坎僮?
    @Test
    void testGetAll(){
        System.out.println(bookDao.selectList(null));
    }
}

MP分頁查詢

  • 創(chuàng)建分頁對象Page
  • 創(chuàng)建分頁攔截器
//    分頁查詢操作  需要在配置類中添加攔截器
    @Test
    void testGetPage(){
        IPage page = new Page(1,2);//page是IPage的實(shí)現(xiàn)類
        // 輸出數(shù)據(jù)
        System.out.println(bookDao.selectPage(page,null).getRecords());
    }
package com.ustc.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MPConfig {
    // 定義攔截器
    //    注入攔截器資源
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
//        創(chuàng)建mP攔截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());// 添加分頁攔截器
        return interceptor;
    }
}

按照條件進(jìn)行查詢

使用QueryWrapper對象封裝查詢條件,推薦使用LambdaQueryWrapper對象,所有查詢操作封裝成方法調(diào)用

    @Test
    void testGetBy(){
        QueryWrapper<Book> qw = new QueryWrapper<>();
//        設(shè)置查詢條件
        qw.like("name","Spring");
        bookDao.selectList(qw);// 傳入查詢條件
    }
    @Test
    void testGetBy1(){
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
//        設(shè)置查詢條件
        qw.like(Book::getName,"Spring");
        bookDao.selectList(qw);// 傳入查詢條件
    }

業(yè)務(wù)層Service開發(fā)

Service層接口定義與數(shù)據(jù)層接口定義具有較大的區(qū)別,不要混用

  • selectByUserNameAndPassword(String username,String password) 數(shù)據(jù)層
  • login(String username,String password) 業(yè)務(wù)層
  • 定義 service接口
package com.ustc.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ustc.domain.Book;
import org.springframework.stereotype.Service;
import java.util.List;
public interface BookService {
//    業(yè)務(wù)層先定義 業(yè)務(wù)的接口
    Boolean save(Book book);
    Boolean update(Book book);
    Boolean delete(Integer id);
    Book getById(Integer id);
    List<Book> getAll();
//    分頁查詢接口
    IPage<Book> getPage(int currentPage, int pageSize);
}
  • 定義service接口的實(shí)現(xiàn)類
package com.ustc.service.Impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ustc.Dao.BookDao;
import com.ustc.domain.Book;
import com.ustc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
// 將該實(shí)現(xiàn)類定義成業(yè)務(wù)層的一個(gè)bean資源
@Service
public class BookServiceImpl implements BookService {
//     注入數(shù)據(jù)層的接口
    @Autowired
    private BookDao bookDao;
    @Override
    public Boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }
    @Override
    public Boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }
    @Override
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }
    @Override
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }
    @Override
    public List<Book> getAll() {
        return bookDao.selectList(null);
    }
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
//         首先創(chuàng)建分頁查詢對象
        IPage page = new Page(currentPage,pageSize);
        return bookDao.selectPage(page,null);
    }
}
  • 注入bookService接口資源 進(jìn)行查詢測試
//    分頁查詢
    @Test
    void testGetPage1(){
        IPage<Book> page = bookService.getPage(1,5);
        System.out.println(page.getRecords());
    }
//    使用業(yè)務(wù)層進(jìn)行查詢
    @Test
    void test1(){
        System.out.println(bookService.getById(4));
    }

業(yè)務(wù)層Service快速開發(fā)

  • 首先定義一個(gè)IBookService
package com.ustc.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ustc.domain.Book;
public interface IBookService extends IService<Book> {
}
  • 實(shí)現(xiàn)IBookService接口
package com.ustc.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ustc.domain.Book;
public interface IBookService extends IService<Book> {
}
  • 使用通用接口(IService) 快速開發(fā)Service
  • 使用通用實(shí)現(xiàn)類(ServiceImpl<M,T>) 快速開發(fā)ServiceImpl
  • 可以在通用接口的基礎(chǔ)上做功能重載或者功能追加
  • 注意重載時(shí)不要覆蓋原始的操作,避免原始提供的功能丟失

表現(xiàn)層開發(fā)

package com.ustc.Controller;
import com.ustc.domain.Book;
import com.ustc.service.IBookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;
//    查詢?nèi)啃畔?
    @GetMapping("/search")
    public List<Book> getAll(){
        return bookService.list();
    }
//    插入數(shù)據(jù)   這里的參數(shù) 通過請求體傳輸json數(shù)據(jù)  添加RequestBody注解
    @PostMapping("/insert")
    public Boolean save(@RequestBody Book book){
        return bookService.save(book);
    }
    // 修改數(shù)據(jù)
    @PutMapping("/update")
    public Boolean update(@RequestBody Book book){
        return bookService.modify(book);
    }
//    刪除數(shù)據(jù)
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Integer id){
        return bookService.delete(id);
    }
//     根據(jù)id進(jìn)行查詢   使用pathVariable注解 使得url參數(shù) 賦值到形參
    @GetMapping("{id}")
    public Book getById(@PathVariable Integer id){
        return bookService.getById(id);
    }
}

使用postman做測試

在這里插入圖片描述

表現(xiàn)層 實(shí)現(xiàn)分頁查詢

  • Controller
    @GetMapping("/page/{currentPage}/{pageSize}")
    public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return bookService.getPage(currentPage,pageSize);
    }
  • Service
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        bookDao.selectPage(page,null);
        return page;
    }

表現(xiàn)層消息一致性的處理

在這里插入圖片描述

在這里插入圖片描述

設(shè)計(jì)表現(xiàn)層返回結(jié)果的模型類,用于后端與前端進(jìn)行數(shù)據(jù)格式統(tǒng)一,也成為前后端數(shù)據(jù)協(xié)議

首先定義一個(gè)R類,這里的flag表示后端的操作有沒有成功,data表示后端傳輸?shù)臄?shù)據(jù)

package com.ustc.Controller.utils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.ustc.domain.Book;
import lombok.Data;
@Data
public class R {
    private Boolean flag;
    private Object data;
    public R(){}
    public R(Boolean flag){
        this.flag = flag;
    }
// 構(gòu)造函數(shù)重載
    public R(Boolean flag,Object data){
        this.flag = flag;
        this.data = data;
    }
}

之后改寫Controller的接口方法,插入,修改,刪除操作只需要傳入Boolean參數(shù)即可,對于需要返回?cái)?shù)據(jù)的接口,使用另一種構(gòu)造方法

package com.ustc.Controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ustc.Controller.utils.R;
import com.ustc.domain.Book;
import com.ustc.service.IBookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;
//    查詢?nèi)啃畔?
    @GetMapping("/search")
    public R getAll(){
        return new R(true,bookService.list());
    }
//    插入數(shù)據(jù)   這里的參數(shù) 通過請求體傳輸json數(shù)據(jù)  添加RequestBody注解
    @PostMapping("/insert")
    public R save(@RequestBody Book book){
        return new R(bookService.save(book));
    }
    // 修改數(shù)據(jù)
    @PutMapping("/update")
    public R update(@RequestBody Book book){
//        return bookService.modify(book);
        return new R(bookService.modify(book));
    }
//    刪除數(shù)據(jù)
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        return new R(bookService.delete(id));
    }
//     根據(jù)id進(jìn)行查詢   使用pathVariable注解 使得url參數(shù) 賦值到形參
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        // 第一個(gè)參數(shù)是flag 第二個(gè)參數(shù)是 object對象  data
        R r = new R(true,bookService.getById(id));
        return r;
    }
    // 分頁查詢操作
    @GetMapping("/page/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return new R(true,bookService.getPage(currentPage,pageSize));
    }
}

意義:

  • 設(shè)計(jì)統(tǒng)一的返回值結(jié)果類型便于前端開發(fā)讀取數(shù)據(jù)
  • 返回值結(jié)果類型可以根據(jù)需求自行設(shè)定,沒有固定的格式
  • 返回值結(jié)果類型用于后端和前端進(jìn)行數(shù)據(jù)格式統(tǒng)一,也稱之為前后端數(shù)據(jù)協(xié)議

在這里插入圖片描述

查詢所有書本信息

發(fā)送異步請求,將請求后端查詢的數(shù)據(jù)傳給前端,前端數(shù)據(jù)雙向綁定進(jìn)行數(shù)據(jù)展示

在這里插入圖片描述

在這里插入圖片描述

添加書本

使用axios請求將前端的請求包括數(shù)據(jù)發(fā)送給后端

在這里插入圖片描述

在這里插入圖片描述

  • 請求方式使用POST調(diào)用后臺對應(yīng)操作
  • 添加操作結(jié)束之后動(dòng)態(tài)刷新頁面加載數(shù)據(jù)
  • 根據(jù)操作結(jié)果不同,顯示對應(yīng)的提示信息
  • 彈出添加div時(shí)清除表單數(shù)據(jù)
            handleAdd () {
                // 使用axios 將數(shù)據(jù)發(fā)送給后端  post請求  發(fā)送請求  返回res 檢查flag操作是否成功
                axios.post("/books",this.formData).then((res)=>{
                    // 判斷當(dāng)前操作是否成功
                    if(res.data.flag){
                        // 關(guān)閉彈層
                        // 點(diǎn)擊確定 發(fā)送數(shù)據(jù)之后 關(guān)閉彈窗
                        this.dialogFormVisible = false;
                        this.$message.success("添加成功");
                    }else{
                        this.$message.error("添加失敗");
                    }
                }).finally(()=>{
                    // 重新加載數(shù)據(jù)
                    this.getAll();
                });
            },

刪除操作

  • 請求方式使用Delete調(diào)用后臺對應(yīng)操作
  • 刪除操作需要傳遞當(dāng)前行數(shù)據(jù)對應(yīng)的id值到后臺
  • 刪除操作結(jié)束之后動(dòng)態(tài)刷新頁面加載數(shù)據(jù)
  • 根據(jù)操作結(jié)果的不同,顯示對應(yīng)的提示信息
  • 刪除操作前彈出提示框避免誤操作
          // 刪除
            handleDelete(row) {
               // axios發(fā)送異步請求  使用deleteMapping 參數(shù)是id  刪除操作
                this.$confirm("此操作永久刪除當(dāng)前信息,是否繼續(xù)?","提示",{type:"info"}).then(()=>{
                    axios.delete("/books/"+row.id).then((res)=>{
                        if(res.data.flag){
                            this.$message.success("刪除成功");
                        }else{
                            this.$message.error("刪除失敗");
                        }
                    }).finally(()=>{
                        // 不管刪除成功 還是失敗 都會(huì)刷新頁面
                        this.getAll();
                    });
                }).catch(()=>{
                    this.$message.info("取消操作");
                });
            },

修改功能

  • 加載要修改數(shù)據(jù)通過傳遞當(dāng)前行數(shù)據(jù)對應(yīng)的id值到后臺查詢數(shù)據(jù)
  • 利用前端數(shù)據(jù)雙向綁定將查詢到的數(shù)據(jù)進(jìn)行回顯
  • 首先點(diǎn)擊編輯按鈕 根據(jù)id加載后端的數(shù)據(jù)
  • 然后編輯數(shù)據(jù),傳給后端
//彈出編輯窗口  點(diǎn)擊編輯按鈕  根據(jù)id加載后臺的數(shù)據(jù)
            handleUpdate(row) {
                // 發(fā)送異步請求
                axios.get("/books/"+row.id).then((res)=>{
                    if(res.data.flag && res.data.data != null){
                        // 內(nèi)容賦值  彈出編輯窗口 然后將數(shù)據(jù)填充上去
                        this.dialogFormVisible4Edit = true;
                        this.formData = res.data.data;
                    }else{
                        this.$message.error("數(shù)據(jù)同步失敗 ,自動(dòng)刷新");
                    }
                }).finally(()=>{
                    // 重新加載數(shù)據(jù)  也就是刷新頁面
                    this.getAll();
                });
            },
            //編輯按鈕:這個(gè)按鈕的作用就是根據(jù)id查詢數(shù)據(jù)信息 然后填充到頁面即可  put操作將表單修改的數(shù)據(jù)進(jìn)行回顯
            handleEdit() {
               axios.put("/books",this.formData).then((res)=>{
                   //  判斷當(dāng)前操作是否成功
                   if(res.data.flag){
                       // 關(guān)閉彈窗
                       this.dialogFormVisible4Edit = false;
                       this.$message.success("添加成功");
                   }else{
                       this.$message.error("添加失敗");
                   }
               });
            },

異常處理功能

自定義異常

package com.itheima.controller.utils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//作為springmvc的異常處理器
//@ControllerAdvice
@RestControllerAdvice
public class ProjectExceptionAdvice {
    //攔截所有的異常信息
    @ExceptionHandler(Exception.class)
    public R doException(Exception ex){
        //記錄日志
        //通知運(yùn)維
        //通知開發(fā)
        ex.printStackTrace();
        return new R("服務(wù)器故障,請稍后再試!");
    }
}
  • 使用注解@RestControllerAdvice定義SpringMVC異常處理器來處理異常
  • 異常處理器必須被掃描加載,否則無法生效
  • 表現(xiàn)層返回結(jié)果的模型類中添加消息屬性用來傳遞消息到頁面

添加分頁查詢

發(fā)起請求調(diào)用,將當(dāng)前頁碼之和每頁的展示數(shù)據(jù)量 傳到后端進(jìn)行查詢

            getAll() {
                //發(fā)送異步請求
                axios.get("/books/" + this.pagination.currentPage + "/" + this.pagination.pageSize).then((res)=>{
                    // console.log(res.data);
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.total = res.data.data.total;
                    // 將請求后端發(fā)送的數(shù)據(jù)傳給前端  展示
                    this.dataList = res.data.data.records;
                });
            },
  • 使用el分頁組件
  • 定義分頁組件綁定的數(shù)據(jù)模型
  • 異步調(diào)用獲取分頁數(shù)據(jù)
  • 分頁數(shù)據(jù)頁面回顯

以上就是基于SpringBoot的SSMP的整合案例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合SSMP的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 字典常用操作之鍵值對的高效數(shù)據(jù)管理

    Python 字典常用操作之鍵值對的高效數(shù)據(jù)管理

    在 Python 的內(nèi)置數(shù)據(jù)結(jié)構(gòu)中,字典(dict)以其靈活、高效和直觀的特性,成為開發(fā)者最常用的工具之一,本文將帶你從零開始,系統(tǒng)掌握 Python字典的基礎(chǔ)知識與常用操作,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • Java中獲取年份月份的幾種常見方法

    Java中獲取年份月份的幾種常見方法

    這篇文章主要給大家介紹了關(guān)于Java中獲取年份月份的幾種常見方法,在開發(fā)應(yīng)用程序時(shí),經(jīng)常需要獲取當(dāng)前的年、月、日,并以特定格式進(jìn)行展示或處理,需要的朋友可以參考下
    2023-09-09
  • springboot實(shí)現(xiàn)將自定義日志格式存儲到mongodb中

    springboot實(shí)現(xiàn)將自定義日志格式存儲到mongodb中

    這篇文章主要介紹了springboot實(shí)現(xiàn)將自定義日志格式存儲到mongodb中的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot運(yùn)用AOP來實(shí)現(xiàn)分布式鎖的示例代碼

    SpringBoot運(yùn)用AOP來實(shí)現(xiàn)分布式鎖的示例代碼

    本文主要介紹了通過注解和AOP實(shí)現(xiàn)分布式鎖的方案,包含鎖過期時(shí)間、等待超時(shí)設(shè)置及自動(dòng)續(xù)約功能,利用定時(shí)任務(wù)監(jiān)控鎖狀態(tài)并延長有效期,感興趣的可以了解一下
    2025-09-09
  • SpringCache的基本使用方法

    SpringCache的基本使用方法

    Spring?Cache利用了AOP,實(shí)現(xiàn)了基于注解的緩存功能,并且進(jìn)行了合理的抽象,業(yè)務(wù)代碼不用關(guān)心底層是使用了什么緩存框架,只需要簡單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能了,本文介紹SpringCache的基本使用方法,感興趣的朋友一起看看吧
    2024-01-01
  • 淺析Java的Hibernate框架中的繼承關(guān)系設(shè)計(jì)

    淺析Java的Hibernate框架中的繼承關(guān)系設(shè)計(jì)

    這篇文章主要介紹了Java的Hibernate框架中的繼承關(guān)系設(shè)計(jì),Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • PC 端微信掃碼注冊和登錄實(shí)例

    PC 端微信掃碼注冊和登錄實(shí)例

    這篇文章主要介紹了PC 端微信掃碼注冊和登錄實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • mybatis的使用-Mapper文件各種語法介紹

    mybatis的使用-Mapper文件各種語法介紹

    這篇文章主要介紹了mybatis的使用-Mapper文件各種語法介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java?數(shù)據(jù)交換?Json?和?異步請求?Ajax詳解

    Java?數(shù)據(jù)交換?Json?和?異步請求?Ajax詳解

    Json(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,采用鍵值對的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲,這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請求Ajax,需要的朋友可以參考下
    2023-09-09
  • mybatis如何返回某列的最大值

    mybatis如何返回某列的最大值

    這篇文章主要介紹了mybatis如何返回某列的最大值操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論

新晃| 长宁县| 德格县| 大埔区| 沁阳市| 稻城县| 眉山市| 玉门市| 吉林省| 顺义区| 台中市| 从化市| 望谟县| 措美县| 措美县| 大埔区| 鸡东县| 调兵山市| 宁蒗| 德格县| 卓尼县| 宁明县| 桐乡市| 古交市| 双牌县| 铁岭县| 广汉市| 平凉市| 宣化县| 舞钢市| 贵德县| 东方市| 三都| 长阳| 手游| 嘉鱼县| 灵川县| 拜城县| 翁源县| 凌海市| 台湾省|