• 使用MybatisPlus快速进行增删改查


    使用MybatisPlus快速进行增删改查

    前言:mybatisplus 可以说是对mybatis更好的拓展,一些简单的增删改查的操作已经被作者实现,我们只需引用即可。

    1.数据库建表

    这里使用的是MySQL数据库,表名为student

    在这里插入图片描述

    在这里插入图片描述

    2.新建一个springboot项目

    这里使用的idea

    (1)、引入相应的jar包

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    修改一下springboot的版本 最好与此一致,其他版本不确定是否兼容

    在这里插入图片描述

    这里如有需要复制时,注意空白格,直接复制可能会报错

        
            org.springframework.boot
            spring-boot-starter-parent
            2.2.6.RELEASE
            
        
    
     
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
                mysql
                mysql-connector-java
                runtime
            
            
                org.projectlombok
                lombok
                true
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
        
    
    • 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

    (2)、快速启动项目

    在此之前,先看看一看我的项目结构

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vdJ6cRtB-1649861709999)(C:Usersjsj rj201zdpAppDataRoamingTypora	ypora-user-imagest9861657658.png)]

    • 新建一个controller包,在controller包下新建一个HelloController.java

      package com.zhu.controller;

      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;

      @RestController
      @RequestMapping(“/test”)
      public class HelloController {

      @RequestMapping("/hello")
      public String hello(){
          return "hello";
      }
      
      • 1
      • 2
      • 3
      • 4

      }

    • springboot启动类,运行main即可

      package com.zhu;

      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

      @SpringBootApplication
      public class MybatisplusDemoApplication {

      public static void main(String[] args) {
          SpringApplication.run(MybatisplusDemoApplication.class, args);
      }
      
      • 1
      • 2
      • 3

      }

    • 在浏览器(这里使用的谷歌浏览器)中输入地址: http://localhost:8080/test/hello

    至此,一个springboot项目快速启动完成,下面我们需要引入mybatisplus相关依赖

    3.springboot结合mybatisplus

    (1)、引入mybatisplus以及其他依赖

    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.2.0
    
    
        org.springframework.boot
        spring-boot-starter-freemarker
    
    
    
        mysql
        mysql-connector-java
        runtime
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    (2)、创建application.yml文件,修改配置

    # DataSource Config
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/此处为你的数据库名?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
        username: 你的数据库用户名
        password: 你的数据库密码
    mybatis-plus:
      # xml文件扫描
      mapper-locations: classpath*:/mapper/**Mapper.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (3)、创建mybaisplus配置类

    package com.zhu.config;
    
    
    import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    @Configuration
    @MapperScan("com.zhu.mapper")//mapper接口扫描注解
    @EnableTransactionManagement
    public class MyBatisPlusConfig {//分页配置,本博客不展示分页操作
    
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            return paginationInterceptor;
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (4)、创建实体类

    package com.zhu.entity;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import java.io.Serializable;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    /**
     * 

    * *

    * * @author xiaozhu * @since 2022-04-13 */ //使用lombok,简化了代码,不用书写set get等方法 @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class Student implements Serializable { private static final long serialVersionUID = 1L; /** * 自动递增 */ @TableId(value = "sno", type = IdType.AUTO) private Integer sno; private String sname; private String sex; private Integer age; private Integer clas; }
    • 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

    (5)、创建mapper接口

    package com.zhu.mapper;
    
    import com.zhu.entity.Student;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    
    /**
     * 

    * Mapper 接口 *

    * * @author xiaozhu * @since 2022-04-13 */ public interface StudentMapper extends BaseMapper { }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    (6)、创建service接口及其实现类

    package com.zhu.service;
    
    import com.zhu.entity.Student;
    import com.baomidou.mybatisplus.extension.service.IService;
    
    /**
     * 

    * 服务类 *

    * * @author xiaozhu * @since 2022-04-13 */ public interface StudentService extends IService { } package com.zhu.service.impl; import com.zhu.entity.Student; import com.zhu.mapper.StudentMapper; import com.zhu.service.StudentService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; /** *

    * 服务实现类 *

    * * @author xiaozhu * @since 2022-04-13 */ @Service public class StudentServiceImpl extends ServiceImpl implements StudentService { }
    • 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

    (7)、创建controller

    package com.zhu.controller;
    
    
    import com.zhu.entity.Student;
    import com.zhu.service.StudentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * 

    * 前端控制器 *

    * * @author xiaozhu * @since 2022-04-13 */ @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; //返回所有学生 @GetMapping("/allStudent") public List findAllStudent(){ return studentService.list(); } //根据学号查询学生 @GetMapping("/findBySno/{sno}") public Student findBySno(@PathVariable("sno") Integer sno){ return studentService.getById(sno); } //根据学号删除学生信息(此方法可以使用软件postman进行测试) @DeleteMapping("/deleteBySno/{sno}") public boolean deleteBySno(@PathVariable("sno") Integer sno){ return studentService.removeById(sno); } //增加一个学生信息(此方法可以使用软件postman进行测试),注意学号自增 @PostMapping("/add") public boolean add(@RequestBody Student student){ return studentService.save(student); } //根据学号修改学生信息(此方法可以使用软件postman进行测试),注意学号自增 @PutMapping("/update") public boolean update(@RequestBody Student student){ return studentService.updateById(student); } }
    • 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
    • 60
    • 61
    • 62
    • 63
    • 64

    至此,我们完成了mybatisplus的快速的入门级别的增删改查

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

  • 相关阅读:
    Clickhouse冷热配置
    搭建企业社区,如何激发员工互动?
    面试官:MyBatis 插件用途和底层原理
    资源画像,看得见的容器资源优化助手
    R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
    网络新闻营销在企业经营中有多重要?
    携职教育:2023年中级会计考试报名时间出来了吗?在什么时候?
    ROS2系列知识(6):Action服务概念
    动态规划 之 打家劫舍
    Redis 主从搭建
  • 原文地址:https://blog.csdn.net/web18484626332/article/details/126080926