网站建设的业务规划,廊坊seo排名外包,基于云服务器的网站开发,上海关键词seospringmore-core springibatis实现读写分离 特点 无缝结合springibatis#xff0c;对于程序员来说#xff0c;是透明的 除了修改配置信息之外#xff0c;程序的代码不需要修改任何东西 支持spring的容器事务规则:基于spring配置的容器事务读写事务到主库只读事务到从库如果没…springmore-core springibatis实现读写分离 特点 无缝结合springibatis对于程序员来说是透明的 除了修改配置信息之外程序的代码不需要修改任何东西 支持spring的容器事务规则:基于spring配置的容器事务读写事务到主库只读事务到从库如果没有配置事务更新语句全部到主库查询语句均衡到从库 实现源码https://gitcafe.com/tangyanbo/springmore 下载spring-core即可快速入门 maven依赖dependencygroupIdorg.springmore/groupIdartifactIdspringmore-core/artifactIdversion1.0.0/version
/dependency dataSource配置(applicationContext.xml中) ?xml version1.0 encodingutf-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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 idmaster classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClassvaluecom.mysql.jdbc.Driver/value/propertyproperty namejdbcUrlvaluejdbc:mysql://192.168.1.246:3306/db1/value/propertyproperty nameuservalueysb/value/propertyproperty namepasswordvalueysb/value/propertyproperty nameinitialPoolSizevalue20/value/propertyproperty nameminPoolSizevalue20/value/propertyproperty namemaxPoolSizevalue200/value/propertyproperty namemaxIdleTimevalue255000/value/property/beanbean iddataSource2 classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClassvaluecom.mysql.jdbc.Driver/value/propertyproperty namejdbcUrlvaluejdbc:mysql://192.168.1.246:3306/db2/value/propertyproperty nameuservalueysb/value/propertyproperty namepasswordvalueysb/value/propertyproperty nameinitialPoolSizevalue20/value/propertyproperty nameminPoolSizevalue20/value/propertyproperty namemaxPoolSizevalue200/value/propertyproperty namemaxIdleTimevalue255000/value/property/beanbean iddataSource3 classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClassvaluecom.mysql.jdbc.Driver/value/propertyproperty namejdbcUrlvaluejdbc:mysql://192.168.1.246:3306/db3/value/propertyproperty nameuservalueysb/value/propertyproperty namepasswordvalueysb/value/propertyproperty nameinitialPoolSizevalue20/value/propertyproperty nameminPoolSizevalue20/value/propertyproperty namemaxPoolSizevalue200/value/propertyproperty namemaxIdleTimevalue255000/value/property/beanbean iddataSource classorg.springmore.core.datasource.DynamicDataSourceproperty namemaster refmaster / property nameslaveslistref beandataSource2/ref beandataSource3//list /property/bean
/beans 整合mybatis配置(applicationContext.xml中) !-- ibatis3 工厂类 --bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource /property nameconfigLocation valueclasspath:sqlMapConfig.xml //beanbean idsqlSessionTemplate classorg.mybatis.spring.SqlSessionTemplateconstructor-arg index0 refsqlSessionFactory //beanbean iddynamicSqlSessionTemplate classorg.springmore.core.datasource.DynamicSqlSessionTemplateconstructor-arg index0 refsqlSessionTemplate //bean 事务配置(applicationContext.xml中) !-- 定义单个jdbc数据源的事务管理器 --bean idtransactionManagerclassorg.springmore.core.datasource.DynamicDataSourceTransactionManagerproperty namedataSource refdataSource //bean!-- 以 Transactional 标注来定义事务 --tx:annotation-driven transaction-managertransactionManagerproxy-target-classtrue / !-- 配置事务的传播特性 --tx:advice idtxAdvice transaction-managertransactionManagertx:attributestx:method nameinsert* propagationREQUIRED read-onlyfalserollback-forException /tx:method namedelete* propagationREQUIRED read-onlyfalserollback-forException /tx:method nameupdate* propagationREQUIRED read-onlyfalserollback-forException /tx:method nameproc* propagationREQUIRED read-onlyfalserollback-forException /tx:method nameselect* read-onlytrue /tx:method name* read-onlyfalse /!-- tx:method name* read-onlytrue / --/tx:attributes/tx:advice!-- 那些类的哪些方法参与事务 --aop:configaop:pointcut idallManagerMethod expressionexecution(* org.springmore.core.dao..*(..)) /aop:advisor pointcut-refallManagerMethod advice-reftxAdvice //aop:config dao代码示例 Repository(UserMapperImpl)
public class UserMapperImpl implements UserMapper{Autowiredprivate DynamicSqlSessionTemplate sqlSessionTemplate;//从库public ListUser 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