快速开始(Spring Boot 2 + OpenAPI2+Swagger2)增删改查

1.创建Spring Boot项目并且在pom.xml中引入Knife4j的依赖包,代码如下:

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.9</version>
</dependency>

2.创建Swagger配置依赖,代码如下: 

//Knife4jConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    @Bean
    public Docket docket(){
            return  new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo.demos.controller"))
                    .paths(PathSelectors.any())
                    .build();

    }

    private ApiInfo apiInfo() {
        Contact author = new Contact("姓名", "地址", "邮箱");
        return new ApiInfo(
                "xx文档",
                "xx文档",
                "1.0",
                "",
                author,
                "",
                "",
                new ArrayList()
        );
    }

}

3.加入配置yml

  spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

4.编写实体类,用于映射数据库中的表。 

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Table(value = "mazuinformation")
@ApiModel(description = "庙宇信息")
public class Information {

    @ApiModelProperty(value = "主键ID")
    private Integer id;

    @ApiModelProperty(value = "庙名")
    private String mazuName;

    @ApiModelProperty(value = "建筑年代")
    private String constructionYear;

    @ApiModelProperty(value = "面积")
    private String area;

    @ApiModelProperty(value = "地址")
    private String address;

    @ApiModelProperty(value = "描述")
    private String description;
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMazuName() {
        return mazuName;
    }

    public void setMazuName(String mazuName) {
        this.mazuName = mazuName;
    }

    public String getConstructionYear() {
        return constructionYear;
    }

    public void setConstructionYear(String constructionYear) {
        this.constructionYear = constructionYear;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

5.编写MyBatis映射器接口,用于定义与数据库交互的SQL语句。

@Mapper
public interface MazuMapper {

    // 根据庙名查询
    @Select("SELECT * FROM mazuinformation WHERE mazuName = #{mazuName}")
    List<Information> findByMazuName(String mazuName);

    // 查询所有记录
    @Select("SELECT * FROM mazuinformation")
    List<Information> findAll();

    // 根据ID查询
    @Select("SELECT * FROM mazuinformation WHERE id = #{id}")
    Information findById(Integer id);

    // 插入新记录
    @Insert("INSERT INTO mazuinformation(mazuName, constructionYear, area, address, description) VALUES(#{mazuName}, #{constructionYear}, #{area}, #{address}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Information information);

    // 更新记录
    @Update("UPDATE mazuinformation SET mazuName=#{mazuName}, constructionYear=#{constructionYear}, area=#{area}, address=#{address}, description=#{description} WHERE id=#{id}")
    void update(Information information);

    // 删除记录
    @Delete("DELETE FROM mazuinformation WHERE id = #{id}")
    void deleteById(Integer id);
}

6.编写控制器,用于处理相关的HTTP请求。 

@RestController
@RequestMapping("/api/mazu")
public class MazuCRUDController {

    @Autowired
    private MazuService mazuService;

    // 获取所有记录
    @GetMapping
    public List<Information> getAllInformation() {
        return mazuService.getAllInformation();
    }

    // 根据ID获取记录
    @GetMapping("/{id}")
    public Information getInformationById(@PathVariable Integer id) {
        return mazuService.getInformationById(id);
    }

    // 根据庙名查询记录
    @GetMapping("/search")
    public List<Information> getInformationByMazuName(@RequestParam String mazuName) {
        return mazuService.getInformationByMazuName(mazuName);
    }

    // 插入新记录
    @PostMapping("/insert")
    public void insertInformation(@ModelAttribute Information information) {
        mazuService.insertInformation(information);
    }

    // 更新现有记录
    @PutMapping("/update")
    public void updateInformation(@ModelAttribute Information information) {
        mazuService.updateInformation(information);
    }

    // 删除记录
    @DeleteMapping("/{id}")
    public void deleteInformation(@PathVariable Integer id) {
        mazuService.deleteInformation(id);
    }
}

7.编写服务层接口,这些方法用于处理与信息相关的业务逻辑。 

public interface MazuService {
    List<Information> getAllInformation();
    Information getInformationById(Integer id);
    List<Information> getInformationByMazuName(String mazuName);
    void insertInformation(Information information);  // 新增插入方法
    void updateInformation(Information information);  // 新增更新方法
    void deleteInformation(Integer id);
}

8.编写MazuServiceImpl类实现MazuService接口,并提供了上述所有方法的实际实现


@Service
public class MazuServiceImpl implements MazuService {

    @Autowired
    private MazuMapper mazuMapper;

    @Override
    public List<Information> getAllInformation() {
        return mazuMapper.findAll();
    }

    @Override
    public Information getInformationById(Integer id) {
        return mazuMapper.findById(id);
    }

    @Override
    public List<Information> getInformationByMazuName(String mazuName) {
        return mazuMapper.findByMazuName(mazuName);
    }

    @Override
    public void insertInformation(Information information) {
        mazuMapper.insert(information);  // 插入操作
    }

    @Override
    public void updateInformation(Information information) {
        mazuMapper.update(information);  // 更新操作
    }

    @Override
    public void deleteInformation(Integer id) {
        mazuMapper.deleteById(id);
    }
}

9.编写启动类  

@SpringBootApplication 
@MapperScan("com.springbootdemo.obg.Mapper")
@EnableTransactionManagement
public class Demo3Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo3Application.class, args);
    }
}

10.启动后访问http://localhost:8080/doc.html#/home查看运行结果(端口号以自己为准)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值