# mybatis-share **Repository Path**: skydengw/mybatis-share ## Basic Information - **Project Name**: mybatis-share - **Description**: 分库分表插件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2017-06-06 - **Last Updated**: 2020-12-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #mybatis-share分库分表插件 #版本介绍: - 1.0版本实现分表的功能 - 2.0后续版本集成分库功能 **使用说明** 1. 配置mybatis的配置文件config.xml ``` ``` 2. 插件中有MyBatisShareUtils.java这个工具类。该工具类可以在执行该sql前设置分表后缀,该sql执行后,分表信息自动清除。 MyBatisShareUtils.setSupperShareTableSuffix("");该方法是设置优先级最高的分表后缀 MyBatisShareUtils.setShareTableSuffix("");该方法是设置优先级最低的分表后缀,可以作为统一的分表的后缀设置,通过spring的AOP进行设置 如果使用AOP进行统一后缀拦截需要使用@MybatisShare注解一同使用,(注意:该注解只能用在实现方法上面,接口上不起作用) AOP示例配置: ``` /** * Aibton.com Inc. * Copyright (c) 2004-2017 All Rights Reserved. */ package com.aibton.share.demo.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import com.aibton.mybatis.share.utils.MyBatisShareUtils; import com.aibton.share.demo.controller.SysUserController; /** * @author huzhihui * @version v 0.1 2017/6/4 21:11 huzhihui Exp $$ */ @Aspect @Configuration public class MybatisShareAop { private static final Logger logger = LoggerFactory.getLogger(MybatisShareAop.class); // && @annotation(com.aibton.mybatis.share.annotation.MybatisShare) @Pointcut("execution(public * com.aibton.share.demo..*.*(..)) && @annotation(com.aibton.mybatis.share.annotation.MybatisShare)") public void shareTable() { } @Before("shareTable()") public void setDataSourceKey(JoinPoint point) throws Exception { MyBatisShareUtils.setShareTableSuffix("_" + SysUserController.userId); } } ``` 配置完成后就能看到效果啦: ``` insert into sys_user_01 (sys_user_id, sys_user_nickname, delete from sys_user_01 where sys_user_id = ? ```