springboot+mybatis+swagger+lombok+增删改查分页

本文详细介绍如何在SpringBoot项目中整合MyBatis,包括项目搭建、依赖配置、实体类与Mapper接口定义、Service层实现及Controller层接口设计,最后通过Swagger进行API文档生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

懒得创建项目的:完整项目地址
如果只看最简单的增删改查:文章地址

新键项目点击下一步开始
在这里插入图片描述
输入组名和项目模块名

在这里插入图片描述

加入热部署和lombok

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

加入mysql 依赖在这里插入图片描述

点击next-finishi 完成创建
目录结构

Pom中添加依赖

<!-- properties 中添加 -->
<mybatis.version>2.1.1</mybatis.version>
<pagehelper.version>1.2.5</pagehelper.version>
<swagger.version>2.9.2</swagger.version>

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>${pagehelper.version}</version>
</dependency>


<!--    加入mybatis    -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis.version}</version>
</dependency>

修改application的后缀为yml,添加配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/mydb2?serverTimezone=UTC
  thymeleaf:
    cache: false
mybatis:
  type-aliases-package: com.shp.dev
  mapper-locations: classpath:mapper/*.xml
logging:
  level:
    com:
      shp:
        dev: debug


pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countsql

server:
  port: 80


创建以下目录和类:

在这里插入图片描述
StudentMapper

package com.shp.dev.spingbootmybatis.student.mapper;

/**
 * @Auther shp
 * @data2020/4/1211:15
 * @description
 */

import com.shp.dev.spingbootmybatis.student.pojo.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {

    @Select("select * from student s where s.name like #{name} and age like #{age} and gender like #{gender} and className like #{className}")
    List<Student> find(Student student);

    @Insert("INSERT INTO student (name,age,gender,className)VALUES(#{name},#{age},#{gender},#{className});")
    void add(Student student);

    @Delete("DELETE FROM student WHERE id=#{id}")
    void del(Student student);

    @Update(" UPDATE student SET name=#{name},age=#{age},gender=#{gender},className=#{className} where id=#{id}")
    void edit(Student student);


}


Student

package com.shp.dev.spingbootmybatis.student.pojo;

/**
 * @Auther shp
 * @data2020/4/1211:14
 * @description
 */

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class Student {
    @ApiModelProperty("主键")
    private Long id;
    @ApiModelProperty("名字")
    private String name;
    @ApiModelProperty("年龄")
    private String age;
    @ApiModelProperty("性别")
    private String gender;
    @ApiModelProperty("班级")
    private String className;

}


IstudentService

package com.shp.dev.spingbootmybatis.student.service;


import com.shp.dev.spingbootmybatis.common.PageInfo;
import com.shp.dev.spingbootmybatis.student.pojo.Student;

/**
 * @Auther shp
 * @data2020/4/1211:17
 * @description
 */
public interface IStudentService {


    PageInfo<Student> findAll(PageInfo pageInfo, Student student);

    void add(Student student);
    void del(Student student);
    void edit(Student student);

}


StudentService

package com.shp.dev.spingbootmybatis.student.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shp.dev.spingbootmybatis.common.PageInfo;
import com.shp.dev.spingbootmybatis.student.mapper.StudentMapper;
import com.shp.dev.spingbootmybatis.student.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther shp
 * @data2020/4/1211:17
 * @description
 */
@Service
public class StudentService implements IStudentService{


    @Autowired
    private StudentMapper mapper;


    String To(String str){
        return "%"+(str==null||str.equals("")||str==""||str.equals("null")||str=="null"?"":str)+"%";
    }


    @Override
    public PageInfo<Student> findAll(PageInfo pageInfo, Student student) {
        student.setAge(To(student.getAge()));
        student.setClassName(To(student.getClassName()));
        student.setGender(To(student.getGender()));
        student.setName(To(student.getName()));

        //查分页
        Page<Student> page= PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());
        List<Student> caseInputs = mapper.find(student);
        //储存值。
        pageInfo.setPage(pageInfo.getPage());
        pageInfo.setLimit(pageInfo.getLimit());
        pageInfo.setCount(page.getTotal());
        pageInfo.setData(caseInputs);
        return pageInfo;

    }

    @Override
    public void add(Student student) {
        mapper.add(student);
    }

    @Override
    public void del(Student student) {
        mapper.del(student);
    }

    @Override
    public void edit(Student student) {
        mapper.edit(student);
    }
}


StudentController

package com.shp.dev.spingbootmybatis.student.web;

import com.shp.dev.spingbootmybatis.common.PageInfo;
import com.shp.dev.spingbootmybatis.student.pojo.Student;
import com.shp.dev.spingbootmybatis.student.service.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther shp
 * @data2020/4/1211:56
 * @description
 */

@RestController
@RequestMapping("student")
@Api(tags = "学生管理相关接口")
public class StudentController {


    @Autowired
    private IStudentService service;

    @ApiOperation("学生的添加接口")
    @RequestMapping("add")
    public void add(Student student) {
        service.add(student);
    }

    @ApiOperation("删除学生的接口")
    //@ApiImplicitParam(name = "ids", value = "删除的id" , required = true)//required = true为必填项
    @RequestMapping("del")
    public void del(Student student) {
        service.del(student);
    }

    @ApiOperation("学生的添加接口")
    @RequestMapping("edit")
    public void edit(Student student) {
        service.edit(student);
    }

    @ApiOperation("学生的分页查询接口")
    @RequestMapping("findAll")
    public PageInfo<Student> findAll(PageInfo pageInfo, Student student) {
        return service.findAll(pageInfo,student);
    }


}


SwaggerConfig

package com.shp.dev.springdatajpademo.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Auther shp
 * @data2020/4/1117:03
 * @description
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.shp.dev"))//扫描控制层的包
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("swagger")//标题
                        .description("")//详细内容
                        .version("1.0")//版本号
                        .contact(new Contact("shp","www.shp.com","2948299576@qq.com"))//作者--地址--邮箱
                        .license("")
                        .licenseUrl("")//网址
                        .build());
    }
}


PageInfo

package com.shp.dev.spingbootmybatis.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@Data
@ApiModel
public class PageInfo<T> {

    @ApiModelProperty("当前页")
    private int page;
    @ApiModelProperty("每页展示的条数")
    private int limit;
    @ApiModelProperty("所有条数")
    private Long count;
    @ApiModelProperty("数据")
    private List<T> data;


}


ResultInfo

package com.shp.dev.spingbootmybatis.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
@Data
@ApiModel
public class ResultInfo {
    @ApiModelProperty("返回结果 true或者false")
    private boolean success=true;
    @ApiModelProperty("返回信息")
    private String msg="";
    @ApiModelProperty("结果集")
    private List data;

}
package com.shp.dev.spingbootmybatis.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
@Data
@ApiModel
public class ResultInfo {
    @ApiModelProperty("返回结果 true或者false")
    private boolean success=true;
    @ApiModelProperty("返回信息")
    private String msg="";
    @ApiModelProperty("结果集")
    private List data;

}


在SpingbootMybatisApplication中添加注解扫描包
@ComponentScan(“com.shp.dev”)
访问地址:http://localhost:80/swagger-ui.html

最终结果:

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

转瞬即逝的记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值