溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis-Plus動態(tài)表名如何使用

發(fā)布時間:2023-04-07 11:41:07 來源:億速云 閱讀:165 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“MyBatis-Plus動態(tài)表名如何使用”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

MyBatis-Plus實現(xiàn)動態(tài)表名

MyBatis實現(xiàn)方法如下現(xiàn)在要用MyBatis-Plus 實現(xiàn)

   <select id="getList" resultType="com.wys.entity.User">
        SELECT *
        FROM ${tableName}
    </select>

MyBatis-Plus官網(wǎng)說明

MyBatis-Plus動態(tài)表名如何使用

MyBatis-Plus版本

1、添加MyBatis-Plus依賴

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.1</version>
</dependency>

MyBatis-Plus配置

2、添加MyBatis-Plus配置,利用攔截器獲取到表名給替換

@Configuration
public class MybatisPlusConfig {
    static List<String> tableList(){
        List<String> tables = new ArrayList<>();
        //偽表名  可以為任意字符串 建議設(shè)置復(fù)雜度 避免重復(fù)    tables.add("C55EA8171877E962E08DFF63AA3678841");
        tables.add("TestUser");
        return tables;
    }

	//攔截器,獲取到表名給替換
    @Bean
    public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            String newTable = null;
            for (String table : tableList()) {
                newTable = RequestDataHelper.getRequestData(table);
                if (table.equals(tableName) && newTable!=null){
                    tableName = newTable;
                    break;
                }
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }
}

如果上面的攔截器不生效可以使用下面這個http://www.kemok4.com/article/280321.htm

@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisPlusConfig {
    static List<String> tableList(){
        List<String> tables = new ArrayList<>();
        //表名
        tables.add("C55EA8171877E962E08DFF63AA3678841");
        return tables;
    }

    //攔截器,獲取到表名給替換
//    @Bean
//    public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
//        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
//            String newTable = null;
//            for (String table : tableList()) {
//                newTable = RequestDataHelper.getRequestData(table);
//                if (table.equals(tableName) && newTable!=null){
//                    tableName = newTable;
//                    break;
//                }
//            }
//            return tableName;
//        });
//        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
//        return interceptor;
//    }

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void addMyInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            String newTable = null;
            for (String table : tableList()) {
                newTable = RequestDataHelper.getRequestData(table);
                if (table.equals(tableName) && newTable!=null){
                    tableName = newTable;
                    break;
                }
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}

請求參數(shù)傳遞輔助類

3、創(chuàng)建請求參數(shù)傳遞輔助類

public class RequestDataHelper {
    /**
     * 請求參數(shù)存取
     */
    private static final ThreadLocal<Map<String, Object>> REQUEST_DATA = new ThreadLocal<>();

    /**
     * 設(shè)置請求參數(shù)
     *
     * @param requestData 請求參數(shù) MAP 對象
     */
    public static void setRequestData(Map<String, Object> requestData) {
        REQUEST_DATA.set(requestData);
    }

    /**
     * 獲取請求參數(shù)
     *
     * @param param 請求參數(shù)
     * @return 請求參數(shù) MAP 對象
     */
    public static <T> T getRequestData(String param) {
        Map<String, Object> dataMap = getRequestData();
        if (CollectionUtils.isNotEmpty(dataMap)) {
            return (T) dataMap.get(param);
        }
        return null;
    }

    /**
     * 獲取請求參數(shù)
     *
     * @return 請求參數(shù) MAP 對象
     */
    public static Map<String, Object> getRequestData() {
        return REQUEST_DATA.get();
    }
}

使用

MyBatis-Plus動態(tài)表名如何使用

4、在程序中使用,注意如果實際表名與實體類與不同,可先在實體類類注明表名@TableName(“TestUser”)

@GetMapping("/listUser")
    public void listUser(){

        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("kfafkasfaskfasjfkasf", "user_2018");

        }});
        Integer age=2018;

       User user=new User();
        List list = userMapper.getList(user);
     //  User user_2019 = userMapper.findById("user_2019", 2019);

        System.out.println(list);
        System.out.println("-------------");
       // System.out.println(user_2019);
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("kfafkasfaskfasjfkasf", "user_2019");

        }});
        List lis2 = userMapper.getList(user);
        System.out.println(lis2);
        System.out.println("-------------");

    }

MyBatis-Plus動態(tài)表名如何使用

結(jié)果:

MyBatis-Plus動態(tài)表名如何使用

“MyBatis-Plus動態(tài)表名如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI