山东大学创新实训3——博客论坛管理员功能
管理员功能
ADMIN
实体
import java.io.Serializable;
/**
* 管理员
*/
public class Admin extends Account implements Serializable {
private static final long serialVersionUID = 1L;
/** ID */
private Integer id;
/** 用户名 */
private String username;
/** 密码 */
private String password;
/** 姓名 */
private String name;
/** 电话 */
private String phone;
/** 邮箱 */
private String email;
/** 头像 */
private String avatar;
/** 角色标识 */
private String role;
@Override
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
@Override
public String getUsername() {
return username;
}
@Override
public void setUsername(String username) {
this.username = username;
}
@Override
public String getPassword() {
return password;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String getAvatar() {
return avatar;
}
@Override
public void setAvatar(String avatar) {
this.avatar = avatar;
}
@Override
public String getRole() {
return role;
}
@Override
public void setRole(String role) {
this.role = role;
}
}
AdminController
package com.wzh.springbootproject.controller;
import com.wzh.springbootproject.common.Result;
import com.wzh.springbootproject.entity.Admin;
import com.wzh.springbootproject.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 管理员前端操作接口
**/
@RestController
@Transactional
@RequestMapping("/admin")
public class AdminController {
@Resource
private AdminService adminService;
/**
* 新增
*/
@PostMapping("/add")
public Result add(@RequestBody Admin admin) {
adminService.add(admin);
return Result.success();
}
/**
* 删除
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
adminService.deleteById(id);
return Result.success();
}
/**
* 批量删除
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
adminService.deleteBatch(ids);
return Result.success();
}
/**
* 修改
*/
@PutMapping("/update")
public Result updateById(@RequestBody Admin admin) {
adminService.updateById(admin);
return Result.success();
}
/**
* 根据ID查询
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Admin admin = adminService.selectById(id);
return Result.success(admin);
}
/**
* 查询所有
*/
@GetMapping("/selectAll")
public Result selectAll(Admin admin) {
List<Admin> list = adminService.selectAll(admin);
return Result.success(list);
}
/**
* 分页查询
*/
@GetMapping("/selectPage")
public Result selectPage(Admin admin,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Admin> page = adminService.selectPage(admin, pageNum, pageSize);
return Result.success(page);
}
}
AdminMapper
package com.wzh.springbootproject.mapper;
import com.wzh.springbootproject.entity.Admin;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 操作admin相关数据接口
*/
public interface AdminMapper {
/**
* 新增
*/
int insert(Admin admin);
/**
* 删除
*/
int deleteById(Integer id);
/**
* 修改
*/
int updateById(Admin admin);
/**
* 根据ID查询
*/
Admin selectById(Integer id);
/**
* 查询所有
*/
List<Admin> selectAll(Admin admin);
@Select("select * from admin where username = #{username}")
Admin selectByUsername(String username);
}
resource文件夹中写AdminMapper.xml来实现
package com.wzh.springbootproject.mapper;
import com.wzh.springbootproject.entity.Admin;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 操作admin相关数据接口
*/
public interface AdminMapper {
/**
* 新增
*/
int insert(Admin admin);
/**
* 删除
*/
int deleteById(Integer id);
/**
* 修改
*/
int updateById(Admin admin);
/**
* 根据ID查询
*/
Admin selectById(Integer id);
/**
* 查询所有
*/
List<Admin> selectAll(Admin admin);
@Select("select * from admin where username = #{username}")
Admin selectByUsername(String username);
}
AdminService
package com.wzh.springbootproject.service;
import cn.hutool.core.util.ObjectUtil;
import com.wzh.springbootproject.common.Constants;
import com.wzh.springbootproject.common.ResultCodeEnum;
import com.wzh.springbootproject.common.RoleEnum;
import com.wzh.springbootproject.entity.Account;
import com.wzh.springbootproject.entity.Admin;
import com.wzh.springbootproject.exception.CustomException;
import com.wzh.springbootproject.mapper.AdminMapper;
import com.wzh.springbootproject.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 管理员业务处理
**/
@Service
public class AdminService {
@Resource
private AdminMapper adminMapper;
/**
* 新增
*/
public void add(Admin admin) {
Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
if (ObjectUtil.isNotNull(dbAdmin)) {
throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
}
if (ObjectUtil.isEmpty(admin.getPassword())) {
admin.setPassword(Constants.USER_DEFAULT_PASSWORD);
}
if (ObjectUtil.isEmpty(admin.getName())) {
admin.setName(admin.getUsername());
}
admin.setRole(RoleEnum.ADMIN.name());
adminMapper.insert(admin);
}
/**
* 删除
*/
public void deleteById(Integer id) {
adminMapper.deleteById(id);
}
/**
* 批量删除
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
adminMapper.deleteById(id);
}
}
/**
* 修改
*/
public void updateById(Admin admin) {
adminMapper.updateById(admin);
}
/**
* 根据ID查询
*/
public Admin selectById(Integer id) {
return adminMapper.selectById(id);
}
/**
* 查询所有
*/
public List<Admin> selectAll(Admin admin) {
return adminMapper.selectAll(admin);
}
/**
* 分页查询
*/
public PageInfo<Admin> selectPage(Admin admin, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Admin> list = adminMapper.selectAll(admin);
return PageInfo.of(list);
}
/**
* 登录
*/
public Account login(Account account) {
Account dbAdmin = adminMapper.selectByUsername(account.getUsername());
if (ObjectUtil.isNull(dbAdmin)) {
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
}
if (!account.getPassword().equals(dbAdmin.getPassword())) {
throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
}
// 生成token
String tokenData = dbAdmin.getId() + "-" + RoleEnum.ADMIN.name();
String token = TokenUtils.createToken(tokenData, dbAdmin.getPassword());
dbAdmin.setToken(token);
return dbAdmin;
}
/**
* 修改密码
*/
public void updatePassword(Account account) {
Admin dbAdmin = adminMapper.selectByUsername(account.getUsername());
if (ObjectUtil.isNull(dbAdmin)) {
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
}
if (!account.getPassword().equals(dbAdmin.getPassword())) {
throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
}
dbAdmin.setPassword(account.getNewPassword());
adminMapper.updateById(dbAdmin);
}
}