mysql+spring+mybatis实现数据库读写分离的代码配置
场景:一个读数据源一个读写数据源。
原理:借助spring的【org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource】这个抽象类实现,看名字可以了解到是一个路由数据源的东西,这个类中有一个方法
/** *Determinethecurrentlookupkey.Thiswilltypicallybe *implementedtocheckathread-boundtransactioncontext. *Allowsforarbitrarykeys.Thereturnedkeyneeds *tomatchthestoredlookupkeytype,asresolvedbythe *{@link#resolveSpecifiedLookupKey}method. */ protectedabstractObjectdetermineCurrentLookupKey();
每次去连数据库的时候,spring会调用这个方法去找对应的数据源。返回值即对应的数据源的LookUpKey.那么这个LookUpKey在哪定义的呢?看下面的dataBase.xml的配置
user=${jdbc.username},password=${jdbc.password} user=${jdbc.r.username},password=${jdbc.r.password}
动态数据源dynamicDataSource中的dataSourceKeyRW、dataSourceKeyR就是
protectedabstractObjectdetermineCurrentLookupKey();
这个方法要返回的值。那么如何设置,让这个方法的返回值是根据我们的需要返回dataSourceKeyRW、dataSourceKeyR呢?由于这个方法没有入参,并且是spring自动调用的,因此考虑使用静态变量存储dataSource的key,在调用sql语句前设置静态变量的值,然后在这个方法中得到静态变量的值,返回。又考虑到多线程,同时可能会有很多请求,为避免线程之间相互干扰,考虑使用threadLocal。
先看存储dataSourceKey的容器类。
publicclassDBContextHolder{
/**
*线程threadlocal
*/
privatestaticThreadLocalcontextHolder=newThreadLocal<>();
privateStringDB_TYPE_RW="dataSourceKeyRW";
privateStringDB_TYPE_R="dataSourceKeyR";
publicStringgetDbType(){
Stringdb=contextHolder.get();
if(db==null){
db=DB_TYPE_RW;//默认是读写库
}
returndb;
}
/**
*设置本线程的dbtype
*@paramstr
*@see[相关类/方法](可选)
*@since[产品/模块版本](可选)
*/
publicvoidsetDbType(Stringstr){
contextHolder.set(str);
}
/**
*clearDBType
*@Title:clearDBType
*@Description:清理连接类型
*/
publicstaticvoidclearDBType(){
contextHolder.remove();
}
}
动态数据源的实现类。
publicclassDynamicDataSourceextendsAbstractRoutingDataSource{
/*
*(non-Javadoc)
*@seejavax.sql.CommonDataSource#getParentLogger()
*/
@Override
publicLoggergetParentLogger()throwsSQLFeatureNotSupportedException{
//TODOAuto-generatedmethodstub
returnnull;
}
/**
*overridedetermineCurrentLookupKey
*
*Title:determineCurrentLookupKey
*
*
*Description:自动查找datasource
*
*@return
*/
@Override
protectedObjectdetermineCurrentLookupKey(){
returnDBContextHolder.getDbType();
}
}
在DAO层中设置数据库类型。
/**
*添加邮件
*@paramsms
*@return
*/
publicbooleaninsertEmail(Emailemail){
//根据具体需要设置不同的数据库
DBContextHolder.setDbType(DBContextHolder.DB_TYPE_RW);
//DBContextHolder.setDbType(DBContextHolder.DB_TYPE_R);
intresult=this.getSqlSession().insert(STATEMENT+".addEntity",
email);
returnresult==1;
}
在本例中,我们是在DAO中指定数据库,我们也可以根据需要在service或者controller中指定DB类型,需要记住的是setDbType是针对线程维度的。要考虑多线程的问题。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。