springboot整合swagger接口文档与mybatis-plus

springboot整合swagger接口文档与mybatis-plus

1.整合swagger接口文档

接口文档的作用: 就是为了方便前后端的交互。

(1)导入swagger2依赖
localhost:8080/doc.html:打开路径

 <!--swagger的依赖引入-->
        <dependency>
            <groupId>io.github.jianzhichun</groupId>
            <artifactId>spring-boot-starter-swagger2</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

(2)创建一个swagger配置类

package com.aaa.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("组名")
                //设置文档的信息
                .apiInfo(getInfo())
                .select()
                //指定未哪些包生成接口文档。
                .apis(RequestHandlerSelectors.basePackage("com.aaa.controller"))
                .build();
        return docket;
    }

    private ApiInfo getInfo(){
        Contact DEFAULT_CONTACT = new Contact("联系人姓名", "网址", "邮箱");
        ApiInfo apiInfo = new ApiInfo("学生管理系统API文档", "学生管理系统API文档", "2.0", "文档打开路径",
                DEFAULT_CONTACT, "公司名", "公司邮箱");
        return apiInfo;
    }
}

(3)开启swagger2的注解驱动
在这里插入图片描述
(4)测试:
http://localhost:8080/doc.html

(5)使用的注解:

@Api:接口类的说明 加在controller类上

@ApiOperation: 接口方法的说明。 加在controller方法上

@ApiImplicitParams: 接口方法的所有参数的说明.

​ @ApiImplicitParam:单个参数的说明

​ --name: 参数名

​ --value: 参数的说明

​ --require: 是否为必须的

​ --dataType: 参数类型说明 int string

@ApiModel: 实体类的说明

@ApiModelProperty: 单个参数的说明

2.整合mybatis-plus

(1)导入mybatis-plus依赖

 <!--mp的依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>

(2)application.properties

#数据源信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# characterEncoding防止您添加到数据的数据出现乱码。
spring.datasource.url=jdbc:mysql://localhost:3306/库名?serverTimezone=Asia/Shanghai&characterEncoding=UTF8
spring.datasource.username=账号
spring.datasource.password=密码

