日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring+mybatis实现读写分离

發布時間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring+mybatis实现读写分离 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

springmore-core

spring+ibatis實現讀寫分離

  • 特點
    無縫結合spring+ibatis,對于程序員來說,是透明的
    除了修改配置信息之外,程序的代碼不需要修改任何東西
    支持spring的容器事務

  • 規則:
  • 基于spring配置的容器事務
  • 讀寫事務到主庫
  • 只讀事務到從庫
  • 如果沒有配置事務,更新語句全部到主庫,查詢語句均衡到從庫
    實現源碼:https://gitcafe.com/tangyanbo/springmore
    下載spring-core即可
    • 快速入門
      maven依賴
    <dependency><groupId>org.springmore</groupId><artifactId>springmore-core</artifactId><version>1.0.0</version> </dependency>

    dataSource配置(applicationContext.xml中)

    <?xml version="1.0" encoding="utf-8"?> <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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- C3P0連接池配置 --><bean id="master" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass"><value>com.mysql.jdbc.Driver</value></property><property name="jdbcUrl"><value>jdbc:mysql://192.168.1.246:3306/db1</value></property><property name="user"><value>ysb</value></property><property name="password"><value>ysb</value></property><property name="initialPoolSize"><value>20</value></property><property name="minPoolSize"><value>20</value></property><property name="maxPoolSize"><value>200</value></property><property name="maxIdleTime"><value>255000</value></property></bean><bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass"><value>com.mysql.jdbc.Driver</value></property><property name="jdbcUrl"><value>jdbc:mysql://192.168.1.246:3306/db2</value></property><property name="user"><value>ysb</value></property><property name="password"><value>ysb</value></property><property name="initialPoolSize"><value>20</value></property><property name="minPoolSize"><value>20</value></property><property name="maxPoolSize"><value>200</value></property><property name="maxIdleTime"><value>255000</value></property></bean><bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass"><value>com.mysql.jdbc.Driver</value></property><property name="jdbcUrl"><value>jdbc:mysql://192.168.1.246:3306/db3</value></property><property name="user"><value>ysb</value></property><property name="password"><value>ysb</value></property><property name="initialPoolSize"><value>20</value></property><property name="minPoolSize"><value>20</value></property><property name="maxPoolSize"><value>200</value></property><property name="maxIdleTime"><value>255000</value></property></bean><bean id="dataSource" class="org.springmore.core.datasource.DynamicDataSource"><property name="master" ref="master" /> <property name="slaves"><list><ref bean="dataSource2"/><ref bean="dataSource3"/></list> </property></bean> </beans>

    整合mybatis配置(applicationContext.xml中)

    <!-- ibatis3 工廠類 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:sqlMapConfig.xml" /></bean><bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactory" /></bean><bean id="dynamicSqlSessionTemplate" class="org.springmore.core.datasource.DynamicSqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionTemplate" /></bean>

    事務配置(applicationContext.xml中)

    <!-- 定義單個jdbc數據源的事務管理器 --><bean id="transactionManager"class="org.springmore.core.datasource.DynamicDataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 以 @Transactional 標注來定義事務 --><tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="true" /> <!-- 配置事務的傳播特性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="insert*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="delete*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="update*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="proc*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="select*" read-only="true" /><tx:method name="*" read-only="false" /><!-- <tx:method name="*" read-only="true" /> --></tx:attributes></tx:advice><!-- 那些類的哪些方法參與事務 --><aop:config><aop:pointcut id="allManagerMethod" expression="execution(* org.springmore.core.dao..*(..))" /><aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /></aop:config>

    dao代碼示例:

    @Repository("UserMapperImpl") public class UserMapperImpl implements UserMapper{@Autowiredprivate DynamicSqlSessionTemplate sqlSessionTemplate;//從庫public List<User> selectByUserNameAndPwd(User user) {return sqlSessionTemplate.selectList("selectByUserNameAndPwd", user);}//主庫public void insert(User user) { sqlSessionTemplate.insert("insert", user); } }

    轉載于:https://www.cnblogs.com/tangyanbo/p/4601220.html

    總結

    以上是生活随笔為你收集整理的spring+mybatis实现读写分离的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。