Java後臺開發學習(1)——User接口

第一次寫後臺,跟着大佬學習java後臺開發。第一個任務就是寫user接口,就跟着大佬之前的代碼,磕磕碰碰開始寫了。html

Model層

@Data
@Document(collection = "user")
public class User {

    @NotNull
    @Id
    @Field("user_id")
    private String userId = UUID.randomUUID().toString();

    @NotNull
    @Field("username")
    private String username;

    @NotNull
    @Field("password")
    private String password;

    @NotNull
    private String nickname;

    @NotNull
    private String role;

    private String token;

}複製代碼

@Data:在類名上方加上了@Data註解, 爲類提供讀寫屬性, 此外還提供了 equals()、java

hashCode()、toString() 方法,能夠簡寫代碼。spring

@Document(collection = "xxx"):實體類將會映射到數據庫中的名爲xxx的collection。數據庫

@Field:用於POST請求,提交單個數據。(這個暫時還不是很懂?)json

DAO層

@Repository
public interface UserRepository extends MongoRepository<User, String> {

    User findByUsername(String username);

}複製代碼

@Repository:註解在持久層中,具備將數據庫操做拋出的原生異常翻譯轉化爲spring的持久層異常的功能。bash

Service層

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public User save(User user) throws Exception {
        user.setPassword(DigestUtils.sha256Hex(user.getPassword()));
        return userRepository.save(user);
    }

    public User find(String userId) throws Exception {
        return userRepository.findById(userId).orElse(null);
    }

    public Page<User> findAll(Pageable pageable) throws Exception {
        return userRepository.findAll(pageable);
    }

    public void delete(String userId) throws Exception {
        User user = userRepository.findById(userId).orElse(null);
        if (user == null) {
            throw new IllegalArgumentException("No such user");
        }
        userRepository.delete(user);
    }

}複製代碼

@Service:業務邏輯層註解,這個註解只是標註該類處於業務邏輯層。app

Controller層

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user")
    public Result saveUser(@RequestBody User user) throws Exception {
        return new Result.Builder().setData(userService.save(user)).build();
    }

    @LoginRequired
    @GetMapping("/user/{userId}")
    public Result getUser(@PathVariable String userId, @CurrentUser User user) throws Exception {
        return new Result.Builder().setData(userService.find(userId)).build();
    }

    @LoginRequired
    @GetMapping("/user/{page}/{size}")
    public Result getAll(@PathVariable Integer page, @PathVariable Integer size, @CurrentUser User user) throws Exception{
        return new Result.Builder().setData(userService.findAll(PageRequest.of(page-1,size))).build();
    }

    @LoginRequired
    @DeleteMapping("/user/{userId}")
    public Result deleteUser(@PathVariable String userId, @CurrentUser User user) throws Exception {
        userService.delete(userId);
        return new Result.Builder().build();
    }

}複製代碼

@RestController:至關於@Controller+@ResponseBody兩個註解的結合,返回json數據不須要在方法前面加@ResponseBody註解了,但使用@RestController這個註解,就不能返回jsp,html頁面,視圖解析器沒法解析jsp,html頁面。
dom


看到大佬寫的@Query還有findBy方法,還有一些註解,emmm打算這兩天再研究一下。jsp

相關文章
相關標籤/搜索