本文介绍了在MyBatis上使用MySQL复制(主/从)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何在MyBatis中使用主/从MySQL复制数据库. JDBC提供了com.mysql.jdbc.ReplicationDriver(请参见此处),但我找不到在哪里可以使用类似的东西,包括可以配置的所有不错的属性( roundRobinLoadBalance autoReconnect ,...)在MyBatis中.

I am just wondering how I can use a Master/Slave MySQL replication database with MyBatis. JDBC offers a com.mysql.jdbc.ReplicationDriver (see here), but I couldn't find out where I can use similar things including all the nice properties I can configure (roundRobinLoadBalance, autoReconnect,...) in MyBatis.

目前,我已经在MyBatis中配置了非复制数据库,如下所示:

Currently I have configured my none-replicated database in MyBatis like this:

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://localhost:3306/database" />
            <property name="username" value="root" />
            <property name="password" value="" />
        </dataSource>
    </environment>
    <environment id="production">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://xxx:3306/database" />
            <property name="username" value="production" />
            <property name="password" value="" />
        </dataSource>
    </environment>
</environments>

有人对我有提示吗?

感谢您的帮助.

丹尼尔

推荐答案

另一个可能的答案

如果您注意到,您在xml中为驱动程序设置的属性也是设置并传递给jdbc的通用属性.因此,如果MyBatis仅将它们带入并传递给驾驶员,我不会感到惊讶.所以也许试试看:

If you notice, the properties you're setting in the xml for the driver are also common properties set and passed to jdbc. So, I wouldn't be surprised if MyBatis was just taking them and passing them right into the driver. So maybe try this:

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <!-- Just use ReplicationDriver -->
            <property name="driver" value="com.mysql.jdbc.ReplicationDriver" />
            <property name="url"
                value="jdbc:mysql://localhost:3306/database" />
            <property name="autoReconnect" value="true" />
            <property name="roundRobinLoadBalance" value="true" />
            <property name="username" value="root" />
            <property name="password" value="" />
        </dataSource>
    </environment>
    <environment id="production">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <!-- Just use ReplicationDriver -->
            <property name="driver" value="com.mysql.jdbc.ReplicationDriver" />
            <property name="url"
                value="jdbc:mysql://xxx:3306/database" />
            <property name="autoReconnect" value="true" />
            <property name="roundRobinLoadBalance" value="true" />
            <property name="username" value="production" />
            <property name="password" value="" />
        </dataSource>
    </environment>
</environments>

这篇关于在MyBatis上使用MySQL复制(主/从)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:26