day06_springboot综合案例

资源权限管理

查询资源权限

查询资源权限执行流程

image-20201017154404234

编写PermissionMapper接口

public interface PermissionMapper {

    /**
     * 查询资源权限
     */
    List<Permission> findAll();
}

编写PermissionService

public interface PermissionService {

    /**
     * 查询资源权限
     * @param page
     * @param size
     * @return
     */
    List<Permission> findAll();
}
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 查询资源权限
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<Permission> findAll() {
        
        return this.permissionMapper.findAll();
    }
}

编写PermissionController

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询所有
     * @param page
     * @param size
     * @param model
     * @return
     */
    @GetMapping("findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1") Integer page ,
                          @RequestParam(value = "size",defaultValue = "5") Integer size,
                          Model model){

        PageHelper.startPage(page,size);

        List<Permission> list =  this.permissionService.findAll();

        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("pageInfo",pageInfo);

        return "permission-list";
    }
}

编写PermissionMapper.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="cn.yanqi.ssm.mapper.PermissionMapper">

    <select id="findAll" resultType="Permission">
        select * from permission
    </select>

</mapper>

测试

image-20210520232246481

添加资源权限

添加资源权限执行流程

image-20201017160527516

编写PermissionMapper接口

    /**
     * 添加资源权限
     * @param permission
     */
    void save(Permission permission);

编写PermissionService

    /**
     * 添加资源权限
     * @param permission
     */
    void save(Permission permission);
   /**
     * 添加资源权限
     * @param permission
     */
    @Override
    public void save(Permission permission) {
        this.permissionMapper.save(permission);
    }

编写PermissionController

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 添加资源权限
     * @param permission
     * @return
     * @throws Exception
     */
    @RequestMapping("save")
    public String save(Permission permission){
        permissionService.save(permission);
        return "redirect:findAll";
    }
}

编写PermissionMapper.xml

    <!--添加资源权限-->
    <insert id="save">
        insert into permission(permissionName,url) values(#{permissionName},#{url})
    </insert>

测试

image-20201017164511136

image-20201017164533405

获取用户名

后台代码

@Controller
@RequestMapping("user")
public class UserController {

    /**
     * 获取登录的用户名
     * @return
     */
    @RequestMapping("getUsername")
    @ResponseBody
    public String getUsername(){
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        return username;
    }
}    

页面显示

aside.html

<!--显示用户名-->
<span id="asideName"></span> 在线

header.html

<!--显示用户名-->
<span class="hidden-xs" id="headName"> </span>

在static下创建js,通过js获取头部和侧边栏 显示用户名span标签

<img src="assets/image-20230614141947261.png" alt="image-20230614141947261" style="zoom:50%;" />

    /*获取用户登录名称*/
    function getLoginUsername(){
        $.get('user/getUsername',function (data){
            $('#asideName').text(data)
            $('#headName').text(data)
        });
    }

每一个页面引入 getPrincipalUserName.js