UserServiceImpl实现类
@Service @Transactional public class UserServiceImpl implements UserService { //注入 @Autowired private UserMapper userMapper; /** * 查询所有用户 */ @Override public List<User> getAllUser() { List<User> users = userMapper.selectByExample(null); return users; } /** * 根据id查询用户 */ @Override public User getById(Integer id) { User user = userMapper.selectByPrimaryKey(id); return user; } /** * 添加用户 * @param user */ @Override public void saveUser(User user) { userMapper.insertSelective(user); } /** * 校验用户名是否存在 * @param userName * @return * 数据库没有这条记录,count==0,返回true */ @Override public Boolean checkUserName(String userName) { UserExample example=new UserExample(); UserExample.Criteria criteria=example.createCriteria(); criteria.andUsernameEqualTo(userName); long count=userMapper.countByExample(example); if(count==0){ return true; } return false; } /** * 修改用户 * @param user */ @Override public void updateUser(User user) { userMapper.updateByPrimaryKeySelective(user); } /** * 根据id删除(单个) * @param id */ @Override public void deleteUser(Integer id) { userMapper.deleteByPrimaryKey(id); } /** * 批量删除 * @param useridList */ @Override public void deleteBatchUser(List<Integer> useridList) { /* UserExample example=new UserExample(); UserExample.Criteria criteria=example.createCriteria(); criteria.andUseridIn(useridList); userMapper.deleteByExample(example);*/ } } UserController @RestController @RequestMapping(value = "/user") public class UserController { //注入 @Autowired private UserService userService; /** * 查询所有用户 */ @ApiOperation(value="获取用户列表") @RequestMapping(value = "/user",method = RequestMethod.GET) public List<User> getListAll(){ List<User> listAll = userService.getAllUser(); return listAll; } /** * 用户保存 * @return */ @ApiOperation(value = "添加用户",notes = "根据user添加用户") @ApiImplicitParam(name = "user",value = "用户user",required = true,dataType = "User") @RequestMapping(value = "/users",method = RequestMethod.POST) public String saveUser(@RequestBody User user){ userService.saveUser(user); return "success"; } /** * 根据id查询 */ @ApiOperation(value = "根据id查询") @ApiImplicitParam(name = "id",value = "用户id") @RequestMapping(value = "/{id}",method = RequestMethod.GET) public User getById(@PathVariable("id") Integer id){ User user = userService.getById(id); return user; } /** * 校验用户名 * @param username * @return */ @ApiOperation(value = "校验用户名") @ApiImplicitParam(name = "userName",value = "用户名",required = true,dataType = "String") @RequestMapping(value = "/{username}",method = RequestMethod.POST) public Boolean checkUserName(@PathVariable("username")String username){ Boolean aboolean = userService.checkUserName(username); if (aboolean){ return true; }else { return false; } } /** * 修改用户 * @param user */ @ApiOperation(value = "修改用户") @ApiImplicitParam(name = "user",value = "用户",required = true,dataType = "User") @RequestMapping(value = "/user",method = RequestMethod.PUT) public String updateUser(@RequestBody User user){ userService.updateUser(user); return "success"; } /** * 根据id删除用户 */ @ApiOperation(value = "根据id删除用户") @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer") @RequestMapping(value = "/{id}",method = RequestMethod.DELETE) public String deleteUser(@PathVariable Integer id){ userService.deleteUser(id); return "success"; } }controller类中使用了swgger2如下:
springboot中整合swgger2