springboot集成Mybatis-Plus并自动生成代码+Shiro权限管理

  1. 首先,刚才试了下,如果选择mybatis-plus自动生成的话,选择了那个数据库表,那么关于那个数据库的所有dao、service、Impl都会被覆盖,小心啊
  2. 通过mybatis-plus实现有则更新,无则添加
<!-- 实现无则插入,有则更新的save操作   -->
<insert id="save" parameterType="com.yyh.alcohol.entity.SysToken">
     INSERT INTO sys_token
     (user_id,
     expire_time,
     token,
     update_time)
     VALUES (
     #{userId,jdbcType=INTEGER},
     #{expireTime,jdbcType=TIMESTAMP},
     #{token,jdbcType=VARCHAR},
     #{updateTime,jdbcType=TIMESTAMP}
     )
     ON DUPLICATE KEY UPDATE
     expire_time = #{expireTime},
     token = #{token},
     update_time = #{updateTime}
 </insert>
  1. 这里的都是Mybatis-Plus自己封装好的直接可以调用的方法
    MyBatis-Plus
    mybatis-plus的使用 ------ 入门

    在service和serviceImpl都可以直接使用,使用参考:
    我现在在idea中写的alcohol项目的service层

  2. mybatis-plus的多表查询
    首先我看网上说的感觉mybatisplus处理多表查询没有什么好的方法,很多方法都是mybatis的方法

    可参考文章:
    最简单的 MyBatis Plus 的多表联接、分页查询实现方法

    MyBatisPlus多对多关联查询

    我觉得我还是参考mall-admin的代码吧…

  3. 现在我的数据库关于角色、权限、用户的表和mall-admin一样了,一共六张表,做到了

    一看就懂!Springboot +Shiro +VUE 前后端分离式权限管理系统
    第七步:编写自己的Realm
    在IDEA中已经导入该项目,继续照着改吧

    对了,我还用xml写了一些save和查找的方法,其实duck不必,mybatis-plus封装了大部分的方法: MyBatis-Plus官方文档

    主要思路:
    三张主表
    三张关系表

    可以用Mybatis-Plus自动生成一些代码
    对于关系表,推荐自己写dao,放在dao层,然后自己手写实现xml
    比如对于用户和角色的关系表,要获取一个用户的所有角色表,因为涉及两张表的查询,将方法写在dao层,然后在xml实现,并在用户的service中用方法封装,调用dao的方法

UserService :

public interface UserService extends IService<User> {
    /**
     * 获取用户所有角色
     */
    List<Role> getRoleList(Long userId);
}

UserServiceImpl:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public List<Role> getRoleList(Long userId) {
        return userRoleDao.getRoleList(userId);
    }
}

UserRoleDao:

package com.yyh.alcohol.dao;

import com.yyh.alcohol.entity.Permission;
import com.yyh.alcohol.entity.Role;
import com.yyh.alcohol.entity.UserRole;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 后台用户与角色管理自定义Dao
 * 这个是自己写的Dao,用来多表查询,不是生成器生成的
 */
public interface UserRoleDao {
    /**
     * 批量插入用户角色关系
     */
    int insertList(@Param("list") List<UserRole> userRoleList);

    /**
     * 获取用于所有角色
     */
    List<Role> getRoleList(@Param("userId") Long userId);

    /**
     * 获取用户所有角色权限
     */
    List<Permission> getRolePermissionList(@Param("userId") Long userId);

    /**
     * 获取用户所有权限(包括+-权限)
     */
    List<Permission> getPermissionList(@Param("userId") Long userId);
}

