MyBatis Multi-Dataソース構成(読み取りワイト分離)
まず、この記事で構成を使用する最も直接的な方法は、実際に使用するのが非常に面倒な場合があります。
実際のアプリケーションには複数の組み合わせがある場合があります。この記事の意味を理解することができ、それを厳密に使用しないでください。
複数のデータソースを持つ可能な状況
1。マスターとスレーブ
通常、MySQLには1人のマスターと複数の奴隷がいる場合があります。この記事の例は、マスターと奴隷がそうである場合ですが、データソースは2つしかないため、直接構成を使用するのは面倒ではありませんが、その後の拡張を助長しません。これは主に説明する例として使用されます。実際の操作で慎重に検討してください。
2。ライブラリを分割します
ビジネスの独立性が強く、データ量が大きい場合、同時性を改善するために、テーブルが分割される場合があります。データベースが分割された後、各データベースはデータソースを構成する必要があります。
この場合、この記事を参照できますが、各データベースに対応するマッパーは、異なるパッケージの下で便利に区別して構成する必要があることに注意する必要があります。
さらに、データベースパーティションがある場合、マスタースレーブケースがあります。データベーススレーブライブラリが多すぎる場合は、上記の方法を参照するか、問題を解決する他の方法を見つけてください。
マッパーサブコントラクト
パーティション化の場合、異なるデータベースのマッパーは異なるパッケージの下に配置する必要があります。
マスターとスレーブの場合、同じマッパーが同時に読み、書かれます。 2つを作成するのは適切ではありません。同じものを使用するだけです。ただし、この場合、スプリングはマッパーの名前を自動的に生成し、タイプは同じであるため、マッパーインターフェイスに直接注入することはできないことに注意する必要があります。 sqlsessionを通じて解決する必要があります。
スプリングの基本構成
ApplicationContext.xml
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" " xmlns:Context = "http://www.springframework.org/schema/context" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemalocation = "http://ww.springframework http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd base-package = "com.isea533.mybatis.service"/> <コンテキスト:プロパティプレイスホルダーlocation = "classpath:config.properties"/> <aop:aspectj-autoproxy/> <import resource = "spring-datasource-master.xml"/> <import resource = "spring-datasource-soource-slave.xml"/>
このファイルは、主にSpring-Datasource-Master.xmlおよびSpring-Datasource-Slave.xmlを導入しています。
Spring-datasource-master.xml
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" " xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.x.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/aop/spring-aop.xxsd ""> bean Destrow-Method = "close"> <Property name = "driverclassname" value = "$ {master.jdbc.driverclass}"/> <プロパティ名= "url" value = "$ {master.jdbc.url}"/> <プロパティ名= "username" value = "$ {master.jdbc.user} value = "$ {master.jdbc.password}"/> <プロパティ名= "フィルター=" value = "stat"/> <プロパティ名= "maxactive" value = "20"/> <プロパティ名= "イニシャルサイズ"値= "1"/> <プロパティ名= "maxwait" value = "60000"/> name = "timevevictionrunsmillis" value = "60000"/> <プロパティ名= "value =" 300000 "/> <プロパティ名=" validationquery "value =" select 'x' '"/> <プロパティ名=" testwhileidle "value =" "true"/> <propertion name = "" testonborrow = "" "" "" balle = "" "" falled ""/>値= "false"/> </bean> <bean id = "sqlsessionfactory1"> <プロパティ名= "datasource" ref = "datasourcemaster"/> <プロパティ名= "mapperlocations"> <array> <balue> classpath:mapper/*。xml </value> </array> < value = "com.isea533.mybatis.mapper"/> <プロパティ名= "sqlsessionfactorybeanname" value = "sqlsessionfactory1"/> </bean> <bean id = "sqlsessionmaster" scope = "prototype"> <construction-arg index = " <aop:pointcut id = "appservice" expression = "execution(*com.isea533.mybatis.service ..*service* Transaction-Manager = "TransactionManager1"> <tx:aTtributes> <tx:method name = "select*" read- nolly = "true"/> <tx:method name = "find*" read- only = "true"/> <tx:method name = "get*" read-only = "true"/>> <tx </tx:属性> </tx:アドバイス> <bean id = "transactionmanager1"> <プロパティ名= "datasource" ref = "datasourcemaster"/> </bean> </beans>Spring-datasource-slave.xml
主にID名とデータソース構成の間に違いがあるため、それとマスターにはほとんど違いはありません。
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" " xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.x.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/aop/spring-aop.xxsd ""> <beanid ""> bean Destrow-method = "close"> <Property name = "driverclassname" value = "$ {slave.jdbc.driverclass}"/> <プロパティ名= "url" value = "$ {slave.jdbc.url}"/> <プロパティname = "username" value = "$ {slave.jdbc.user}"/< value = "$ {slave.jdbc.password}"/> <プロパティ名= "フィルター=" value = "stat"/> <プロパティ名= "maxactive" value = "20"/> <プロパティ名= "イニシャルサイズ"値= "1"/> <プロパティ名= "maxwait" value = "60000"/> name = "timevevictionrunsmillis" value = "60000"/> <プロパティ名= "value =" 300000 "/> <プロパティ名=" validationquery "value =" select 'x' '"/> <プロパティ名=" testwhileidle "value =" "true"/> <propertion name = "" testonborrow = "" "" "" balle = "" "" falled ""/> value = "false"/> </bean> <bean id = "sqlsessionfactory2"> <プロパティ名= "dataSource" ref = "dataSourceslave"/> <プロパティ名= "mapperlocations"> <array> <balue> classpath:mapper/*。xml </value> </array> < value = "com.isea533.mybatis.mapper"/> <プロパティ名= "sqlsessionfactorybeanname" value = "sqlsessionfactory2"/> </bean> <bean id = "sqlsessionslave" scope = "prototype"> <construction-arg index = " <aop:pointcut id = "appservice" expression = "execution(*com.isea533.mybatis.service ..*service* Transaction-Manager = "TransactionManager2"> <tx:aTtributes> <tx:method name = "*" read-only = "true"/>> </tx:attributes> </tx:advise> <bean id = "transactionmanager2"> <プロパティname = "datasource" ref = "datasourcelase"/> </bean> </bean> </bean> </beanここでは、<tx:method name = "*" read-only = "true"/>は読み取り専用です。奴隷ライブラリでない場合は、メインライブラリに従って構成できます。
次のコードで:
<bean> <プロパティ名= "basepackage" value = "com.isea533.mybatis.mapper"/> <プロパティ名= "sqlsessionfactorybeanname" value = "sqlsessionfactory2"/> </bean>
SQLSessionFactoryBeanNameを通じて、異なるsqlSessionFactoryを指定する必要があります。
config.properties
#データベース構成-MasterMaster.jdbc.driverclass = com.mysql.jdbc.drivermaster.jdbc.url = jdbc:mysql://192.168.1.11:3306/testmaster.jdbc.user = rootmaster.jdbc.password djdbc.password = jjdbc.password = com.mysql.jdbc.driverslave.jdbc.url = jdbc:mysql://192.168.1.22:3306/testslave.jdbc.user = rootlave.jdbc.password = jj
マッパーを使用します
これは、マスターと奴隷の状況に設定されています。 2つの構成スキャンマッパーは同じであるため、直接注入することはできないため、以下の厄介な方法で注入する必要があります。
@servicepublic 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 country){return writemapper.insert(country); } public List <Country> SelectPage(int pagenum、int pagesize){pagehelper.startpage(pagenum、pagesize); ReadMapper.Select(null)を返します。 }}sqlsessionは名前で区別できるため、sqlsessionからマッパーを取得します。
さらに、同じトランザクションで書くことと読み取りを検討する必要がある場合は、同じWritemapperを使用して、トランザクションの最新データを読み取り時に取得できるようにする必要があります。
上記は、マスターと奴隷の状況です。
ライブラリパーティションの場合、異なるマッパーは異なるパッケージの下にあるため、@Resourceまたは@Autowiredを使用してマッパーを直接注入できます。
この記事は、複数のデータソースの参照にすぎません。実際にそれを適用するとき、あなた自身の状況に従ってそれを考慮してください。
将来的には、この記事と上記の2つの関連するリンクをMySQL複数のデータソースに自動的に切り替えることができるプラグインを開発しようとするために、暇な時間を使用します。私はこの分野の実際のアプリケーションにあまり慣れていないので、私は皆が自分のソリューションを共有するためにメッセージを残すことを歓迎します。これらについて知っているほど、一般的なデータソーススイッチングプラグインを開発する可能性が高くなります。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。