# springboot-mybatis-wr-separation **Repository Path**: sunchenbin/springboot-mybatis-wr-separation ## Basic Information - **Project Name**: springboot-mybatis-wr-separation - **Description**: springboot-mybatis-wr-separation is a Maven project based on SpringBoot and Mybatis, which is Provide read-write separation function.Supporting one master DB and one slave DB. - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: develop - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 0 - **Created**: 2019-05-15 - **Last Updated**: 2021-04-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # springboot-mybatis-wr-separation #### 介绍 springboot-mybatis-wr-separation is a Maven project based on SpringBoot and Mybatis, which is Provide read-write separation function.Supporting one master DB and one slave DB. #### 软件架构 1. 使用jdk1.7 2. 依赖spring-boot-starter-parent(1.5.4.RELEASE)/spring-boot-starter-aop/mybatis-spring-boot-starter(1.3.1) #### 部署说明 1. 直接进行jar包依赖即可 com.gitee.sunchenbin springboot-mybatis-wr-separation 0.0.4.RELEASE 2. application.properties中需要做如下配置 (1) mybatis的配置(根据自己的配置来) mybatis.mapperLocations=classpath:sql-mapper/**/*.xml mybatis.typeAliasesPackage=com.xxx.api.xxx.model,com.xxx.api.xxx.command (2) 读库和写库的配置 spring.datasource.write.url=jdbc:postgresql://127.0.0.1/db-master spring.datasource.write.username=root spring.datasource.write.password=123456 spring.datasource.write.driver-class-name=org.postgresql.Driver spring.datasource.read.url=jdbc:postgresql://127.0.0.1/db-slave spring.datasource.read.username=root spring.datasource.read.password=123456 spring.datasource.read.driver-class-name=org.postgresql.Driver (3) 读写分离切点的expression表达式(建议切manager或者service) wr.separation.pointcut.expression=execution(public * com.xxx..*.*(..)) 3. 应用插件的springboot项目需要配置扫码插件的包,即@ComponentScan需要配置对"com.gitee.sunshine.*"包路径的扫描 #### 使用说明 1. 本插件提供一个注解,@ReadDB(走读库),如果不打注解默认走写库 2. 注解适用范围,必须是spring管理的bean的方法,注解不支持打在接口方法上,通常来说建议在manager层service层的方法上使用 3. 注解生命周期,为了避免读库写库来回切换,产生的事务问题,这里规定,当前线程只会使用一个库去执行操作,比如a.test1()被调用时,首先a是被spring管理的类,并且他在我们配置文件“读写分离切点表达式”的范围内,那么此时,如果test1()打了注解@ReadDB,那么从此开始直到这个线程结束了,都使用读库,如果没有打注解,那么从此开始知道整个线程结束了都使用写库。 4. 没有被切到的方法如果使用了数据库操作,默认走写库