当前位置:   article > 正文

spring boot + mybatis-plus + MySql建立接口_mysql+mybatis+spring 写接口

mysql+mybatis+spring 写接口

1、项目结构

在这里插入图片描述

2、pom配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.school</groupId>
    <artifactId>study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


<!--        swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!--        不加这个就报Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>


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

        <!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.1</version>
            </plugin>
        </plugins>
    </build>

</project>

  • 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
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

3、application配置

spring:
  profiles:
    active: dev
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
server:
  port: 9000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4、书写实体类

package com.school.study.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2021/12/10 14:15
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */

@ApiModel("用户实体类")  //swagger-ui
@Data //等同于set/get方法
@AllArgsConstructor  //构造函数
@NoArgsConstructor  // 生成一个无参数的构造方法
public class user {
    @ApiModelProperty(value = "用户id唯一主键")
    @TableId(type = IdType.AUTO)
    private Integer id;
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private Integer password;

}

  • 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

5、书写Mapper接口

package com.school.study.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.school.study.pojo.user;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2021/12/10 14:27
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */

@Mapper
public interface userMapper extends BaseMapper<user> {
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
继承了BaseMapper类,类中已经写好了增删改查的方法ctrl点击打开BaseMapper就可以看见这些方法,当然也可以自己重写这些方法
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;

public interface BaseMapper<T> {
    int insert(T var1);

    int deleteById(Serializable var1);

    int deleteByMap(@Param("cm") Map<String, Object> var1);

    int delete(@Param("ew") Wrapper<T> var1);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> var1);

    int updateById(@Param("et") T var1);

    int update(@Param("et") T var1, @Param("ew") Wrapper<T> var2);

    T selectById(Serializable var1);

    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> var1);

    List<T> selectByMap(@Param("cm") Map<String, Object> var1);

    T selectOne(@Param("ew") Wrapper<T> var1);

    Integer selectCount(@Param("ew") Wrapper<T> var1);

    List<T> selectList(@Param("ew") Wrapper<T> var1);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1);

    List<Object> selectObjs(@Param("ew") Wrapper<T> var1);

    IPage<T> selectPage(IPage<T> var1, @Param("ew") Wrapper<T> var2);

    IPage<Map<String, Object>> selectMapsPage(IPage<T> var1, @Param("ew") Wrapper<T> var2);
}


  • 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

6、书写Service接口


package com.school.study.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.school.study.pojo.user;
import org.springframework.stereotype.Service;


/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2021/12/10 14:32
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */

public interface userService extends IService<user> {
     //增加
    boolean addUser(user user);
     //删除
    boolean deleteUser(int id);
    //更新
    boolean updateUser(user user);
    //查找
    user findUserById(int 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

7、书写service的impl类

package com.school.study.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.study.mapper.userMapper;
import com.school.study.pojo.user;
import com.school.study.service.userService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2021/12/10 14:43
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */

@Service
public class userServiceImpl extends ServiceImpl<userMapper, user> implements userService {

    //增删改返回布尔值,判断是否成功
    @Override
    public boolean addUser(user user) {
        int i = baseMapper.insert(user);
        return i > 0;

    }

    @Override
    public boolean deleteUser(int id) {
        int i = baseMapper.deleteById(id);
        return i > 0;

    }

    @Override
    public boolean updateUser(user user) {
        int i = baseMapper.updateById(user);
        return i > 0;
    }

    @Override
    public user findUserById(int id) {
        return baseMapper.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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

8、书写Controller类

package com.school.study.controller;


import com.school.study.pojo.user;
import com.school.study.response.CommonResponse;
import com.school.study.service.userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2021/12/10 15:00
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */

@Api(tags = "用户信息类")
@RestController
public class userController {

    @Autowired
    private userService service;

    @ApiOperation("用Id获取用户信息")
    @GetMapping("/findById")
    public CommonResponse findById(int id){
       return CommonResponse.ok().data("items",  service.findUserById(id));
    }


    //要写RequestBody
    @ApiOperation("插入数据")
    @PostMapping("/insert")
    public CommonResponse insert(@RequestBody user user){
       boolean flag = service.addUser(user);
        if (flag){
            return CommonResponse.ok().data("items", "插入成功");
        } else {
            return CommonResponse.error().data("items", "插入失败");
        }
    }

    @ApiOperation("删除数据")
    @PostMapping("/delete")
    public CommonResponse delete(int id){
        boolean flag = service.deleteUser(id);
        if (flag){
            return CommonResponse.ok().data("items", "删除成功");
        } else {
            return CommonResponse.error().data("items", "删除失败");
        }
    }

    @ApiOperation("更新数据")
    @PostMapping("/upDate")
    public CommonResponse upDate(@RequestBody user user){
        boolean flag = service.updateUser(user);
        if (flag){
            return CommonResponse.ok().data("items", "更新成功");
        } else {
            return CommonResponse.error().data("items", "更新失败");
        }
    }

}

  • 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
  • 65
  • 66
  • 67
  • 68

9、使用swagger进行测试

1.数据库数据

在这里插入图片描述

2.增加数据

在这里插入图片描述

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

我的id使用的是自增的,保证唯一性,剩下的就不演示了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/1009435
推荐阅读
相关标签
  

闽ICP备14008679号