后台用户管理功能设计(个人博客)
作者:互联网
后台用户管理功能设计(个人博客)
1、编写dao层
@Mapper
public interface UserMapper {
@Select("select * from user where username=#{username}")
User getByName(String username);
@Update("update user set password = #{password},nickname = #{nickname},avatar = #{avatar}" +
"where id = #{id}")
int updateUser(User user);
int deleteByPrimaryKey(Long id);
int insert(User record);
List<User> selectAll();
}
2、编写绑定配置文件
<?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.pang.dao.UserMapper" >
<resultMap id="BaseResultMap" type="user" >
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="nickname" property="nickname"/>
<result column="avatar" property="avatar"/>
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from user
where id = #{id}
</delete>
<insert id="insert" parameterType="user" >
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into user (username, password, nickname,
avatar)
values (#{username}, #{password}, #{nickname},
#{avatar})
</insert>
<update id="updateByPrimaryKey" parameterType="user" >
update user
set username = #{username},
password = #{password},
nickname = #{nickname},
avatar = #{avatar}
where id = #{id}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select id, username, password, nickname, avatar
from user
where id = #{id}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, username, password, nickname, avatar
from user
</select>
<select id="login" resultMap="BaseResultMap" >
select id, username, password, nickname, avatar
from user
where username = #{username}
and password = #{password}
</select>
</mapper>
3、编写service层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> selectAll() {
return userMapper.selectAll();
}
public int updateUser(User user) {
return userMapper.updateUser(user);
}
public User getByName(String username){
return userMapper.getByName(username);
}
public int deleteUser(Long id){
return userMapper.deleteByPrimaryKey(id);
}
}
4、编写controller层
@RequestMapping("/toUpdUser")
public String toUpdateUser(Principal principal, Model model){
User user = userService.getByName(principal.getName());
model.addAttribute("user",user);
return "admin/users-update";
}
@RequestMapping("/admin/updUser")
public String updUser(User user){
userService.updateUser(user);
return "redirect:/logout";
}
@RequestMapping("/admin/deleteUser/{id}")
public String deleteUser(@PathVariable("id") Long id){
userService.deleteUser(id);
return "redirect:/admin/toUser";
}
5、绑定前端页面
<table class="ui celled table">
<thead>
<tr>
<th>编号</th>
<th>用户名</th>
<th>个性名称</th>
<th>头像链接</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="user:${pageInfo.list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.nickname}"></td>
<td th:text="${user.avatar}"></td>
<td>
<a th:href="@{/admin/deleteUser/{id}(id=${user.id})}" class="ui mini red basic button">删除</a>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="6">
<div class="ui mini pagination menu">
<a class="icon item" th:href="@{/admin/toUser(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" th:unless="${pageInfo.isFirstPage}">上一页</a>
<a class="icon item" th:href="@{/admin/toUser(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" th:unless="${pageInfo.isLastPage}">下一页</a>
</div>
</th>
</tr>
</tfoot>
</table>
标签:username,博客,public,user,后台,password,nickname,id,功能设计 来源: https://blog.csdn.net/datou0_0/article/details/114209993