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}") @GetMapping("/find/{id}")
public Optional<Merchants> getMerchantsById(@PathVariable("id") Long id) { public Merchants getMerchantsById(@PathVariable("id") Long id) {
return merchantsService.findById(id); return merchantsService.findById(id);
} }
} }

View File

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

View File

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

View File

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

View File

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