1.创建Springboot+Mybatis 项目,这里为节省时间,使用了之前写的,链接如下:
SpringBoot Mybatis PageHelper 集成
注:这里可测试数据库连接
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {
@Autowired
UserService UserService;
@Test
public void getUserList() {
List<User> userList = UserService.selectByPage(1,2);
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", userList);
System.out.println(jsonObject.toJSONString());
}
}
2.pom.xml依赖文件中添加
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3.启动类中添加如下注解
@EnableSwagger2
4.接口简单修改
@Api(tags = {"用户接口"})
@RestController
public class UserController {
private static Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@ApiOperation(value = "分页查询用户信息")
@PostMapping("selectByPage")
public ModelMap selectByPage(Integer page, Integer rows) {
logger.info("/user/selectByPage开始调用!");
ModelMap model = new ModelMap();
try {
List<User> userList = userService.selectByPage(page, rows);
// 获取分页查询后的数据
PageInfo<User> pageInfo = new PageInfo<User>(userList);
model.put("data", userList);
// 总记录数
model.put("total", pageInfo.getTotal());
// 当前页
model.put("page", pageInfo.getPageNum());
} catch (Exception e) {
logger.info("/user/selectByPag出现异常!!");
}
return model;
}
}
5.测试
浏览器访问 http://localhost:11111/swagger-ui.html#/
点击User Controller列表,再点击Try it out,填写数据,点击Execute按钮
结果如下: