1、添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.liubaihui</groupId> <artifactId>datapress-test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>datapress-test</name> <description>datapress-test</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>3.0.2</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.0.2</version> </plugin> </plugins> </build> </project>
2、添加拦截器
package com.liubaihui.datapresstest.configure; import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport; import com.baomidou.mybatisplus.extension.plugins.handler.MultiDataPermissionHandler; import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import lombok.*; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.statement.select.Select; import net.sf.jsqlparser.statement.select.SelectBody; import net.sf.jsqlparser.statement.select.SetOperationList; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.sql.SQLException; import java.util.List; @Data @NoArgsConstructor @AllArgsConstructor @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) @Component public class MyDataPermissionInterceptor extends DataPermissionInterceptor { @Autowired private MyDataPermissionHandler myDataPermissionHandler=new MyDataPermissionHandler(); @Override public Expression buildTableExpression(Table table, Expression where, String whereSegment) { return myDataPermissionHandler.getSqlSegment(table, where, whereSegment); } }
3、添加处理器
package com.liubaihui.datapresstest.configure; import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler; import com.baomidou.mybatisplus.extension.plugins.handler.MultiDataPermissionHandler; import com.baomidou.mybatisplus.generator.jdbc.DatabaseMetaDataWrapper; import net.sf.jsqlparser.expression.Alias; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.HexValue; import net.sf.jsqlparser.expression.StringValue; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.expression.operators.relational.EqualsTo; import net.sf.jsqlparser.expression.operators.relational.ExpressionList; import net.sf.jsqlparser.expression.operators.relational.InExpression; import net.sf.jsqlparser.expression.operators.relational.ItemsList; import net.sf.jsqlparser.schema.Column; import net.sf.jsqlparser.schema.Table; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; /** * 参考:https://baijiahao.baidu.com/s?id=1773281250190331633&wfr=spider&for=pc * @author: liubh * @since: 2023/10/15 */ @Component public class MyDataPermissionHandler implements MultiDataPermissionHandler { @Override public Expression getSqlSegment(Table table, Expression where, String mappedStatementId) { if (where == null) { where = new HexValue(" 1 = 1 "); } //获取mapper名称 String className = mappedStatementId.substring(0, mappedStatementId.lastIndexOf(".")); //获取方法名 String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(".") + 1); // String fromItem = table.getName(); // // 有别名用别名,无别名用表名,防止字段冲突报错 // Alias fromItemAlias = fromItem.getAlias(); // String mainTableName = fromItemAlias == null ? fromItem.getName() : fromItemAlias.getName(); //获取当前mapper 的方法 Method[] methods = new Method[0]; try { methods = Class.forName(className).getMethods(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } //遍历判断mapper 的所以方法,判断方法上是否有 UserDataPermission for (Method m : methods) { if (Objects.equals(m.getName(), methodName)) { EqualsTo usesEqualsTo = new EqualsTo(); usesEqualsTo.setLeftExpression( new Column(table.getName() + ".company_id")); usesEqualsTo.setRightExpression(new StringValue("1")); return new AndExpression(where, usesEqualsTo); } } //说明无权查看, where = new HexValue(" 1 = 2 "); return where; } }
4、设置拦截器
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 添加数据权限插件 MyDataPermissionInterceptor dataPermissionInterceptor = new MyDataPermissionInterceptor(); // 添加自定义的数据权限处理器 dataPermissionInterceptor.setDataPermissionHandler(new MyDataPermissionHandler()); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加 interceptor.addInnerInterceptor(dataPermissionInterceptor); return interceptor; }
参考:实现过滤多个字段值 https://baijiahao.baidu.com/s?id=1773281250190331633&wfr=spider&for=pc
标签:mybatisplus3.5,jsqlparser,版本,org,import,net,权限,com,sf From: https://www.cnblogs.com/liubaihui/p/17765898.html