学习java,挺好玩的呢

1、根据用户 id 查询用户数据

  1.1 controll控制器

@RequestMapping("restful/user")
@Controller
public class RestfulUserController {
    
    @Autowired
    private NewUserService newUserService;
    
    /**
     * 根据用户 id查询
     * @param id
     * @return
     */
    @RequestMapping(value="{id}",method=RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<User> queryUserByid(@PathVariable("id") Long id){
        try {
            User user= this.newUserService.queryUserid(id);
            if (user==null) {
                //请求资源不存在 404 
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
                //请求资源存在,200
//        return ResponseEntity.status(HttpStatus.OK).body(user);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
   }

  1.2 通用Mapper

public interface NewUserMapper extends Mapper<User>{
    
}

  1.3 Service设置

@Service
public class NewUserService {
    
    @Autowired
    private NewUserMapper newUsermapper;public User queryUserid(Long id) {
        return this.newUsermapper.selectByPrimaryKey(id);
    }

}

  1.4查询

2、新增用户

  2.1 Controller

@RequestMapping("restful/user")
@Controller
public class RestfulUserController {
    
    @Autowired
    private NewUserService newUserService;
/**
     * 插入用户
     * @param user
     * @return
     */
    @RequestMapping(method=RequestMethod.POST)
    public ResponseEntity<Void> insertUser(User user){
        try {
            //添加成功
            this.newUserService.saveUser(user);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
    
}

  2.2 service设置

@Service
public class NewUserService {
    
    @Autowired
    private NewUserMapper newUsermapper;public void saveUser(User user) {
        this.newUsermapper.insert(user);
    }

}

3、修改数据

  3.1 对于 PUT请求方式,默认不可以提交表单数据的,必须使用过滤器进行配置。。

在 Web.xml中配置过滤器
<filter> <filter-name>HttpMethodFilter</filter-name> <filter-class>org.springframeword.web.filter.HttpPutFormContentFilter</filter-calss>  此过滤器只能处理PUT请求 </filter> <filter-mapping> <filter-name>HttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

  3.2 Controller设置


@RequestMapping("restful/user")
@Controller
public class RestfulUserController {
    
    @Autowired
    private NewUserService newUserService;
/**
     * 更新用户数据
     * @param user
     * @return
     */
    @RequestMapping(method=RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(User user){
        try {
            //修改成功
            this.newUserService.updateuser(user);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        
    }
    
}

  3.3 servicer设置

@Service
public class NewUserService {
    
    @Autowired
    private NewUserMapper newUsermapper;public void updateuser(User user) {
        this.newUsermapper.updateByPrimaryKeySelective(user);
    }

}

4、删除数据

  4.1 默认请求方式中,DELETE方式不会提交表单的,必须在web.xml中进行配置

<!--将POST请求转化为DELETE或者是PUT要用 _method指定真正的请求参数--> 此过滤器更加强大

<filter>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <filer-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filer>
<filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

  4.2 Controller

@RequestMapping("restful/user")
@Controller
public class RestfulUserController {
    
    @Autowired
    private NewUserService newUserService;

/** * 删除用户数据 * @return */ @RequestMapping(method=RequestMethod.DELETE)  DELETE请求方式 public ResponseEntity<Void> deletedUser(@RequestParam(value="id",defaultValue="0") Long id){ try { if (id.intValue()==0) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } this.newUserService.deleteuserByid(id); return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } catch (Exception e) { e.printStackTrace(); } // 500 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); }
}

  4.3 Service设置

@Service
public class NewUserService {
    
    @Autowired
    private NewUserMapper newUsermapper;
public void deleteuserByid(Long id) { this.newUsermapper.deleteByPrimaryKey(id); }

}

  

 

posted on 2019-01-07 15:30  axu521  阅读(419)  评论(0编辑  收藏  举报

<