Optional用法优化

This commit is contained in:
myh 2023-11-07 22:42:22 +08:00
parent 08b4513d14
commit 74976ade9c
6 changed files with 47 additions and 18 deletions

View File

@ -39,7 +39,7 @@ public class MerchantsController {
}
@GetMapping("/find/{id}")
public Optional<Merchants> getMerchantsById(@PathVariable("id") Long id) {
public Merchants getMerchantsById(@PathVariable("id") Long id) {
return merchantsService.findById(id);
}
}

View File

@ -1,7 +1,9 @@
package com.example.springdemo.controller;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import com.example.springdemo.service.UsersService;
import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@ -14,34 +16,62 @@ public class UsersController {
@Resource
private UsersService usersService;
//添加新用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@PostMapping("/add")
public Users addUsers(@RequestBody Users users) {
public Users addUsers(@RequestBody UsersPassword usersPassword, @RequestBody Users users) {
usersService.addPassword(usersPassword);
return usersService.addUsers(users);
}
@DeleteMapping("/delete/{id}")
public void deleteUsers(@PathVariable("id") Long id) {
usersService.deleteUsersById(id);
//通过ID删除用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@DeleteMapping("/delete")
public void deleteUsers(@RequestParam(name = "userID") Long userID) {
usersService.deleteUsersById(userID);
}
@PutMapping("/update")
//通过姓名删除用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@DeleteMapping("/delete")
public void deleteUsers(@RequestParam(name = "name") String name) {
usersService.deleteUsersByName(name);
}
//更新用户信息
@RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1})
@PutMapping("/update/info")
public Users updateUsers(@RequestBody Users users) {
return usersService.updateUsers(users);
}
//查找全部用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find")
public Iterable<Users> getUsers() {
return usersService.findAllUsers();
}
@GetMapping("/find/{name}")
public Users getUsersByName(@PathVariable("name") String name) {
//根据姓名查找用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find")
public Users getUsersByName(@RequestParam(name = "name") String name) {
return usersService.findByName(name);
}
@GetMapping("/find/{id}")
public Optional<Users> getUsersById(@PathVariable("id") Long id) {
return usersService.findById(id);
//根据ID查找用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find")
public Users getUsersById(@RequestParam(name = "id") Long userID) {
return usersService.findById(userID);
}
//更新用户密码
@RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1})
@PutMapping("/update/password")
public void updatePassword(@RequestParam(name = "password") String password,
@RequestParam(name = "userID") Long userID) {
usersService.updatePassword(password, userID);
}
}

View File

@ -13,7 +13,7 @@ public interface MerchantsService {
Iterable<Merchants> findAllMerchants();
Optional<Merchants> findById(Long id);
Merchants findById(Long id);
Merchants findByName(String name);
}

View File

@ -6,7 +6,6 @@ import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class MerchantsServiceImpl implements MerchantsService {
@ -34,8 +33,8 @@ public class MerchantsServiceImpl implements MerchantsService {
}
@Override
public Optional<Merchants> findById(Long id) {
return merchantsRepository.findById(id);
public Merchants findById(Long id) {
return merchantsRepository.findById(id).orElse(null);
}
@Override

View File

@ -17,7 +17,7 @@ public interface UsersService {
Iterable<Users> findAllUsers();
Optional<Users> findById(Long userID);
Users findById(Long userID);
Users findByName(String name);

View File

@ -40,8 +40,8 @@ public class UsersServiceImpl implements UsersService {
}
@Override
public Optional<Users> findById(Long userID) {
return usersRepository.findById(userID);
public Users findById(Long userID) {
return usersRepository.findById(userID).orElse(null);
}
@Override