效果图
前端代码【User.vue】
<template>
<div class="data-container">
<!--添加 start-->
<div class="data-header">
<el-button round @click="addHander" size="large" type="primary">
<el-icon>
<DocumentAdd/>
</el-icon>
<span>新增</span>
</el-button>
</div>
<!--添加 end-->
<!--表格数据展示 start-->
<div class="data-table">
<el-table :data="dataList.list" style="width: 1200px;">
<el-table-column label="头像" prop="userImg" align="center">
<template #default="scope">
<img :src="scope.row.userImg" style="height:60px"/>
</template>
</el-table-column>
<el-table-column show-overflow-tooltip label="昵称" prop="nickname" align="center"
width="120"></el-table-column>
<el-table-column label="账号" prop="username" align="center" width="140"></el-table-column>
<el-table-column label="手机号" prop="phone" align="center" width="140"></el-table-column>
<el-table-column show-overflow-tooltip label="邮箱" prop="email" align="center" width="160"></el-table-column>
<el-table-column label="性别" prop="userSex" align="center" width="60"></el-table-column>
<el-table-column label="状态" prop="userStatus" align="center">
<template #default="scope">
<el-tag round :type="scope.row.userStatus == '1' ? 'success' : 'info'">
{{ scope.row.userStatus == '1' ? '激活' : '未激活' }}
</el-tag>
</template>
</el-table-column>
<el-table-column show-overflow-tooltip label="个性签名" prop="signature" align="center"></el-table-column>
<el-table-column show-overflow-tooltip label="自我介绍" prop="userIntroduce" align="center"></el-table-column>
<el-table-column label="操作" align="center" width="220">
<template #default="scope">
<el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
<el-button size="small" type="primary" @click="handleStatus(scope.$index, scope.row)">修改状态</el-button>
<el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!--分页 start-->
<div class="page">
<el-pagination background
layout="prev,pager,next,jumper"
:default-page-size="defaultPageSize"
:total="totalData"
@current-change="currentChangeHaddler"></el-pagination>
</div>
<!--分页 end-->
</div>
<!--表格数据展示 end-->
<!--添加对话框 start-->
<el-dialog draggable destroy-on-close v-model="dialogAddVisible" title="添加" width="70%" center>
<el-form inline :model="addFormInfo" label-width="150px">
<el-form-item label="头像">
<el-input v-model="addFormInfo.userImg"></el-input>
</el-form-item>
<el-form-item label="昵称">
<el-input v-model="addFormInfo.nickname"></el-input>
</el-form-item>
<el-form-item label="账号">
<el-input v-model="addFormInfo.username"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input v-model="addFormInfo.password"></el-input>
</el-form-item>
<el-form-item label="手机号">
<el-input v-model="addFormInfo.phone"></el-input>
</el-form-item>
<el-form-item label="邮箱">
<el-input v-model="addFormInfo.email"></el-input>
</el-form-item>
<el-form-item label="性别">
<el-input v-model="addFormInfo.userSex"></el-input>
</el-form-item>
<el-form-item label="个性签名">
<el-input type="textarea" rows="3" style="width: 800px" v-model="addFormInfo.signature"></el-input>
</el-form-item>
<el-form-item label="自我介绍">
<el-input type="textarea" rows="6" style="width: 800px" v-model="addFormInfo.userIntroduce"></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogAddVisible = false">取消</el-button>
<el-button type="primary" @click="sureHandler">确定</el-button>
</span>
</template>
</el-dialog>
<!--添加对话框 end-->
<!--编辑对话框 start-->
<!--destroy-on-close:每次关闭对话框时直接销毁对话框,没有缓存-->
<el-dialog
draggable
destroy-on-close
v-model="dialogEditorVisible"
title="编辑"
width="70%"
center>
<el-form inline :model="editorFormInfo" label-width="150px">
<el-form-item label="头像">
<el-input v-model="editorFormInfo.userImg"></el-input>
</el-form-item>
<el-form-item label="昵称">
<el-input v-model="editorFormInfo.nickname"></el-input>
</el-form-item>
<el-form-item label="账号">
<el-input v-model="editorFormInfo.username"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input v-model="editorFormInfo.password"></el-input>
</el-form-item>
<el-form-item label="手机号">
<el-input v-model="editorFormInfo.phone"></el-input>
</el-form-item>
<el-form-item label="邮箱">
<el-input v-model="editorFormInfo.email"></el-input>
</el-form-item>
<el-form-item label="性别">
<el-input v-model="editorFormInfo.userSex"></el-input>
</el-form-item>
<el-form-item label="个性签名">
<el-input type="textarea" rows="3" style="width: 800px" v-model="editorFormInfo.signature"></el-input>
</el-form-item>
<el-form-item label="自我介绍">
<el-input type="textarea" rows="6" style="width: 800px" v-model="editorFormInfo.userIntroduce"></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogEditorVisible = false">取消</el-button>
<el-button type="primary" @click="sureEditorHandler">确定</el-button>
</span>
</template>
</el-dialog>
<!--编辑对话框 end-->
</div>
</template>
<script setup>
import axios from "@/utils/request.js"
import {onMounted, reactive, ref} from "vue";
import {ElMessage} from "element-plus";
//初始化分页查询数据
const dataList = reactive({
list: []
})
//初始化总条数
const totalData = ref(0)
//当前页
const currentPage = ref(1)
//初始化分页显示条数
const defaultPageSize = ref(10)
//添加添加对话框控制器
const dialogAddVisible = ref(false)
//初始化添加对话框状态
const addFormInfo = reactive({
userImg: '',
nickname: '',
username: '',
password: '',
phone: '',
email: '',
userSex: '',
signature: '',
userIntroduce: '',
userStatus: '1',
})
//编辑对话框控制器
const dialogEditorVisible = ref(false)
//初始化编辑对话框状态
const editorFormInfo = reactive({
userId: '',
userImg: '',
nickname: '',
username: '',
password: '',
phone: '',
email: '',
userSex: '',
signature: '',
userIntroduce: '',
userStatus: '',
})
/**
* 网路请求:分页查询
* */
const http = (page, size) => {
axios.get('user/search', {
params: {
page: page,
size: size
}
}).then(res => {
if (res.data.code == 200) {
dataList.list = res.data.data.records
totalData.value = res.data.data.total
} else {
ElMessage.error(res.data.message)
}
})
}
onMounted(() => {
http(currentPage.value, defaultPageSize.value)
})
/**
* 分页
*/
const currentChangeHaddler = (nowPage) => {
http(nowPage, defaultPageSize.value)
currentPage.value = nowPage
}
/**
* 添加对话框弹出事件
*/
const addHander = () => {
dialogAddVisible.value = true
}
/**
* 添加对话框 确定事件
*/
const sureHandler = () => {
axios.post('user/add', {
userImg: addFormInfo.userImg,
nickname: addFormInfo.nickname,
username: addFormInfo.username,
password: addFormInfo.password,
phone: addFormInfo.phone,
email: addFormInfo.email,
userSex: addFormInfo.userSex,
signature: addFormInfo.signature,
userIntroduce: addFormInfo.userIntroduce,
userStatus: addFormInfo.userStatus,
}).then(res => {
if (res.data.code == 200) {
dialogAddVisible.value = false
http(currentPage.value, defaultPageSize.value)
} else {
ElMessage.error(res.data.message)
}
})
}
/**
* 编辑对话框 弹出事件
* */
const handleEdit = (index, row) => {
dialogEditorVisible.value = true
axios.get('user/findById', {
params: {
userId: row.userId
}
}).then(res => {
if (res.data.code == 200) {
editorFormInfo.userId = res.data.data.userId;
editorFormInfo.userImg = res.data.data.userImg;
editorFormInfo.nickname = res.data.data.nickname;
editorFormInfo.username = res.data.data.username;
editorFormInfo.password = res.data.data.password;
editorFormInfo.phone = res.data.data.phone;
editorFormInfo.email = res.data.data.email;
editorFormInfo.userSex = res.data.data.userSex;
editorFormInfo.signature = res.data.data.signature;
editorFormInfo.userIntroduce = res.data.data.userIntroduce;
editorFormInfo.userStatus = res.data.data.userStatus;
} else {
ElMessage.error(res.data.data.message)
}
})
}
/**
* 编辑对话框 确定事件
*/
const sureEditorHandler = () => {
axios.post('user/update', {
userId: editorFormInfo.userId,
userImg: editorFormInfo.userImg,
nickname: editorFormInfo.nickname,
username: editorFormInfo.username,
password: editorFormInfo.password,
phone: editorFormInfo.phone,
email: editorFormInfo.email,
userSex: editorFormInfo.userSex,
signature: editorFormInfo.signature,
userIntroduce: editorFormInfo.userIntroduce,
userStatus: editorFormInfo.userStatus,
}).then(res => {
if (res.data.code == 200) {
dialogEditorVisible.value = false
http(currentPage.value, defaultPageSize.value)
} else {
//添加失败:给出提示信息(element-plus/反馈组件/message信息提示)
ElMessage.error(res.data.message)
}
})
}
/**删除 */
const handleDelete = (index, row) => {
ElMessageBox.confirm(
'确定删除么',
'删除',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
//确认删除
axios.delete("user/delete", {
params: {
userId: row.userId
}
}).then(res => {
if (res.data.code == 200) {
ElMessage({
type: 'success',
message: "删除成功!!!",
})
//刷新
http(currentPage.value, defaultPageSize.value)
} else {
ElMessage({
type: 'error',
message: res.data.message,
})
}
})
}).catch(error => {
ElMessage({
type: 'info',
message: "取消删除",
})
})
}
/**
* 修改用户状态
*/
const handleStatus = (index, row) => {
axios.post("user/updateStatus", {userId: row.userId}).
then(res=>{
if(res.data.code==200){
ElMessage.success("状态已更改")
http(currentPage.value, defaultPageSize.value)
}
})
}
</script>
<style scoped>
.data-container {
background: linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
background: -o-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
background: -ms-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
background: -moz-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
background: -webkit-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
height: 800px;
}
.data-header {
padding: 20px;
}
.data-table {
padding: 20px;
}
.page {
position: fixed;
right: 10px;
bottom: 10px;
}
</style>
后端代码【java】
实体类
package jkw.pojo;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
/**
* 用户
*/
@Data
public class User implements Serializable {
@TableId
private Integer userId;
private String userImg; // 头像
private String nickname; // 昵称
private String username; // 账号
private String password; // 密码
private String phone; // 手机号
private String email;//邮箱
private String userSex; // 性别
private String signature;//个性签名
private String userIntroduce;//自我介绍
private String userStatus; // 用户状态(1激活 0未激活)
}
mapper
package jkw.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jkw.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
service
package jkw.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jkw.pojo.User;
public interface UserService {
void add(User user);
void update(User user);
void delete(Integer id);
User findById(Integer id);
Page<User> search(int page,int size);
void updateStatus(Integer id);
}
serviceImpl
package jkw.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jkw.mapper.UserMapper;
import jkw.pojo.User;
import jkw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void add(User user) {
userMapper.insert(user);
}
@Override
public void update(User user) {
userMapper.updateById(user);
}
@Override
public void delete(Integer id) {
userMapper.deleteById(id);
}
@Override
public User findById(Integer id) {
return userMapper.selectById(id);
}
@Override
public Page<User> search(int page, int size) {
return userMapper.selectPage(new Page<>(page, size), null);
}
@Override
public void updateStatus(Integer id) {
User user = userMapper.selectById(id);
if(user.getUserStatus().equals("1")){
user.setUserStatus("0");
userMapper.updateById(user);
}else {
user.setUserStatus("1");
userMapper.updateById(user);
}
}
}
controller
package jkw.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jkw.pojo.User;
import jkw.service.UserService;
import jkw.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RequestMapping("/user")
@RestController
public class UserCon {
@Autowired
private UserService userService;
/**
* 新增
*
* @param user
* @return
*/
@PostMapping("/add")
//@PreAuthorize("hasAnyAuthority('/user')")
public BaseResult add(User user) {
userService.add(user);
return BaseResult.ok();
}
/**
* 修改
*
* @param user
* @return
*/
@PostMapping("/update")
public BaseResult update(User user) {
userService.update(user);
return BaseResult.ok();
}
/**
* 删除
*
* @param userId
* @return
*/
@DeleteMapping("/delete")
public BaseResult delete(Integer userId) {
userService.delete(userId);
return BaseResult.ok();
}
/**
* 根据id查询
*
* @param userId
* @return
*/
@GetMapping("/findById")
public BaseResult findById(Integer userId) {
User user = userService.findById(userId);
return BaseResult.ok(user);
}
/**
* 分页查询
*
* @param page
* @param size
* @return
*/
@GetMapping("/search")
//@PreAuthorize("hasAnyAuthority('/user')")
public BaseResult search(int page, int size) {
Page<User> brandPage = userService.search(page, size);
return BaseResult.ok(brandPage);
}
/**
* 修改状态
* @param userId
* @return
*/
@PostMapping("/updateStatus")
public BaseResult updateStatus(Integer userId) {
userService.updateStatus(userId);
return BaseResult.ok();
}
}