2021-06-30 山东大学2021暑期项目实训记录

今天和同学商量好前端渲染需要的数据,建立了数据库表,
过程中出现了如下图的错误在这里插入图片描述
发现忘记在主程序添加

@MapperScan("com.economy.back.mapper")

或者在UserMapper.java文件添加

@Mapper

使用IDEA的插件RestfulToolkit 进行GETPOST测试(安装后要重启IDEA)在这里插入图片描述

Post,Put,Delete分页查询等接口开发

public interface UserMapper {
    @Select("select * from user")
    List<User> findAll() ;

    @Update("INSERT INTO `user` (`id`, `name`, `password`, `age`, `sex`, `phone`) VALUES (#{id},#{name},#{password},#{age},#{sex},#{phone});")
    @Transactional
    void save(User user);

    @Update("UPDATE `user` SET `name`=#{name},`password`=#{password},`age`=#{age},`sex`=#{sex},`phone`=#{phone} WHERE id=#{id}")
    @Transactional
    void updateById(User user);

    @Delete("delete from user where id=#{id}")
    @Transactional
    void deleteById(Long id);
}
public class UserController {
    @Resource
    UserMapper userMapper;
    @GetMapping
    public List<User> getUser(){
        return userMapper.findAll();
    }
    @PostMapping
    public String addUser(@RequestBody User user){
        userMapper.save(user);
        return "success";
    }
    @PutMapping
    public String updateUser(@RequestBody User user){
        userMapper.updateById(user);
        return "success";
    }
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable("id") Long id){
        userMapper.deleteById(id);
        return "success";
    }
}

接口文档

我们决定用swagger生成在线的接口文档,进行接口测试

swagger配置过程

1.导入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.新建SwaggerConfig类

package com.economy.back.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 加了ApiOperation注解的类,才会生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 指定包下的类,才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("com.economy.back.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger 测试")
                .description("Swagger 测试文档")
                .termsOfServiceUrl("")
                .version("1.0.0")
                .build();
    }
}

3.启动服务,并访问 swagger-ui.html 页面。

访问 http://localhost:8080/swagger-ui.html
  在这里插入图片描述
  在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值