spring 读写mysql数据库_mysql+spring+mybatis实现数据库读写分离[代码配置] .

场景:一个读数据源一个读写数据源。

原理:借助spring的【org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource】这个抽象类实现,看名字可以了解到是一个路由数据源的东西,这个类中有一个方法

/**

* Determine the current lookup key. This will typically be

* implemented to check a thread-bound transaction context.

*

Allows for arbitrary keys. The returned key needs

* to match the stored lookup key type, as resolved by the

* {@link #resolveSpecifiedLookupKey} method.

*/

protected abstract Object determineCurrentLookupKey();

每次去连数据库的时候,spring会调用这个方法去找对应的数据源。返回值即对应的数据源的LookUpKey.那么这个LookUpKey在哪定义的呢?看下面的dataBase.xml的配置

user=${jdbc.username},password=${jdbc.password}

user=${jdbc.r.username},password=${jdbc.r.password}

class="org.mybatis.spring.SqlSessionFactoryBean">

动态数据源dynamicDataSource中的dataSourceKeyRW、dataSourceKeyR就是

protected abstract Object determineCurrentLookupKey();

这个方法要返回的值。那么如何设置,让这个方法的返回值是根据我们的需要返回dataSourceKeyRW、dataSourceKeyR呢?由于这个方法没有入参,并且是spring自动调用的,因此考虑使用静态变量存储dataSource的key,在调用sql语句前设置静态变量的值,然后在这个方法中得到静态变量的值,返回。又考虑到多线程,同时可能会有很多请求,为避免线程之间相互干扰,考虑使用threadLocal。

先看存储dataSourceKey的容器类。

public class DBContextHolder {

/**

* 线程threadlocal

*/

private static ThreadLocal contextHolder = new ThreadLocal<>();

private String DB_TYPE_RW = "dataSourceKeyRW";

private String DB_TYPE_R = "dataSourceKeyR";

public String getDbType() {

String db = contextHolder.get();

if (db == null) {

db = DB_TYPE_RW;// 默认是读写库

}

return db;

}

/**

*

* 设置本线程的dbtype

*

* @param str

* @see [相关类/方法](可选)

* @since [产品/模块版本](可选)

*/

public void setDbType(String str) {

contextHolder.set(str);

}

/**

* clearDBType

*

* @Title: clearDBType

* @Description: 清理连接类型

*/

public static void clearDBType() {

contextHolder.remove();

}

}

动态数据源的实现类。

public class DynamicDataSource extends AbstractRoutingDataSource {

/*

* (non-Javadoc)

* @see javax.sql.CommonDataSource#getParentLogger()

*/

@Override

public Logger getParentLogger() throws SQLFeatureNotSupportedException {

// TODO Auto-generated method stub

return null;

}

/**

*

* override determineCurrentLookupKey

*

* Title: determineCurrentLookupKey

*

*

* Description: 自动查找datasource

*

*

* @return

*/

@Override

protected Object determineCurrentLookupKey() {

return DBContextHolder.getDbType();

}

}

在DAO层中设置数据库类型。

/**

* 添加邮件

*

* @param sms

* @return

*/

public boolean insertEmail(Email email) {

//根据具体需要设置不同的数据库

DBContextHolder.setDbType(DBContextHolder.DB_TYPE_RW);

//DBContextHolder.setDbType(DBContextHolder.DB_TYPE_R);

int result = this.getSqlSession().insert(STATEMENT + ".addEntity",

email);

return result == 1;

}

在本例中,我们是在DAO中指定数据库,我们也可以根据需要在service或者controller中指定DB类型,需要记住的是setDbType是针对线程维度的。要考虑多线程的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值