#指定映射文件的路径--链表操作
mybatis-plus.mapper-locations=classpath:/mapper/*.xml

#sql日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

(3)表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` int(0) NOT NULL,
  `name` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `phone` varchar(11) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `email` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `profession` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `age` int(0) NULL DEFAULT NULL,
  `gender` tinyint(0) NULL DEFAULT NULL,
  `status` tinyint(0) NULL DEFAULT NULL,
  `createtime` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `phone_index`(`phone`) USING BTREE,
  INDEX `name_index`(`name`) USING BTREE,
  INDEX `pro_age_status_index`(`profession`, `age`, `status`) USING BTREE,
  INDEX `email_index`(`email`) USING BTREE,
  INDEX `gender_index`(`gender`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '吕布', '17799990000', 'lvbu666@163.com', '软件工程', 23, 1, 6, '2001-02-02 00:00:00');
INSERT INTO `student` VALUES (2, '曹操', '17799990001', 'caocao666@qq.com', '通信工程', 33, 1, 0, '2001-03-05 00:00:00');
INSERT INTO `student` VALUES (3, '赵云', '17799990002', '17799990@139.com', '英语', 34, 1, 2, '2002-03-02 00:00:00');
INSERT INTO `student` VALUES (4, '孙悟空', '17799990003', '17799990@sina.com', '工程造价', 54, 1, 0, '2001-07-02 00:00:00');
INSERT INTO `student` VALUES (5, '花木兰', '17799990004', '199980729@sina.com', '软件工程', 23, 2, 1, '2001-04-22 00:00:00');
INSERT INTO `student` VALUES (6, '大乔', '17799990005', 'daqiao666@sina.com', '舞蹈', 22, 2, 0, '2001-02-07 00:00:00');
INSERT INTO `student` VALUES (7, '露娜', '17799990006', 'luna_love@sina.com', '应用数学', 24, 2, 0, '2001-02-08 00:00:00');
INSERT INTO `student` VALUES (8, '程咬金', '17799990007', 'chengyaojin@163.com', '化工', 38, 1, 5, '2001-05-23 00:00:00');
INSERT INTO `student` VALUES (9, '项羽', '17799990008', 'xiaoyu666@qq.com', '金属材料', 43, 1, 0, '2022-08-25 17:00:45');
INSERT INTO `student` VALUES (10, '白起', '17799990009', 'baiqi666@sina.com', '机械工程及其自动化', 27, 1, 2, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (11, '韩信', '17799990010', 'hanxin520@163.com', '无机非金属材料工程', 27, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (12, '荆轲', '17799990011', 'jinke123@163.com', '会计', 29, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (13, '兰陵王', '17799990012', 'lanlinwang666@126.com', '工程造价', 44, 1, 1, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (14, '狂铁', '17799990013', 'kuangtie@sina.com', '应用数学', 43, 1, 2, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (15, '貂蝉', '17799990014', '84958948374@qq.com', '软件工程', 40, 1, 3, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (16, '妲己', '17799990015', '2783238293@qq.com', '软件工程', 31, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (17, '芈月', '17799990016', 'xiaomin2001@sina.com', '工业经济', 35, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (18, '嬴政', '17799990017', '8839434342@qq.com', '化工', 38, 1, 1, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (19, '狄仁杰', '17799990018', 'jujiamlm8166@163.com', '国际贸易', 30, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (20, '安琪拉', '17799990019', 'jdodmlh@126.com', '城市规划', 51, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (21, '典韦', '17799990020', 'ycaunanjian@163.com', '城市规划', 52, 1, 2, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (22, '廉颇', '17799990021', 'lianpo321@126.com', '土木工程', 19, 1, 3, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (23, '后裔', '17799990022', 'altycj2000@139.com', '城市园林', 20, 1, 0, '2022-08-25 17:23:51');
INSERT INTO `student` VALUES (24, '姜子牙', '17799990023', '37483844@qq.com', '工程造价', 29, 1, 4, '2022-08-25 17:23:51');

SET FOREIGN_KEY_CHECKS = 1;

(4)实体类
表名和实体类名不一致: 使用@TableName(value=“表名”)
列名和实体类名不一致: 使用@TableField(value=“列名”)
主键和实体的不一致| 不叫id 使用@TableId(value=“主键列”)
如果实体类中出现某个属性 不属于表里面的字段。 @TableField(exist = false)

package com.aaa.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "学生类")
public class Student {
    @TableId(type = IdType.AUTO)
    @ApiModelProperty(value = "学生编号")
    private Integer id;
    @ApiModelProperty(value = "学上姓名")

    private String name;
    @ApiModelProperty(value = "手机号码")
    private String phone;
    @ApiModelProperty(value = "邮箱地址")
    private String email;
    @ApiModelProperty(value = "专业院系")
    private String profession;
    @ApiModelProperty(value = "学上年龄")
    private Integer age;
    @ApiModelProperty(value = "学上性别")
    private Integer gender;
    @ApiModelProperty(value = "学上身份")
    private Integer status;
    @ApiModelProperty(value = "出生日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    private Date createtime;

}

(5)dao接口

package com.aaa.dao;

import com.aaa.entity.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface StudentsDao extends BaseMapper<Student> {
}

(6) 扫描dao接口
在这里插入图片描述

(7)测试

package com.aaa;

import com.aaa.dao.StudentsDao;
import com.aaa.entity.Student;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
class Springboot03ApplicationTests {

    @Autowired
    private StudentsDao studentsDao;

    /**
     * 测试根据id查询用户信息
     */
    @Test
    void testSelectById(){
        Student student = studentsDao.selectById(1);
        System.out.println(student);
    }

    /**
     * 测试添加方法
     */
    @Test
    void testInsert(){

        Student student = new Student(null, "戈雅", "17513145220", "2024@qq.com", "软件工程",
                22, 1, 0, new Date());
        studentsDao.insert(student);
        System.out.println(student);
    }

    /**
     * 测试修改
     */
    @Test
    void testUpdate(){
        Student student = new Student(25, "赵怀真", "17513145220", "2024@qq.com", "软件工程",
                22, 1, 0, new Date());
        //studentsDao.updateById(student);
       //mp封装了一个条件类 每个条件对应一个方法:Wrapper---UpdateWrapper[增删改的条件]  QueryWrapper[查询的条件]
        UpdateWrapper<Student> wrapper = new UpdateWrapper<>();
        //where name=赵怀真
        wrapper.eq("name","赵怀真");
        //where age>18
        wrapper.ge("age",18);
        //where age<25
        wrapper.lt("age",30);
        // where age between 18 and 25
        wrapper.between("age",18,30);
        //where name like '赵%'
        wrapper.likeRight("name","赵");
        //where name like '%怀%'
        wrapper.like("name","怀");
        //where name != 赵怀真
        wrapper.ne("name","赵怀真");
        //where name not like '%怀%' or name <> '赵怀真';
        wrapper.notLike("name","怀");
        wrapper.or();
        wrapper.ne("name","赵怀真");
        studentsDao.update(student,wrapper);
    }

    /**
     * 测试删除
     */
    @Test
    void testDeleteById(){
        //根据id删除
        int deleteById = studentsDao.deleteById(25);
        //批量删除
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(1l);
        ids.add(10l);
        ids.add(25l);
        studentsDao.deleteBatchIds(ids);
        //根据其他条件删除  等同于修改
        UpdateWrapper<Student> wrapper = new UpdateWrapper<>();
        wrapper.likeRight("name","赵");
        studentsDao.delete(wrapper);
    }

    /**
     * 根据条件多条记录
     */

    @Test
    public void testSelectAll(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.like("name","王");
        List<Student> users = studentsDao.selectList(wrapper);
        System.out.println(users);
    }
    /**
     * 分页查询. 如果想让分页有效 必须配置分页拦截器。
     */
    @Test
    public void testSelectByPage(){
        IPage<Student> iPage=new Page(1,3);
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //Page:分页对象  Wrapper:条件对象
        IPage<Student> page = studentsDao.selectPage(iPage, wrapper);
        System.out.println("总条数:"+page.getTotal());
        System.out.println("总页数:"+page.getPages());
        System.out.println("当前页的记录:"+page.getRecords());
    }


    @Test
    void contextLoads() {
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值