在项目开发和工作实践中,创作灵感往往来自于对日常经验的总结与反思。通过记录技术难点和解决方案,不仅可以加深对问题的理解,还能为后续项目的优化提供参考。与此同时,撰写技术笔记、分享职场心得,不仅是对自己成长的回顾,也是帮助他人学习与进步的过程。
总结几条创作灵感:
记录工作中的实践经验,反思项目中的得失,逐步积累自己的知识库。
通过笔记记录重要的技术知识点,不断丰富技术储备。
发表职场心得与感悟,不仅是总结自我成长的过程,还能与同行交流分享。
将原创文章搬运至各大平台,扩大影响力,促进与更多技术爱好者的互动。
详细的代码 sql 我放下面 需要完整的代码 vx:Java980320 免费分享
系统用户页面展示
用户添加页面展示
编辑用户信息
SQL表DDL语句
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`username` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户名',
`password` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '密码',
`role` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '角色',
`touxiang` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '头像',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户';
User实体
package com.example.entity;
/**
* 用户
*/
public class User {
/** ID */
private Integer id;
/** 用户名 */
private String username;
/** 密码 */
private String password;
/** 角色 */
private String role;
/** 头像 */
private String touxiang;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getTouxiang() {
return touxiang;
}
public void setTouxiang(String touxiang) {
this.touxiang = touxiang;
}
}
UserController
package com.example.controller;
import com.example.common.Result;
import com.example.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 用户前端操作接口
**/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
* 新增
*/
@PostMapping("/add")
public Result add(@RequestBody User user) {
userService.add(user);
return Result.success();
}
/**
* 删除
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
userService.deleteById(id);
return Result.success();
}
/**
* 批量删除
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
userService.deleteBatch(ids);
return Result.success();
}
/**
* 修改
*/
@PutMapping("/update")
public Result updateById(@RequestBody User user) {
userService.updateById(user);
return Result.success();
}
/**
* 根据ID查询
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
User user = userService.selectById(id);
return Result.success(user);
}
/**
* 查询所有
*/
@GetMapping("/selectAll")
public Result selectAll(User user) {
List<User> list = userService.selectAll(user);
return Result.success(list);
}
/**
* 分页查询
*/
@GetMapping("/selectPage")
public Result selectPage(User user,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<User> page = userService.selectPage(user, pageNum, pageSize);
return Result.success(page);
}
}
UserService
package com.example.service;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 用户业务处理
**/
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 新增
*/
public void add(User user) {
userMapper.insert(user);
}
/**
* 删除
*/
public void deleteById(Integer id) {
userMapper.deleteById(id);
}
/**
* 批量删除
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
userMapper.deleteById(id);
}
}
/**
* 修改
*/
public void updateById(User user) {
userMapper.updateById(user);
}
/**
* 根据ID查询
*/
public User selectById(Integer id) {
return userMapper.selectById(id);
}
/**
* 查询所有
*/
public List<User> selectAll(User user) {
return userMapper.selectAll(user);
}
/**
* 分页查询
*/
public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> list = userMapper.selectAll(user);
return PageInfo.of(list);
}
}
UserMapper
package com.example.mapper;
import com.example.entity.User;
import java.util.List;
/**
* 操作user相关数据接口
*/
public interface UserMapper {
/**
* 新增
*/
int insert(User user);
/**
* 删除
*/
int deleteById(Integer id);
/**
* 修改
*/
int updateById(User user);
/**
* 根据ID查询
*/
User selectById(Integer id);
/**
* 查询所有
*/
List<User> selectAll(User user);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<sql id="Base_Column_List">
id,username,password,role,touxiang
</sql>
<select id="selectAll" resultType="com.example.entity.User">
select
<include refid="Base_Column_List" />
from user
<where>
<if test="id != null"> and id = #{id}</if>
<if test="username != null"> and username like concat('%', #{username}, '%')</if>
<if test="password != null"> and password like concat('%', #{password}, '%')</if>
<if test="role != null"> and role like concat('%', #{role}, '%')</if>
<if test="touxiang != null"> and touxiang like concat('%', #{touxiang}, '%')</if>
</where>
order by id desc
</select>
<select id="selectById" resultType="com.example.entity.User">
select
<include refid="Base_Column_List" />
from user
where id = #{id}
</select>
<delete id="deleteById">
delete from user
where id = #{id}
</delete>
<insert id="insert" parameterType="com.example.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="username != null">username,</if>
<if test="password != null">password,</if>
<if test="role != null">role,</if>
<if test="touxiang != null">touxiang,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="username != null">#{username},</if>
<if test="password != null">#{password},</if>
<if test="role != null">#{role},</if>
<if test="touxiang != null">#{touxiang},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.example.entity.User">
update user
<set>
<if test="username != null">
username = #{username},
</if>
<if test="password != null">
password = #{password},
</if>
<if test="role != null">
role = #{role},
</if>
<if test="touxiang != null">
touxiang = #{touxiang},
</if>
</set>
where id = #{id}
</update>
</mapper>
User.vue
<template>
<div>
<div class="search">
<el-input placeholder="请输入关键字查询" style="width: 200px" v-model="name"></el-input>
<el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
<el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
</div>
<div class="operation">
<el-button type="primary" plain @click="handleAdd">新增</el-button>
<el-button type="danger" plain @click="delBatch">批量删除</el-button>
</div>
<div class="table">
<el-table :data="tableData" strip @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center"></el-table-column>
<el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
<el-table-column prop="username" label="用户名"></el-table-column>
<el-table-column prop="password" label="密码"></el-table-column>
<el-table-column prop="role" label="角色"></el-table-column>
<el-table-column prop="touxiang" label="头像"></el-table-column>
<el-table-column label="操作" align="center" width="180">
<template v-slot="scope">
<el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
<el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination">
<el-pagination
background
@current-change="handleCurrentChange"
:current-page="pageNum"
:page-sizes="[5, 10, 20]"
:page-size="pageSize"
layout="total, prev, pager, next"
:total="total">
</el-pagination>
</div>
</div>
<el-dialog title="用户" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
<el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
<el-form-item label="用户名" prop="username">
<el-input v-model="form.username" placeholder="用户名"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="form.password" placeholder="密码"></el-input>
</el-form-item>
<el-form-item label="角色" prop="role">
<el-input v-model="form.role" placeholder="角色"></el-input>
</el-form-item>
<el-form-item label="头像" prop="touxiang">
<el-input v-model="form.touxiang" placeholder="头像"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="fromVisible = false">取 消</el-button>
<el-button type="primary" @click="save">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: "User",
data() {
return {
tableData: [], // 所有的数据
pageNum: 1, // 当前的页码
pageSize: 10, // 每页显示的个数
total: 0,
name: null,
fromVisible: false,
form: {},
user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
rules: {
},
ids: []
}
},
created() {
this.load(1)
},
methods: {
handleAdd() { // 新增数据
this.form = {} // 新增数据的时候清空数据
this.fromVisible = true // 打开弹窗
},
handleEdit(row) { // 编辑数据
this.form = JSON.parse(JSON.stringify(row)) // 给form对象赋值 注意要深拷贝数据
this.fromVisible = true // 打开弹窗
},
save() { // 保存按钮触发的逻辑 它会触发新增或者更新
this.$refs.formRef.validate((valid) => {
if (valid) {
this.$request({
url: this.form.id ? '/user/update' : '/user/add',
method: this.form.id ? 'PUT' : 'POST',
data: this.form
}).then(res => {
if (res.code === '200') { // 表示成功保存
this.$message.success('保存成功')
this.load(1)
this.fromVisible = false
} else {
this.$message.error(res.msg) // 弹出错误的信息
}
})
}
})
},
del(id) { // 单个删除
this.$confirm('您确定删除吗?', '确认删除', {type: "warning"}).then(response => {
this.$request.delete('/user/delete/' + id).then(res => {
if (res.code === '200') { // 表示操作成功
this.$message.success('操作成功')
this.load(1)
} else {
this.$message.error(res.msg) // 弹出错误的信息
}
})
}).catch(() => {
})
},
handleSelectionChange(rows) { // 当前选中的所有的行数据
this.ids = rows.map(v => v.id)
},
delBatch() { // 批量删除
if (!this.ids.length) {
this.$message.warning('请选择数据')
return
}
this.$confirm('您确定批量删除这些数据吗?', '确认删除', {type: "warning"}).then(response => {
this.$request.delete('/user/delete/batch', {data: this.ids}).then(res => {
if (res.code === '200') { // 表示操作成功
this.$message.success('操作成功')
this.load(1)
} else {
this.$message.error(res.msg) // 弹出错误的信息
}
})
}).catch(() => {
})
},
load(pageNum) { // 分页查询
if (pageNum) this.pageNum = pageNum
this.$request.get('/user/selectPage', {
params: {
pageNum: this.pageNum,
pageSize: this.pageSize,
name: this.name,
}
}).then(res => {
if (res.code === '200') {
this.tableData = res.data?.list
this.total = res.data?.total
} else {
this.$message.error(res.msg)
}
})
},
reset() {
this.name = null
this.load(1)
},
handleCurrentChange(pageNum) {
this.load(pageNum)
},
}
}
</script>
<style scoped>
</style>