SwaggerConfig
@Configuration//该类为配置类 public class SwaggerConfig { @Bean//swagger所有的类都封装到Docket中 public Docket docket(){ Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo())//设置api文档信息 .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//未指定包下的类生成接口文档 .build(); return docket; } //自定义接口信息 private ApiInfo apiInfo(){ Contact concat = new Contact("王文超","http://www.jd.com","110@qq.com"); ApiInfo apiInfo = new ApiInfo("超哥大标题","描述超哥的帅气","1.0","http://www.baidu.com", concat,"支援","http://www.taobao.com",new ArrayList<>()); return apiInfo; } }
Helloword
@RestController @Api(tags = "hello的接口类") public class Helloword { @GetMapping("/getinfo") @ApiOperation(value = "获取用户信息") public HashMap<Object, Object> getinfo(){ HashMap<Object, Object> hashMap = new HashMap<>(); hashMap.put("name","王文超"); hashMap.put("age",18); return hashMap; } @PostMapping("/login") @ApiOperation(value = "登录接口") @ApiImplicitParams( { @ApiImplicitParam(value = "账号",name="zh",required = true ), @ApiImplicitParam(value = "密码",name="mm",required = true) } ) public CommonResult login(String zh,String mm){ return new CommonResult(2000,"成功",null); } @Autowired private UserMapper userMapper; @PostMapping("/delete") @ApiOperation("删除") @ApiImplicitParam(value = "编号",name="id",required = true) public CommonResult delete(Integer id){ Integer delete = userMapper.delete(id); if (delete!=null){ return new CommonResult(2000,"修改成功",null); } return new CommonResult(5000,"修改失败",null); } @PostMapping("selectAll") @ApiOperation("查询所有") @ApiImplicitParams({ @ApiImplicitParam(value = "账号",name = "username",required = true), @ApiImplicitParam(value = "编号",name = "id"), @ApiImplicitParam(value = "密码",name = "password",required = true) }) public CommonResult seleteAll(){ PageHelper.startPage(1,5); List<User> users = userMapper.selectAll(); PageInfo<User> userPageInfo = new PageInfo<>(users); if (userPageInfo!=null){ return new CommonResult(2000,"查询成功",userPageInfo); } return new CommonResult(5000,"查询失败",userPageInfo); } @PostMapping("/insert") @ApiOperation("添加") @ApiImplicitParams({ @ApiImplicitParam(value = "账号",name = "username",required = true), @ApiImplicitParam(value = "编号",name = "id"), @ApiImplicitParam(value = "密码",name = "password",required = true) }) public CommonResult insert(User user){ Integer insert = userMapper.insert(user); if (insert!=null){ return new CommonResult(2000,"添加成功",null); } return new CommonResult(5000,"添加失败",null); } @PostMapping("/update") @ApiOperation("修改") @ApiImplicitParams({ @ApiImplicitParam(value = "账号",name = "username",required = true), @ApiImplicitParam(value = "编号",name = "id",required = true), @ApiImplicitParam(value = "密码",name = "password",required = true) }) public CommonResult update(User user){ Integer update = userMapper.update(user); if (update!=null){ return new CommonResult(2000,"修改成功",null); } return new CommonResult(5000,"修改失败",null); }
usermapper
@Mapper public interface UserMapper { public User select(Integer id); public List<User> selectAll(); public Integer delete(Integer id); Integer insert(User user); Integer update(User hello); }
DemoApplication
//指定扫描包以及子包 @SpringBootApplication @MapperScan(basePackages = "com.example.demo.dao")//为该接口生成实现代理类 @EnableSwagger2//开启注解 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 测试类
@SpringBootTest class DemoApplicationTests { @Autowired private DataSource dataSource; @Autowired private UserMapper userMapper; @Test public void text(){ System.out.println(userMapper.select(14)); } @Test public void text1(){ PageHelper.startPage(1,5); List<User> users = userMapper.selectAll(); PageInfo<User> userPageInfo = new PageInfo<>(users); System.out.println("当前页码"+userPageInfo.getPageNum()); System.out.println("总页码"+userPageInfo.getPages()); System.out.println("总条数"+userPageInfo.getTotal()); System.out.println("当前页码的记录"+userPageInfo.getList()); } @Test public void text01() throws SQLException{ System.out.println(dataSource.getConnection()); } @Test public void delete(){ userMapper.delete(14); } @Test public void insert(){ User user = new User(null,"王文超", "18"); userMapper.insert(user); } @Test public void update(){ User hello = new User(33, "你好", "56"); userMapper.update(hello); } }