成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringBoot 數據權限新姿勢,注解+動態SQL真香!

開發 項目管理
easy-data-scop 是一個通過動態注入SQL實現的數據權限項目。支持MyBatis、MyBatis-plus、MyBatis-flex。使用簡單,無需設置各種復雜配置,僅僅通過注解便可實現效果功能。

介紹

easy-data-scop 是一個通過動態注入SQL實現的數據權限項目。支持MyBatis、MyBatis-plus、MyBatis-flex。使用簡單,無需設置各種復雜配置,僅僅通過注解便可實現效果功能。

基礎項目搭建

1.數據庫

圖片圖片

這是一張簡單的用戶表,接下來我們將為這張表編寫以下數據權限:

  • 僅看id為1的人
  • 僅看年齡為111的人
  • 僅看年齡為222的人
  • 看年齡為111、222的人

2.導入依賴基礎依賴 (使用MyBatis-plus、MyBatis XML演示)

<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.核心依賴

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

4.啟動類

@SpringBootApplication
publicclassMain{
    publicstaticvoidmain(String[] args){
        SpringApplication.run(Main.class);
    }
}

5.省略編寫Mapper、Service

6.application.yml

server:
  port:8001
# DataSource Config
spring:
datasource:
    driver-class-name:com.mysql.cj.jdbc.Driver
    url:url
    username:name
    password:password
mybatis:
mapper-locations:classpath:mapper/*.xml# XML映射文件路徑
mybatis-plus:
configuration:
    log-impl:org.apache.ibatis.logging.stdout.StdOutImpl

7.測試

@Autowired
private UserService userService;

@Test
publicvoidtest(){
    
    userService.getAll().forEach(System.out::println);
}

圖片圖片

到這里項目就已經搭建完成了。

使用 easy-data-scope

圖片圖片

實現核心接口DataScopeFindRule 并交由Spring管理。

圖片圖片

easy-data-scope 會去代理 @DataScope 方法調用 find() 獲取到 DataScopeInfo

DataScopeInfo介紹

easy-data-scope 會根據 find() 方法返回的 DataScopeInfo 列表來構建SQL。

圖片圖片

@DataScope介紹

可以編寫在對應需要數據權限攔截的方法上。

屬性:

public@interface DataScope {
    /**
     * 通過傳遞給DataScopeFindRule.find方法來獲取指定的數據權限實體
     * @return
     */
    String[] keys();

    /**
     * 構建模板
     * TODO 注意:當key為多個時此值生效
     * key1 ==SQL==> table1.column1 = 1
     * key2 ==SQL==> table2.column2 = 2
     * 示例:template = "{key1} OR {key2}"
     * 通過template生成后的SQL:table1.column1 = 1 OR table2.column2 = 2
     * @return
     */
    String template()default "";

    /**
     * 是否對數據權限進行自動合并
     * 當操作符為 =、!= 時間如果TableName、ColumnName、操作符一樣,并且使用的是 Value 形式將會對數據權限進行合并為 IN、NOT IN
     * 示例:
     * 權限1:=、table1、column1、Value1 >>> table1.column1 = Value1
     * 權限2:=、table1、column1、Value2 >>> table1.column1 = Value2
     * 最終合并 in table1、column1、“Value1, Value2" >>> table1.column1 in (Value1, Value2)
     * @return
     */
    booleanmerge()defaultfalse;

    /**
     * 邏輯符
     * 決定數據權限SQL拼接到當前執行的SQL中用的使用的是 WHERE還是AND還是OR..
     * TODO 注意:在flag為true時此值將會失效
     * @return
     */
    String logical()default SqlConsts.AND;

    /**
     * 是否使用數據權限標記位標記位,true是 false否
     * @return
     */
    booleanflag()defaultfalse;
}

圖片圖片

實現前文的數據權限

編寫DataScopeFindRule find 方法。

@Override
public List<DataScopeInfo> find(String[] key){
    // 模擬的用戶登陸Session
    UserSessionInfo userSession = UserSessionContext.getUserSession();
    if (userSession != null) {
        // 數據庫中查詢
        QueryWrapper<AuthDatascopeEntity> idQueryWrapper = new QueryWrapper<>();
        // 查詢用戶Session中保存用戶有哪些數據權限
        idQueryWrapper.in("id", userSession.getDataScopeIds());
        idQueryWrapper.in("datascope_key", key);
        List<AuthDatascopeEntity> authDatascopes = authDataSocpeMapper.selectList(idQueryWrapper);
        // 構建出DataScopeInfo
        List<DataScopeInfo> dataScopeInfos = new ArrayList<>(authDatascopes.size());
        for (AuthDatascopeEntity authDatascope : authDatascopes) {
            DataScopeInfo dataScopeInfo = new DataScopeInfo();
            dataScopeInfo.setKey(authDatascope.getDatascopeKey());
            dataScopeInfo.setOperator(authDatascope.getDatascopeOpName());
            dataScopeInfo.setTableName(authDatascope.getDatascopeTbName());
            dataScopeInfo.setColumnName(authDatascope.getDatascopeColName());
            dataScopeInfo.setSql(authDatascope.getDatascopeSql());
            dataScopeInfo.setValue(authDatascope.getDatascopeValue());
            dataScopeInfo.setSort(authDatascope.getDatascopeSort());
            dataScopeInfos.add(dataScopeInfo);
        }
        return dataScopeInfos;
    }

    return Collections.emptyList();
}

創建數據權限表

-- auto-generated definition
createtable auth_datascope
(
    id                 int auto_increment comment'編號'
        primary key ,
    datascope_key      varchar(200)  nullcomment'數據權限標識' ,
    datascope_name     varchar(200)  nullcomment'數據權限名稱' ,
    datascope_tb_name  varchar(500)  nullcomment'數據權限表別名' ,
    datascope_col_name varchar(500)  nullcomment'數據權限字段名' ,
    datascope_op_name  varchar(10)   nullcomment'數據權限操作符' ,
    datascope_sql      varchar(5000) nullcomment'數據權限sql' ,
    datascope_value    varchar(200)  nullcomment'數據權限值' ,
    datascope_sort     int           nullcomment'數據權限排序' ,
    datascope_des      varchar(500)  nullcomment'數據權限描述'
)
    comment'數據權限表';

1.只看Id為1的記錄

圖片圖片

將對應實體添加到庫中,實現動態配置。

編寫Service:

@DataScope(keys = "USER_LIST_ID", logical = SqlConsts.WHERE)
public List<UserEntity> getAll(){
    return userMapper.selectList(null);
}

調用后得到結果:

SELECTid,username,age FROMuserWHERE ( user.id = 1)

2.僅看年齡為111的人

圖片圖片

@DataScope(keys = "USER_LIST_AGE111", logical = SqlConsts.WHERE)
public List<UserEntity> getAll2(){
    return userMapper.selectList(null);
}

調用后得到結果:

SELECTid,username,age FROMuserWHERE ( user.age = 111)

3.僅看年齡為222的人

圖片圖片

@DataScope(keys = "USER_LIST_AGE222", logical = SqlConsts.WHERE)
public List<UserEntity> getAll3(){
    return userMapper.selectList(null);
}

調用后得到結果:

SELECTid,username,age FROMuserWHERE ( user.age = 222)

4.看年齡為111、222的人(merge屬性)

其他的不用動,使用注解中的 merge 屬性,在keys中將兩個前兩個key都加上。

@DataScope(keys = {"USER_LIST_AGE111", "USER_LIST_AGE222"}, merge = true, logical = SqlConsts.WHERE)
public List<UserEntity> getAll4(){
    return userMapper.selectList(null);
}

調用后得到結果:

SELECTid,username,age FROMuserWHERE ( user.age IN (111, 222))

更多操作

@DataScope.flag

Mapper.xml

@DataScope(keys = {"USER_LIST_AGE111", "USER_LIST_AGE222"}, merge = true, flag = true)
List<UserEntity> getAll5();
<selectid="getAll5"resultType="cn.zlinchuan.entity.UserEntity">
    select * from (select * from user where {{_DATA_SCOPE_FLAG}}) t where 1 = 1
</select>

注意 {{_DATA_SCOPE_FLAG}} 為程序定義占位,不能修改。

sql

select * from (select * fromuserwhere user.age IN (111, 222)) t where1 = 1

@DataScope.template

@DataScope(keys = {"USER_LIST_AGE111", "USER_LIST_AGE222"}, flag = true, template = "{{USER_LIST_AGE111}} OR {{USER_LIST_AGE222}}")
List<UserEntity> getAll6();
<selectid="getAll6"resultType="cn.zlinchuan.entity.UserEntity">
    select * from (select * from user where {{_DATA_SCOPE_FLAG}}) t where 1 = 1
</select>

sql

select * from (select * fromuserwhere user.age = 111OR user.age = 222) t where1 = 1
項目源碼地址:https://github.com/zoulinchuan/easy-data-scope
責任編輯:武曉燕 來源: 碼猿技術專欄
相關推薦

2024-10-28 07:10:00

scroll標記前端網格布局

2025-02-17 11:41:14

2024-04-30 11:49:16

瀏覽器前端開發折疊屏應用

2024-01-18 15:17:56

谷歌云計算三星

2021-05-26 08:21:43

@Autowired項目@Resouce

2025-02-19 12:00:00

SpringBootDeepSeekAI

2023-12-13 07:59:04

2019-02-27 09:08:20

Java 8StringJoineIDEA

2024-06-25 12:10:26

2018-02-25 11:24:02

APPiPhone手機

2025-02-07 10:52:00

2023-12-19 07:30:58

MySQL數據恢復數據庫

2018-03-06 17:24:57

2016-09-29 22:36:40

2023-07-18 09:00:00

ChatGPT文本轉語音

2025-04-21 03:30:00

2023-03-09 08:15:56

GPT-3系統數據

2019-01-16 07:30:47

2024-03-07 12:11:31

PoetryPython代碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品免费一区二区三区四区 | 黄色大片免费播放 | 久久99精品久久久久久国产越南 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲精品视频在线看 | 91国产视频在线观看 | 亚洲一区国产 | 超碰地址 | 亚洲精品日韩欧美 | 久久久精品一区二区 | 日韩中文字幕一区二区 | 成人欧美日韩一区二区三区 | 国产高清精品一区二区三区 | 大香在线伊779 | 欧美性极品xxxx做受 | 日本在线网站 | 日本精品裸体写真集在线观看 | 欧美一区 | 亚洲成人网在线播放 | 中文字幕一区二区三区在线观看 | 亚洲一区二区三区在线播放 | 爱爱免费视频网站 | 一级看片免费视频囗交动图 | 青青草原综合久久大伊人精品 | 欧美综合一区 | 久久精品电影 | 九九免费在线视频 | 精品一区二区三区免费视频 | 精品久久久久久久久久久下田 | 成人在线看片 | 日韩精品| 一区二区三区在线免费观看 | 蜜桃在线视频 | 最新国产福利在线 | 日韩免费一区二区 | 欧美精品久久 | 国产成人精品久久二区二区91 | 日韩欧美在线观看视频 | 粉嫩av久久一区二区三区 | 国产黄色一级片 | 国产成人免费视频网站高清观看视频 |