今天和同学商量好前端渲染需要的数据,建立了数据库表,
过程中出现了如下图的错误
发现忘记在主程序添加
@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