Backend Development 9 min read

Easy-Data-Scope: A Simple Data‑Permission Library for MyBatis and Spring Boot

This article introduces easy-data-scope, a lightweight Java library that enables dynamic SQL‑based data permission control for MyBatis, MyBatis‑Plus, and MyBatis‑Flex, showing how to set up the project, configure dependencies, define annotations, and apply various permission scenarios with code examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Easy-Data-Scope: A Simple Data‑Permission Library for MyBatis and Spring Boot

Introduction – easy-data-scope is a data‑permission project that works by dynamically injecting SQL and supports MyBatis, MyBatis‑Plus, and MyBatis‑Flex. It requires only annotations to achieve the desired functionality.

Basic Project Setup

1. Database – A simple user table is used as the data source for permission testing.

2. Import Basic Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version>
    </dependency>
</dependencies>

3. Core Dependency

<dependency>
    <groupId>cn.zlinchuan</groupId>
    <artifactId>ds-mybatis</artifactId>
    <version>1.0.1</version>
</dependency>

4. Main Application Class

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}

5. Omitted Mapper and Service implementations – they follow standard MyBatis patterns.

6. application.yml Configuration

server:
  port: 8001
# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/yourdb
    username: name
    password: password
mybatis:
  mapper-locations: classpath:mapper/*.xml
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Using easy-data-scope

The core interface DataScopeFindRule is implemented and managed by Spring. Methods annotated with @DataScope trigger the injection of DataScopeInfo objects that build the permission SQL.

DataScopeInfo – represents a single permission rule (key, operator, table, column, SQL, value, etc.) and is constructed from the result of DataScopeFindRule.find() .

@DataScope Annotation

public @interface DataScope {
    /** Keys used to fetch permission entities */
    String[] keys();
    /** Optional SQL template when multiple keys are present */
    String template() default "";
    /** Whether to merge conditions with the same column */
    boolean merge() default false;
    /** Logical connector (WHERE, AND, OR, …) */
    String logical() default SqlConsts.AND;
    /** Flag to enable/disable the permission filter */
    boolean flag() default false;
}

Examples show how to restrict queries to a specific user ID, to users of a certain age, and how to merge multiple age conditions using the merge attribute.

Creating the Permission Table

create table auth_datascope (
    id int auto_increment primary key comment '编号',
    datascope_key varchar(200) null comment '数据权限标识',
    datascope_name varchar(200) null comment '数据权限名称',
    datascope_tb_name varchar(500) null comment '数据权限表别名',
    datascope_col_name varchar(500) null comment '数据权限字段名',
    datascope_op_name varchar(10) null comment '数据权限操作符',
    datascope_sql varchar(5000) null comment '数据权限sql',
    datascope_value varchar(200) null comment '数据权限值',
    datascope_sort int null comment '数据权限排序',
    datascope_des varchar(500) null comment '数据权限描述'
) comment '数据权限表';

Various service methods demonstrate how the generated SQL looks for each scenario, including single‑key filters, merged IN clauses, and custom templates.

Project Source Code

https://github.com/zoulinchuan/easy-data-scope

Additional references: Juejin article https://juejin.cn/post/7483708438692102144.

Community Invitation – The author invites readers to join a backend‑focused technical group, emphasizing genuine technical exchange and warning against advertisements.

JavaSQLSpring BootMyBatisAnnotationData Permissioneasy-data-scope
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.