UserRoleDao.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.yyh.alcohol.dao.UserRoleDao">
    <!--批量新增回写主键支持-->
    <insert id="insertList">
        INSERT INTO user_role (user_id, role_id) VALUES
        <foreach collection="list" separator="," item="item" index="index">
            (#{item.userId,jdbcType=BIGINT},
            #{item.roleId,jdbcType=BIGINT})
        </foreach>
    </insert>
    <select id="getRoleList" resultMap="com.yyh.alcohol.mapper.RoleMapper.BaseResultMap">
        select r.*
        from user_role ar left join role r on ar.role_id = r.id
        where ar.user_id = #{userId}
    </select>
    <select id="getRolePermissionList" resultMap="com.yyh.alcohol.mapper.PermissionMapper.BaseResultMap">
        select p.*
        from user_role ar left join role r on ar.role_id = r.id
            left join role_permission rp on r.id = rp.role_id
            left join permission p on rp.permission_id=p.id
            where ar.user_id = #{userId} and p.id is not null
    </select>
    <select id="getPermissionList" resultMap="com.yyh.alcohol.mapper.PermissionMapper.BaseResultMap">
        SELECT
            p.*
        FROM
            user_role ar
            LEFT JOIN role r ON ar.role_id = r.id
            LEFT JOIN role_permission rp ON r.id = rp.role_id
            LEFT JOIN permission p ON rp.permission_id = p.id
        WHERE
            ar.user_id = #{userId}
            AND p.id IS NOT NULL
            AND p.id NOT IN (
                SELECT
                    p.id
                FROM
                    user_permission pr
                    LEFT JOIN permission p ON pr.permission_id = p.id
                WHERE
                    pr.type = - 1
                    AND pr.user_id = #{userId}
            )
        UNION
        SELECT
            p.*
        FROM
            user_permission pr
            LEFT JOIN permission p ON pr.permission_id = p.id
        WHERE
            pr.type = 1
            AND pr.user_id = #{userId}
    </select>
</mapper>

ps:自定义xml格式
IntelliJ IDEA中创建xml文件

options请求

好工具:

Swagger的增强版
knife4j
默认访问地址:http://localhost:8081/doc.html

bug:
SpringBoot整合Mybatis-Plus自动装配@Autowired失效问题

还没有真正实现权限管理,最主要的AuthRealm写的还不对

bug2:
shiro的doGetAuthorizationInfo一直没效果

步骤:
shiro中doGetAuthorizationInfo授权方法不执行问题
1.

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

关于使用springboot不编译xml文件到target目录!
2.
mave的install

spring-boot项目MapperScan注解包含多个包
3.
application

@MapperScan({"com.yyh.alcohol.dao","com.yyh.alcohol.mapper"})

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

yml:

mybatis-plus:
  mapper-locations:
    - classpath:dao/*.xml
    - classpath*:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

resultMap引用另一个Mapper文件的resultMap
原创

ResultMap的引用选择的是namespace+id 这里就是com.yyh.alcohol.mapper.PermissionMapper + BaseResultMap

<!--ResultMap的引用选择的是namespace+id  这里就是com.yyh.alcohol.mapper.PermissionMapper + BaseResultMap-->
<select id="getPermissionList" resultMap="com.yyh.alcohol.mapper.PermissionMapper.BaseResultMap">
    SELECT
        p.*
    FROM
        role_permission r
        LEFT JOIN permission p ON r.permission_id = p.id
    WHERE
        r.role_id = #{roleId};
</select>

天啦,终于成功了,主要测试权限的controller的代码TestController

@RequiresRoles({"品牌管理员"}) //没有的话 AuthorizationException
@PostMapping("/pmsread")
public Map<String, Object> save(@RequestHeader("token")String token) {
    System.out.println("pmsread");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("status", 200);
    map.put("msg", "当前用户有save的权力");
    return map;
}//f603cd4348b8f1d41226e3f555d392bd

@RequiresPermissions({"pms:product:read"}) //没有的话 AuthorizationException
@DeleteMapping("/pmscreate")
public Map<String, Object> delete(@RequestHeader("token")String token) {
    System.out.println("pmscreate");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("status", 200);
    map.put("msg", "当前用户有delete的权力");
    return map;
}

这两个分别对角色和权限去验证

@RequiresRoles({“品牌管理员”}) //没有的话 AuthorizationException
@RequiresPermissions({“pms:product:read”}) //没有的话

品牌管理员
pms:product:read
这是数据库里面报错的内容

不过这里实现是后端的权限管理,还有前台,emmm…

五.shiro表单拦截器FormAuthenticationFilter如何认证,登录成功后如何继续访问原请求

shiro过滤器详解分析

MarkerHub公众号文章索引

Vue + Spring Boot 项目实战(一):项目简介

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_42955958

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值