详解MyBatis多数据源配置(读写分离)

网友投稿 199 2023-06-20


详解MyBatis多数据源配置(读写分离)

MyBatis多数据源配置(读写分离)

首先说明,本文的配置使用的最直接的方式,实际用起来可能会很麻烦。

实际应用中可能存在多种结合的情况,你可以理解本文的含义,不要死板的使用。

多数据源的可能情况

1.主从

通常是mysql一主多从的情况,本文的例子就是主从的情况,但是只有两个数据源,所以采用直接配置不会太麻烦,但是不利于后续扩展,主要是作为一个例子来说明,实际操作请慎重考虑。

2.分库

当业务独立性强,数据量大的时候的,为了提高并发,可能会对表进行分库,分库后,每一个数据库都需要配置一个数据源。

这种情况可以参考本文,但是需要注意每一个数据库对应的Mapper要在不同的包下方便区分和配置。

另外分库的情况下也会存在主从的情况,如果你的数据库从库过多,就参考上面提供的方法,或者寻找其他方式解决。

Mapper分包

分库的情况下,不同的数据库的Mapper一定放在不同的包下。

主从的情况下,同一个Mapper会同时存在读写的情况,创建两个并不合适,使用同一个即可。但是这种情况下需要注意,Spring对Mapper自动生成的名字是相同的,而且类型也相同,这是就不能直接注入Mapper接口。需要通过SqlSession来解决。

Spring基础配置

applicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

这个文件,主要是引入了spring-datasource-master.xml和spring-datasource-slave.xml。

spring-datasource-master.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

init-method="init" destroy-method="close">

</bean>

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

classpath:mapper/*.xml

expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

init-method="init" destroy-method="close">

</bean>

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

classpath:mapper/*.xml

init-method="init" destroy-method="close">

</bean>

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

classpath:mapper/*.xml

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

classpath:mapper/*.xml

expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

spring-datasource-slave.xml

和master区别不大,主要是id名字和数据源配置有区别。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

init-method="init" destroy-method="close">

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

classpath:mapper/*.xml

expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

init-method="init" destroy-method="close">

init-method="init" destroy-method="close">

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

classpath:mapper/*.xml

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

classpath:mapper/*.xml

expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

这里需要注意是只读的。如果不是从库,可以按主库进行配置。

在下面代码中:

必须通过sqlSessionFactoryBeanName来指定不同的sqlSessionFactory。

config.properties

# 数据库配置 - Master

master.jdbc.driverClass = com.mysql.jdbc.Driver

master.jdbc.url = jdbc:mysql://192.168.1.11:3306/test

master.jdbc.user = root

master.jdbc.password = jj

# - Slave

slave.jdbc.driverClass = com.mysql.jdbc.Driver

slave.jdbc.url = jdbc:mysql://192.168.1.22:3306/test

slave.jdbc.user = root

slave.jdbc.password = jj

使用Mapper

这里是针对主从的情况进行设置的,两个配置扫描的Mapper是一样的,所以没法直接注入,需要通过下面的麻烦方式注入。

@Service

public class DemoService {

private CountryMapper writeMapper;

private CountryMapper readMapper;

@Resource(name = "sqlSessionMaster")

public void setWriteMapper(SqlSession sqlSession) {

this.writeMapper = sqlSession.getMapper(CountryMapper.class);

}

@Resource(name = "sqlSessionSlave")

public void setReadMapper(SqlSession sqlSession) {

this.readMapper = sqlSession.getMapper(CountryMapper.class);

}

public int save(Country countryhcTEZkBB){

return writeMapper.insert(country);

}

public List selectPage(int pageNum, int pageSize) {

PageHelper.startPage(pageNum, pageSize);

return readMapper.select(null);

}

}

因为sqlSession能通过name区分开,所以这里从sqlSession获取Mapper。

另外如果需要考虑在同一个事务中写读的时候,需要使用相同的writeMapper,这样在读的时候,才能获取事务中的最新数据。

以上是主从的情况。

在分库的情况时,由于不同Mapper在不同的包下,所以可以直接使用@Resource或者@Autowired注入Mapper,不需要通过sqlSession获取。

本篇文章,只是一个多数据源的参考,实际应用时,请根据自己的情况进行考虑。

后续,我会利用业余时间,在本文和上面两个相关链接的基础上,针对MySql多数据源,尝试开发可以自动切换数据源的插件,因为我对这方面的实际应用不是很熟,所以欢迎大家留言分享自己的解决方案,对这些了解的越多,就越有可能开发出通用的数据源切换插件。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JDK安装配置教程
下一篇:详解spring mvc对异步请求的处理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~