SpringBoot+Mybatis-Plus实现mysql读写分离方案的示例代码
程序员文章站
2022-03-22 14:24:15
1. 引入mybatis-plus相关包,pom.xml文件2. 配置文件application.property增加多库配置mysql 数据源配置spring.datasource.primary....
1. 引入mybatis-plus相关包,pom.xml文件
2. 配置文件application.property增加多库配置
mysql 数据源配置
spring.datasource.primary.jdbc-url=jdbc:mysql://xx.xx.xx.xx:3306/portal?useunicode=true&usejdbccomplianttimezoneshift=true&uselegacydatetimecode=false&servertimezone=utc&characterencoding=utf8&servertimezone=gmt%2b8 spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.driver #mysql slave 数据源配置 spring.datasource.slave.jdbc-url=jdbc:mysql://xx.xx.xx.xx:3306/portal?useunicode=true&usejdbccomplianttimezoneshift=true&uselegacydatetimecode=false&servertimezone=utc&characterencoding=utf8&servertimezone=gmt%2b8 spring.datasource.slave.username=root spring.datasource.slave.password=root spring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.driver
3. 配置数据源及注解
数据源配置 multidatasourceconfig.java
/** * 配置多数据源 */ @profile("dev")//开发模式配置文件 @configuration @mapperscan(basepackages = "com.csc.portal.mapper")//扫描包 public class multidatasourceconfig { /** * 主数据源 * @return */ @bean @configurationproperties(prefix = "spring.datasource.primary") public datasource masterdatasource() { return datasourcebuilder.create().build(); } /** * 从数据源 * @return */ @bean @configurationproperties(prefix = "spring.datasource.slave") public datasource slavedatasource() { return datasourcebuilder.create().build(); } /** * 路由数据源,前面两个数据源是为了创建此数据源 * @param masterdatasource 主数据源 * @param slavedatasource 从数据源 * @return */ @bean public datasource myroutingdatasource(@qualifier("masterdatasource") datasource masterdatasource, @qualifier("slavedatasource") datasource slavedatasource) { map<object, object> targetdatasources = new hashmap<>(); targetdatasources.put(dbtypeenum.master, masterdatasource); targetdatasources.put(dbtypeenum.slave, slavedatasource); myroutingdatasource myroutingdatasource = new myroutingdatasource(); myroutingdatasource.setdefaulttargetdatasource(slavedatasource);//设置默认数据源 myroutingdatasource.settargetdatasources(targetdatasources);//设置路由表,使用map的key,value方式得到对应数据源 return myroutingdatasource; }
数据库枚举类
public enum dbtypeenum { master, slave; }
注解
@target({elementtype.method}) @retention(retentionpolicy.runtime) public @interface master { }
@target({elementtype.method}) @retention(retentionpolicy.runtime) public @interface slave { }
4. mybatis-plus配置
@enabletransactionmanagement @configuration @mapperscan(basepackages = "com.csc.portal.mapper") public class mybatisplusconfig { /** * 分页插件 */ @bean public paginationinterceptor paginationinterceptor() { return new paginationinterceptor(); } @resource(name = "myroutingdatasource") private datasource myroutingdatasource; /** * 使用mybatis plus的sqlsessionfactory代替, * 此处注意mybatis与mybatisplus的配置不同,不然扫描不到对数据操作的方法。会报未绑定错误 * @return sqlsessionfactory * @throws exception */ @bean public sqlsessionfactory sqlsessionfactory() throws exception { mybatissqlsessionfactorybean sqlsessionfactorybean = new mybatissqlsessionfactorybean(); sqlsessionfactorybean.setdatasource(myroutingdatasource); sqlsessionfactorybean.setmapperlocations(new pathmatchingresourcepatternresolver().getresources("classpath:mapper/*.xml")); mybatisconfiguration mybatisconfiguration = new mybatisconfiguration(); sqlsessionfactorybean.setconfiguration(mybatisconfiguration); return sqlsessionfactorybean.getobject(); } /** * 此处为使用mybatis时的sqlsessionfactory配置 * @return * @throws exception */ /* @bean public sqlsessionfactory sqlsessionfactory() throws exception { sqlsessionfactorybean sqlsessionfactorybean = new sqlsessionfactorybean(); sqlsessionfactorybean.setdatasource(myroutingdatasource); sqlsessionfactorybean.setmapperlocations(new pathmatchingresourcepatternresolver().getresources("classpath:mapper/*.xml")); return sqlsessionfactorybean.getobject(); } */ /** * 事务配置 * @return 事务管理器 */ @bean public datasourcetransactionmanager transactionmanager() { datasourcetransactionmanager tx = new datasourcetransactionmanager(); tx.setdatasource(myroutingdatasource); return tx; }
5. 增加数据源管理类
dbcontextholder.java
public class dbcontextholder { /** * 外部一个请求将会产生一个线程与之对应,每个线程的变量可用threadlocal进行存储 */ private static final threadlocal<dbtypeenum> contextholder = new threadlocal<>(); public static void set(dbtypeenum dbtype) { contextholder.set(dbtype); } public static dbtypeenum get() { return contextholder.get(); } public static void master() { set(dbtypeenum.master); system.out.println("切换到master"); } public static void slave() { set(dbtypeenum.slave); system.out.println("切换到slave"); } }
指定选择数据源
myroutingdatasource.java 方法determinecurrentlookupkey决定最终使用哪个数据源进行操作,若为空则使用默认数据源。
public class myroutingdatasource extends abstractroutingdatasource { @nullable @override protected object determinecurrentlookupkey() { system.out.println("线程名:"+thread.currentthread().getname()+":"+dbcontextholder.get()); return dbcontextholder.get(); /* if (dbcontextholder.get() != null) { system.out.println("线程名:"+thread.currentthread().getname()+":"+dbcontextholder.get()); return dbcontextholder.get(); } else { system.out.println("未匹配到指定数据库,默认切换到master"); return dbtypeenum.master; }*/ //return dbcontextholder.get(); } }
6. 增加aop切面
@aspect @component @order(0)//配置注解优先级,优于事物注解@transactional先进行数据源切换, //不然在事物中进行数据源切换无效 public class datasourceaop { @pointcut(/*"!@annotation(com.csc.portal.annotation.master) " + "&& (execution(* com.csc.portal.service..*.select*(..)) " + "|| execution(* com.csc.portal.service..*.get*(..))"+*/ " @annotation(com.csc.portal.annotation.slave)") public void readpointcut() { } @pointcut("@annotation(com.csc.portal.annotation.master) " //+ /* "|| execution(* com.csc.portal.service..*.insert*(..)) " + "|| execution(* com.csc.portal.service..*.add*(..)) " + "|| execution(* com.csc.portal.service..*.update*(..)) " + "|| execution(* com.csc.portal.service..*.edit*(..)) " + "|| execution(* com.csc.portal.service..*.delete*(..)) " + "|| execution(* com.csc.portal.service..*.remove*(..))"*/) public void writepointcut() { } @before("readpointcut()") public void read() { //获取拦截类 dbcontextholder.slave(); system.out.println(thread.currentthread().getname()+dbcontextholder.get()); } @before("writepointcut()") public void write() { //获取拦截类 /* string classname = pjp.gettarget().getclass().getname(); system.out.println("当前线程"+thread.currentthread().getname()+" 拦截类为:" + classname); //获取拦截的方法名 methodsignature msig = (methodsignature) pjp.getsignature(); method currentmethod = null; try { currentmethod = pjp.gettarget().getclass().getmethod(msig.getname(), msig.getparametertypes()); } catch (nosuchmethodexception e) { e.printstacktrace(); } string methodname = currentmethod.getname(); system.out.println("拦截方法名为:" + methodname);*/ dbcontextholder.master(); system.out.println(thread.currentthread().getname()+dbcontextholder.get()); } }
6. 实际应用
- 在service层方法前增加注解@master表示使用主库,进行增删改的操作使用主库。
- 在service层方法前增加注解@slave表示使用从库,进行查的操作使用从库,默认使用从库,可不配置。
- @ transactional注解加到service层,增加了@transactional注解后,启用事务后,一个事务内部的connection是复用的,所以就算aop切了数据源字符串,但是数据源并不会被真正修改。所以@transactional注解不要写在controller层,不然在service层也切换不了数据源。
- @transactional与@master可同时使用,已经配置@master注解的优先级较高,先切换数据源后执行事务。
到此这篇关于springboot+mybatis-plus实现mysql读写分离方案的示例代码的文章就介绍到这了,更多相关springboot mybatis-plus mysql读写分离内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 《浅入浅出》-RocketMQ