• SpringBoot的SSMP案例(后端开发)


    在这里插入图片描述

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

    目录


    创建数据表

    创建一个tbl_book表
    在这里插入图片描述
    在这里插入图片描述

    IDEA配置MySQL数据库连接

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

    搭建项目

    不同版本的搭建项目在我springboot专栏里,本次项目采用的是阿里云版。模块的创建就不用多说了。
    注意:选择需要的技术
    在这里插入图片描述
    在这里插入图片描述
    Lombok开发工具可以简化实体类的开发。
    在这里插入图片描述
    ??导入druid的依赖

            
                com.alibaba
                druid
                1.2.11
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ??设置服务器端口号:

    server:
      port: 80
    
    • 1
    • 2

    ??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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    数据层开发

    ??编写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_
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ??编写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 {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ??测试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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

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

    mybatis-plus:
      global-config:
        db-config:
          table-prefix: tbl_
          id-type: auto
    
    • 1
    • 2
    • 3
    • 4
    • 5

    开启MP运行日志

    ??编辑yml

    mybatis-plus:
      global-config:
        db-config:
          table-prefix: tbl_
          id-type: auto
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ??测试类

     @Test
        void findAll() {
            bookMapper.selectList(null);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    分页查询

    ??编写拦截器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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ??分页测试

        @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());
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    条件查询

    ??编写测试类
    推荐使用

    • 使用 QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用。

      //推荐使用
      @Test
      void ByCondition1(){
          String name="一";
          LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
          //支持动态拼写查询条件
          lqw.like(name!=null,Book::getName,name);
          bookMapper.selectList(lqw);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      @Test
      void ByCondition(){
      QueryWrapper 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 SelectAll();
      IPage 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 SelectAll() {
            return bookMapper.selectList(null);
        }
    
        @Override
        public IPage getPage(int currentPage, int pageSize) {
            IPage page = new Page(currentPage,pageSize);
            bookMapper.selectPage(page,null);
            return page;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    ??测试类

    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 page = new Page(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());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    ??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 {
      }

    ??实现类

    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 implements IBookService {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ??测试类

    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 page = new Page(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());
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

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

    • 基于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 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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    ??启动postman测试
    查全部
    在这里插入图片描述
    通过id查询
    在这里插入图片描述
    添增
    在这里插入图片描述
    修改
    在这里插入图片描述
    修改之后再查询一次
    在这里插入图片描述
    删除
    在这里插入图片描述

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

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

    ??编写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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    ??编写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));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    ??postman测试,如上。

    在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Linux四种I/O模型
    配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
    计算机网络:数据报与虚电路
    由两个独立的高增益运算放大器组成的运放芯片D258,可应用于音频信号处理系统上
    深入理解CSS常见选择器
    fastjson远程命令执行
    【JVM】垃圾回收机制
    c语言:将链表数据写入到文件,将数据读入链表
    计算机毕业设计ssm小学教师网络培训网站ea3c0系统+程序+源码+lw+远程部署
    CMake中add_executable的使用
  • 原文地址:https://blog.csdn.net/m0_67403188/article/details/126115197