SpringBoot的SSMP案例(后端开发)

博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:SpringBoot专栏(每日更新)
SpringBoot入门案例-阿里云版和纯手工版:点击查看
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起学习!一起加油!
在这里插入图片描述

目录

  • ✨创建数据表
  • ✨IDEA配置MySQL数据库连接
  • ✨搭建项目
  • ✨数据层开发
  • ✨开启MP运行日志
  • ✨分页查询
  • ✨条件查询
  • ✨业务层开发
    • 基础(CRUD)
    • MyBatis-Plus快速开发
  • ✨表现层开发(前后端数据协调统一)
    • 标准版开发
    • 表现层消息一致性处理(前后端分离)


✨创建数据表

创建一个tbl_book表
SpringBoot的SSMP案例(后端开发)_第1张图片
SpringBoot的SSMP案例(后端开发)_第2张图片

✨IDEA配置MySQL数据库连接

详细步骤操作 点击直接查看

✨搭建项目

不同版本的搭建项目在我springboot专栏里,本次项目采用的是阿里云版。模块的创建就不用多说了。
⭐注意:选择需要的技术
SpringBoot的SSMP案例(后端开发)_第3张图片
SpringBoot的SSMP案例(后端开发)_第4张图片
⭐Lombok开发工具可以简化实体类的开发。
SpringBoot的SSMP案例(后端开发)_第5张图片
导入druid的依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.11</version>
        </dependency>

设置服务器端口号:

server:
  port: 80

Lombok注解
常用注解: @Data @setter @Getter

  • @Data
    • 为当前实体类在编译期设置对应的get/set,toString方法,hashCode方法,equals方法等。
  • @Constructor
    • @AllArgsConstructor: 有参构造
    • @NoArgsConstructor: 无参构造

Book

package com.jkj.domain;
import lombok.*;
//@Setter
//@Getter
@Data
public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;
}

✨数据层开发

编写yml

server:
  port: 80

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    
 mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_

编写Dao层(采用MyBatis-Plus技术)

package com.jkj.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jkj.domain.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookMapper extends BaseMapper<Book> {

}

测试MyBatis-Plus的其他操作

package com.jkj.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest()
public class SsmpTest {
    @Autowired
    private BookMapper bookMapper;
    @Test
    void findById() {
        System.out.println(bookMapper.selectById(2));
    }
    @Test
    void insert() {
        Book book=new Book();
        book.setName("springboot");
        book.setType("框架");
        book.setDescription("好用");
        bookMapper.insert(book);
    }
    @Test
    void updateById() {
        Book book=new Book();
        book.setId(4);
        book.setDescription("666");
        bookMapper.updateById(book);
    }

    @Test
    void delete() {
        bookMapper.deleteById(4);

    }
    @Test
    void findAll() {
        bookMapper.selectList(null);
    }
}

注意:在进行添加操作过程中,会爆id自增的问题在yml文件中添加这行代码: id-type: auto

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
      id-type: auto

✨开启MP运行日志

编辑yml

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

测试类

 @Test
    void findAll() {
        bookMapper.selectList(null);

    }

SpringBoot的SSMP案例(后端开发)_第6张图片

✨分页查询

编写拦截器MPConfig类

package com.jkj.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(){
        //定义MP拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加具体拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

分页测试

    @Test
    void page(){
        IPage page = new Page(2,5);
        bookMapper.selectPage(page,null);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getRecords());

    }

SpringBoot的SSMP案例(后端开发)_第7张图片

✨条件查询

编写测试类
推荐使用

  • 使用 QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用。
    //推荐使用
    @Test
    void ByCondition1(){
        String name="一";
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        //支持动态拼写查询条件
        lqw.like(name!=null,Book::getName,name);
        bookMapper.selectList(lqw);
    }
  @Test
    void ByCondition(){
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like("name","一");
        bookMapper.selectList(wrapper);
    }

在这里插入图片描述

✨业务层开发

基础(CRUD)

  • Service层接口定义与数据层接口定义具有较大区别,不要混用。
    • selectByUserNameAndPassword(String username, String password);
    • login(String username , string password );
      编写service层
package com.jkj.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jkj.domain.Book;

import java.util.List;

public interface BookService {
    Boolean save(Book book);
    Boolean update(Book book);
    Boolean delete(Integer id);
    Book selectById(Integer id);
    List<Book> SelectAll();
    IPage<Book> getPage(int currentPage,int pageSize);

}

实现类

package com.jkj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.dao.BookMapper;
import com.jkj.domain.Book;
import com.jkj.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public Boolean save(Book book) {
        return bookMapper.insert(book)>0;
    }

    @Override
    public Boolean update(Book book) {
        return bookMapper.updateById(book)>0;
    }

    @Override
    public Boolean delete(Integer id) {
        return bookMapper.deleteById(id)>0;
    }

    @Override
    public Book selectById(Integer id) {
        return bookMapper.selectById(id);
    }

    @Override
    public List<Book> SelectAll() {
        return bookMapper.selectList(null);
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        bookMapper.selectPage(page,null);
        return page;
    }
}

测试类

