SpringMVC实现RESTful服务
这里只说service,controller层的代码。Mapper层则直接继承Mapper<T>则可以,记住mybatis-config.xml一定要配置分页插件和mapper类的插件。
1.查询所有用户
service:
@Autowired
private IUserMapper userMapper; @Override
public EasyUIResult getAllUser(Integer page, Integer rows) {
// TODO Auto-generated method stub // 后台帮我们封装了一个page并且直接存到本地线程
PageHelper.startPage(page, rows);
Example example = new Example(User.class);
example.setOrderByClause("updated desc");
List<User> list = userMapper.selectByExample(example);
PageInfo<User> pageInfo = new PageInfo<>(list);
return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
}
controller:
/**
* 查询所有用户 返回数据类型为:json格式
*
* @param page
* @param rows
* @return
*/
@RequestMapping(value = "list", method = { RequestMethod.POST })
public @ResponseBody EasyUIResult finAllUser(@RequestParam(value = "page", defaultValue = "") Integer page,
@RequestParam(value = "rows", defaultValue = "") Integer rows) {
return userService.getAllUser(page, rows);
}
2.根据用户id查询用户信息
service:
@Override
public User queryUserById(Long id) {
// TODO Auto-generated method stub
return userMapper.selectByPrimaryKey(id);
}
cotroller:
@Controller
@RequestMapping("user/rest")
public class RestUserController { @Autowired
private IUserService userService; /**
* 根据id查询用户信息
*
* @param id
* @return
*/
@RequestMapping(value = "{userId}", method = RequestMethod.GET)
public ResponseEntity<User> queryUserById(@PathVariable(value = "userId") Long id) {
try {
User user = userService.queryUserById(id);
if (user == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
// return ResponseEntity.ok(user);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
// return ResponseEntity.status(500).body(null);
// 系统内部异常
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
3.添加一个用户
service:
@Override
public Boolean saveUser(User user) {
// TODO Auto-generated method stub
return userMapper.insertSelective(user) == 1;
}
controller:
/**
* 添加一个用户
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> addUser(User user) {
try {
userService.saveUser(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
// return ResponseEntity.ok(user);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
4.修改用户
service:
@Override
public boolean updateUsers(User user) {
// TODO Auto-generated method stub
// 修改用户所有信息
// userMapper.updateByPrimaryKey(User);
// 修改不为空的字段
// userMapper.updateByPrimaryKeySelective(record);
return userMapper.updateByPrimaryKey(user) == 1;
}
controller:
/**
* 跟新用户
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<Void> updateUser(User user) {
try {
boolean b = userService.updateUsers(user);
if (b) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
// return ResponseEntity.ok(user);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
5.删除
service:
@Override
public boolean deleteUser(Long id) {
// TODO Auto-generated method stub
return userMapper.deleteByPrimaryKey(id) == 1;
}
controller:
/**
* 删除一个用户
*
* @param id
* @return
*/
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
try {
if (id == 0) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
boolean b = userService.deleteUser(id);
if (b) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
// return ResponseEntity.ok(user);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
delete删除时比较特殊:一定要注意
进行删除和修改时,要在web.xml中配置过滤器(补充):
<!--
将POST请求转化为DELETE或者是PUT
要用_method指定真正的请求参数
-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决PUT请求无法提交表单数据的问题 -->
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
大功告成!!