• 031 SSM综合练习07--数据后台管理系统--用户详情查询


    1.用户详情查询流程分析

    2.代码实现

    (1)user-list.jsp页面部分代码

    点击jsp页面中的详情按钮,发送请求到UserController.java

    <!--数据列表-->
                                <table id="dataList"
                                    class="table table-bordered table-striped table-hover dataTable">
                                    <thead>
                                        <tr>
                                            <th class="" style="padding-right: 0px"><input
                                                id="selall" type="checkbox" class="icheckbox_square-blue">
                                            </th>
                                            <th class="sorting_asc">ID</th>
                                            <th class="sorting_desc">用户名</th>
                                            <th class="sorting_asc sorting_asc_disabled">邮箱</th>
                                            <th class="sorting_desc sorting_desc_disabled">联系电话</th>
                                            <th class="sorting">状态</th>
                                            <th class="text-center">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
    
                                        <c:forEach items="${userList}" var="user">
                                            <tr>
                                                <td><input name="ids" type="checkbox"></td>
                                                <td>${user.id }</td>
                                                <td>${user.username }</td>
                                                <td>${user.email }</td>
                                                <td>${user.phoneNum }</td>
                                                <td>${user.statusStr }</td>                                            
                                                <td class="text-center">
                                                    <a href="${pageContext.request.contextPath}/user/queryById?id=${user.id}" class="btn bg-olive btn-xs">详情</a>
                                                    <a href="${pageContext.request.contextPath}/user/findUserByIdAndAllRole.do?id=${user.id}" class="btn bg-olive btn-xs">添加角色</a>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                    </tbody>
                                    <!--
                                <tfoot>
                                <tr>
                                <th>Rendering engine</th>
                                <th>Browser</th>
                                <th>Platform(s)</th>
                                <th>Engine version</th>
                                <th>CSS grade</th>
                                </tr>
                                </tfoot>-->
                                </table>

    (2)表现层:UserController.java

    package lucky.controller;
    
    import lucky.domain.UserInfo;
    import lucky.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.util.List;
    
    @Controller
    @RequestMapping(path = "/user")
    public class UserController {
        @Autowired
        private IUserService iUserService;
    /**
         * 查询指定id的用户
         * @return 查询结果
         * @throws Exception
         * 注意:SpringMVC 绑定请求参数是自动实现的,会自动将参数绑定queryById方法的id上
         */
        @RequestMapping(path = "/queryById")
        public ModelAndView queryById(String id) throws Exception{
            ModelAndView mv=new ModelAndView();
            UserInfo userInfo=iUserService.queryById(id);
            mv.addObject("user",userInfo);
            mv.setViewName("user-show1");
            return mv;
        }
    
    
    }

    (3)服务层:IUserService及其实现类

    package lucky.service;
    
    import lucky.domain.UserInfo;
    import org.springframework.security.core.userdetails.UserDetailsService;
    
    import java.util.List;
    
    public interface IUserService extends UserDetailsService {
    
        /**
         * 查询所有用户
         */
        public List<UserInfo> queryAll() throws Exception;
    
        /**
         * 保存用户
         * @param userInfo 需要保存的用户
         */
        void save(UserInfo userInfo) throws Exception;
    
        /**
         * 根据用户id查询
         * @param id 用户id
         */
        UserInfo queryById(String id) throws Exception;
    }

    实现类:

    package lucky.service.impl;
    
    import lucky.dao.IUserDao;
    import lucky.domain.Role;
    import lucky.domain.UserInfo;
    import lucky.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.stereotype.Service;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 使用springsecurity进行用户登录
     */
    @Service(value = "userService")
    public class UserServiceImpl implements IUserService {
        @Autowired
        private IUserDao iUserDao;
    
        @Autowired
        private BCryptPasswordEncoder bCryptPasswordEncoder;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            UserInfo userInfo=null;
            try {
                userInfo=iUserDao.queryByUsername(username);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //将自己的用户对象封装成UserDetails,参数1为用户名,参数2为密码,参数3是用户权限
            User user=new User(userInfo.getUsername(),userInfo.getPassword(),getAuthority(userInfo.getRoles()));
            return user;
        }
    
    
        /**
         * @return 返回一个list集合,集合中装的是用户权限描述
         * @param roles
         */
        public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
            List<SimpleGrantedAuthority> list=new ArrayList<>();
            for (Role role : roles) {
                list.add(new SimpleGrantedAuthority(role.getRoleName()));
            }
            return list;
        }
    
        @Override
        public List<UserInfo> queryAll() throws Exception {
            return iUserDao.queryAll();
        }
    
        @Override
        public void save(UserInfo userInfo) throws Exception {
            //调用spirngsecurity这个加密类BCryptPasswordEncoder,对密码进行加密
            userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
            iUserDao.save(userInfo);
        }
    
        /**
         * 根据用户id查询
         * @param id 用户id
         * @return 查询结果
         * @throws Exception
         */
        @Override
        public UserInfo queryById(String id) throws Exception {
            return iUserDao.queryById(id);
        }
    }

    (4)持久层Dao

    <1>IUserDao.java

    package lucky.dao;
    
    import lucky.domain.UserInfo;
    import org.apache.ibatis.annotations.*;
    
    import java.util.List;
    
    public interface IUserDao {
        /**
         * @param username 用户名
         * @Results 映射查询结果集到实体类属性
         * column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。
         * 涉及多对多查询,有中间表
         */
        @Select("select * from LUCKY.USERS where username=#{username}")
        @Results({
                @Result(id=true,property = "id",column = "id"),
                @Result(property = "username",column = "username"),
                @Result(property = "email",column = "email"),
                @Result(property = "password",column = "password"),
                @Result(property = "phoneNum",column = "phoneNum"),
                @Result(property = "status",column = "status"),
                @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select="lucky.dao.IRoleDao.queryByUserId")),
        })
        public UserInfo queryByUsername(String username) throws Exception;
    
        /**
         * @return 查询所有用户
         */
        @Select("select * from LUCKY.USERS")
        public List<UserInfo> queryAll() throws Exception;
    
        @Insert("insert into LUCKY.USERS(email,username,password,phoneNum,status) values(#{email},#{username},#{password},#{phoneNum},#{status})")
        public void save(UserInfo userInfo) throws Exception;
    
        /**
         * 根据id查询
         * @param id 用户id
         */
        @Select("select * from LUCKY.USERS where id=#{id}")
        @Results({
                @Result(id=true,property = "id",column = "id"),
                @Result(property = "username",column = "username"),
                @Result(property = "email",column = "email"),
                @Result(property = "password",column = "password"),
                @Result(property = "phoneNum",column = "phoneNum"),
                @Result(property = "status",column = "status"),
                @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select="lucky.dao.IRoleDao.queryByUserId")),
        })
        UserInfo queryById(String id) throws Exception;
    }

    <2>IRoleDao.java

    package lucky.dao;
    
    import lucky.domain.Role;
    import org.apache.ibatis.annotations.Many;
    import org.apache.ibatis.annotations.Result;
    import org.apache.ibatis.annotations.Results;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    public interface IRoleDao {
        /**
         * 多对多查询
         * 思路分析:先根据userid,从中间表USERS_ROLE中查到roleId,再根据roleId到ROLE表查询数据
         * @param userId USER表id值
         * @return 查询结果
         */
        @Select("select * from LUCKY.ROLE where id in (select roleId from LUCKY.USERS_ROLE WHERE userId=#{userId})")
        @Results({
                @Result(id=true,property = "id",column = "id"),
                @Result(property = "roleName",column = "roleName"),
                @Result(property = "roleDesc",column = "roleDesc"),
                @Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select="lucky.dao.IPermissionsDao.queryByRoleId")),
        })
        public List<Role> queryByUserId(String userId)throws Exception;
    }

    <3>IPermission.java

    package lucky.dao;
    
    import lucky.domain.Permission;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    public interface IPermissionsDao {
        @Select("select * from LUCKY.PERMISSION where id in (select permissionId from LUCKY.ROLE_PERMISSION WHERE roleId=#{roleId})")
        public List<Permission> queryByRoleId(String roleId);
    }

    (5)user-show1.jsp部分代码

    <div class="box-body">
                            <!--树表格-->
                            <div class="tab-pane" id="tab-treetable">
                                <table id="collapse-table" class="table table-bordered table-hover dataTable">
                                    <thead>
                                    <tr>
                                        <th>用户</th>
                                        <th>描述</th>
                                    </tr>
                                    </thead>
    
                                    <tr data-tt-id="0">
                                        <td colspan="2">${user.username}</td>
                                    </tr>
    
                                    <tbody>
                                    <c:forEach items="${user.roles}" var="role" varStatus="vs">
                                        <tr data-tt-id="${vs.index+1}" data-tt-parent-id="0">
                                            <td>${role.roleName }</td>
                                            <td>${role.roleDesc }</td>
                                        </tr>
                                        <c:forEach items="${role.permissions}" var="permission">
                                            <tr data-tt-id="1-1" data-tt-parent-id="${vs.index+1}">
                                                <td>${permission.permissionName}</td>
                                                <td>${permission.url}</td>
                                            </tr>
    
                                        </c:forEach>
                                    </c:forEach>
    
                                    </tbody>
                                </table>
                            </div>
                            <!--树表格/-->
    
    
                    <!-- .box-footer
        <div class="box-footer"></div>
        -->
                    <!-- /.box-footer-->
    
                </div>

    3.效果图

  • 相关阅读:
    图片在线压缩
    超级棒的前端学习网站
    关于python的一些学习
    强大的前端网站
    一个技术交流网站
    发现一个全部用html5写的网站,里面还有很多知识
    发现一个改变滚动条的大小颜色等样式的方法
    建立数据库镜像
    【转】怎样查出SQLServer的性能瓶颈
    sys.dm_tran_locks,
  • 原文地址:https://www.cnblogs.com/luckyplj/p/11414187.html
Copyright © 2020-2023  润新知