package com.jkj.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BookServiceTest {
    @Autowired(required = true)
    private BookService bookService;

    @Test
    void testGetById(){
        System.out.println(bookService.selectById(1));
    }

    @Test
    void testSave(){
        Book book = new Book();
        book.setType("测试");
        book.setName("测试");
        book.setDescription("测试");
        bookService.save(book);
    }

    @Test
    void testUpdate(){
        Book book = new Book();
        book.setId(11);
        book.setType("测试1");
        book.setName("测试1");
        book.setDescription("测试1");
        bookService.update(book);
    }

    @Test
    void testDelete(){
        bookService.delete(14);
    }

    @Test
    void testGetAll(){
        bookService.SelectAll();
    }

    @Test
    void testGetPage(){
        IPage<Book> page = new Page<Book>(2,5);

        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getRecords());
    }
}

MyBatis-Plus快速开发

  • 使用MyBatisPlus提供有业务层通用接口(ISerivce)与业务层通用实现类(ServiceImpl)
  • 在通用类基础上做功能重载或功能追加
  • 注意重载时不要覆盖原始操作,避免原始提供的功能丢失
    service层接口定义
package com.jkj.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.jkj.domain.Book;

public interface IBookService extends IService<Book> {
}

实现类

package com.jkj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkj.dao.BookMapper;
import com.jkj.domain.Book;
import com.jkj.service.IBookService;
import org.springframework.stereotype.Service;
@Service
public class IBookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
}

测试类

package com.jkj.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class IBookServiceTest {
    @Autowired
    private IBookService iBookService;

    @Test
    void save(){
        Book b=new Book();
        b.setType("文学");
        b.setName("白鹿原");
        b.setDescription("好看");
        iBookService.save(b);
    }
    @Test
    void update(){
        Book b=new Book();
        b.setId(15);
        b.setType("文学");
        b.setName("白鹿原");
        b.setDescription("666");
        iBookService.updateById(b);
    }
    @Test
    void delete(){
        iBookService.removeById(15);
    }
    @Test
    void findById(){
        System.out.println(iBookService.getById(1));

    }
    @Test
    void findAll(){
        iBookService.list();
    }
    @Test
    void page(){
        IPage<Book> page = new Page<Book>(2,5);
        iBookService.page(page);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getRecords());

    }
}

✨表现层开发(前后端数据协调统一)

  • 基于Restful进行表现层接口开发 (目前正在更新可到这里查看SpringBoot专栏)
  • 使用Postman测试表现层接口功能(目前正在更新可到这里查看SpringBoot专栏)

标准版开发

编写Controller类

package com.jkj.controller;

import com.jkj.domain.Book;
import com.jkj.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*@RestController
@RequestMapping("/book")*/
public class BookController {
    @Autowired
    private BookService bookService;
    @GetMapping
    public List<Book> selectAll(){
        return bookService.SelectAll();
    }
    @PostMapping
    public Boolean save(@RequestBody Book book){
        return bookService.save(book);
    }
    @PutMapping
    public Boolean update(@RequestBody Book book){
        return bookService.update(book);
    }
    @DeleteMapping("{id}")
    public Boolean delete(@PathVariable Integer id){
        return bookService.delete(id);
    }
    @GetMapping("{id}")
    public Book selectById(@PathVariable Integer id){
        return bookService.selectById(id);
    }
}

启动postman测试
查全部
SpringBoot的SSMP案例(后端开发)_第8张图片
通过id查询
SpringBoot的SSMP案例(后端开发)_第9张图片
添增
SpringBoot的SSMP案例(后端开发)_第10张图片
修改
SpringBoot的SSMP案例(后端开发)_第11张图片
修改之后再查询一次
SpringBoot的SSMP案例(后端开发)_第12张图片
删除
SpringBoot的SSMP案例(后端开发)_第13张图片

表现层消息一致性处理(前后端分离)

  • 设计表现层返回结果模型类,用于后端与前端进行数据格式统一,也称为前后端数据协议。

编写R.java

package com.jkj.controller.utils;

import lombok.Data;

@Data
public class R {
    private Boolean flag;
    private Object data;

    public R(){}

    public R(Boolean flag){
        this.flag = flag;
    }

    public R(Boolean flag,Object data){
        this.flag = flag;
        this.data = data;
    }
}

编写IController类

package com.jkj.controller;
import com.jkj.domain.Book;
import com.jkj.controller.utils.R;
import com.jkj.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class IBookController {
    @Autowired
    private IBookService iBookService;
    @GetMapping
    public R selectAll(){
        return new R(true,iBookService.list());
    }
    @PostMapping
    public R save(@RequestBody Book book){
        /*R r = new R();
        boolean flag = iBookService.save(book);
        r.setFlag(flag);*/
        return new R(iBookService.save(book));
    }
    @PutMapping
    public R update(@RequestBody Book book){
        return new R(iBookService.updateById(book));
    }
    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(iBookService.removeById(id));
    }
    @GetMapping("{id}")
    public R selectById(@PathVariable Integer id){
        return new R(true,iBookService.getById(id));
    }
}

postman测试,如上。

SpringBoot的SSMP案例(后端开发)_第14张图片

你可能感兴趣的:(SpringBoot,spring,boot,后端)