Controller:
package com.tzb.springboot.controller;
import com.tzb.springboot.entity.UserEntity;
import com.tzb.springboot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
//=====================JPA内置方法实现CRUD==========================
//查询
@GetMapping("/user/{id}")
public UserEntity getUser(@PathVariable("id") Integer id){
UserEntity user = userRepository.getOne(id);
return user;
}
//新增
@GetMapping("/user")
public UserEntity insertUser(UserEntity user){
UserEntity u =userRepository.save(user);
return u;
}
//修改User
@GetMapping("/user/deleteUserByJPA")
public String deleteUserByJPA(HttpServletRequest request){
String id = request.getParameter("id");
userRepository.deleteById(Integer.valueOf(id));
return "删除成功!";
}
//=====================自定义方法/自定义sql语句,实现CRUD=======&