基于RESTful springMVC开发
基于RESTful springMVC开发
stars永恒 发表于2年前
基于RESTful springMVC开发
  • 发表于 2年前
  • 阅读 6
  • 收藏 0
  • 点赞 1
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: RESTful springMVC

controller代码

package cn.internet.userinfo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.internet.userinfo.pojo.User;
import cn.internet.userinfo.service.UserService;

@RequestMapping("rest/user")
@Controller
public class RestUserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 新增
     * Title: saveUser
     * @param user
     * @return
     */
    @RequestMapping(method=RequestMethod.POST)
    //@ResponseBody
    public ResponseEntity<Void> saveUser(User user){
        try {
            this.userService.saveUser(user);
            //返回201
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //500服务器内部错误
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        
    }
    /**
     * 删除
     * Title: deleteUser
     * @param id
     * @return
     */
    @RequestMapping(value="{id}",method=RequestMethod.DELETE)
    public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id){
        try {
            this.userService.deleteUser(id);
            //返回204
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //500服务器内部错误
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        
    }
    /**
     * 修改
     * Title: updateUser
     * @param user
     * @return
     */
    @RequestMapping(method=RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(User user){
        try {
            this.userService.updateUser(user);
            //返回204
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //500服务器内部错误
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        
    }
    /**
     * 根据id查询
     * Title: queryUserById
     * @param id
     * @return
     */
    @RequestMapping(value="{id}",method=RequestMethod.GET)
    public ResponseEntity<User> queryUserById(@PathVariable("id") Long id){
        try {
            User user = this.userService.queryUserById(id);
            //返回200
            //return ResponseEntity.ok(user);
            return ResponseEntity.ok().body(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //500服务器内部错误
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        
    }
}

service代码

package cn.internet.userinfo.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.internet.userinfo.mapper.UserMapper;
import cn.internet.userinfo.pojo.User;

@Service
public class UserService {
	@Autowired
    private UserMapper userMapper;
	/**
     * 根据id查询用户数据
     * 
     * @param id
     * @return
     */
    public User queryUserById(Long id) {
       User user = this.userMapper.selectByPrimaryKey(id);
       return user;
    }
    /**
     * 分页查询用户数据
     * 
     * @param page 页码
     * @param rows 每页显示数据条数
     * @return
     */
	public PageInfo<User> queryListByPage(Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		List<User> list = this.userMapper.select(null);
		PageInfo<User> pageInfo=new PageInfo<User>(list);
		
		return pageInfo;
	}
	/**
         * 保存
         * Title: saveUser
         */
        public void saveUser(User user) {
            user.setId(null);
        }
        /**
         * 删除
         * Title: deleteUser
         * @param id
         */
        public void deleteUser(Long id) {
            this.userMapper.deleteByPrimaryKey(id);
        }
        /**
         * 修改
         * Title: updateUser
         * @param user
         */
        public void updateUser(User user) {
            user.setUpdated(new Date());
            this.userMapper.updateByPrimaryKeySelective(user);
        }
}


共有 人打赏支持
粉丝 5
博文 136
码字总数 135293
×
stars永恒
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: