zoukankan      html  css  js  c++  java
  • Java读写分离实现

    1、查看源码

    AbstractRoutingDataSource类中有个determineTargetDataSource方法

     protected DataSource determineTargetDataSource() {
            Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
            Object lookupKey = this.determineCurrentLookupKey();
            DataSource dataSource = (DataSource)this.resolvedDataSources.get(lookupKey);
            if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
                dataSource = this.resolvedDefaultDataSource;
            }
    
            if (dataSource == null) {
                throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
            } else {
                return dataSource;
            }
        }
    determineTargetDataSource会调用抽象方法determineCurrentLookupKey
    @Nullable
        protected abstract Object determineCurrentLookupKey();

    2、创建类继承AbstractRoutingDataSource

     1 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
     2 
     3 public class DynamicDataSource extends AbstractRoutingDataSource{
     4 
     5     @Override
     6     protected Object determineCurrentLookupKey() {
     7         return DynamicDataSourceHolder.getDbType();
     8     }
     9 
    10 }
    DynamicDataSource
     1 import org.slf4j.Logger;
     2 import org.slf4j.LoggerFactory;
     3 
     4 public class DynamicDataSourceHolder {
     5     private static Logger logger = LoggerFactory.getLogger(DynamicDataSourceHolder.class);
     6     private static ThreadLocal<String> contextHolder = new ThreadLocal<String>();
     7     public static final String DB_MASTER = "master";
     8     public static final String DB_SLAVE = "slave";
     9 
    10     public static String getDbType() {
    11         String db = contextHolder.get();
    12         if (db == null) {
    13 
    14             db = DB_MASTER;
    15         }
    16         return db;
    17     }
    18 
    19     /**
    20      * 设置线程的dbType
    21      * 
    22      * @param str
    23      */
    24     public static void setDbType(String str) {
    25         logger.debug("所使用的数据源为:" + str);
    26         contextHolder.set(str);
    27     }
    28 
    29     /**
    30      * 清理连接类型
    31      */
    32     public static void clearDBType() {
    33         contextHolder.remove();
    34     }
    35 
    36 }
    DynamicDataSourceHolder

    3、设置Mybatis拦截器

    import java.util.Locale;
    import java.util.Properties;
    import org.apache.ibatis.executor.Executor;
    import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
    import org.apache.ibatis.mapping.BoundSql;
    import org.apache.ibatis.mapping.MappedStatement;
    import org.apache.ibatis.mapping.SqlCommandType;
    import org.apache.ibatis.plugin.Interceptor;
    import org.apache.ibatis.plugin.Intercepts;
    import org.apache.ibatis.plugin.Invocation;
    import org.apache.ibatis.plugin.Plugin;
    import org.apache.ibatis.plugin.Signature;
    import org.apache.ibatis.session.ResultHandler;
    import org.apache.ibatis.session.RowBounds;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.transaction.support.TransactionSynchronizationManager;
    
    @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
            @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
                    RowBounds.class, ResultHandler.class }) })
    public class DynamicDataSourceInterceptor implements Interceptor {
        private static Logger logger = LoggerFactory.getLogger(DynamicDataSourceInterceptor.class);
        private static final String REGEX = ".*insert\u0020.*|.*delete\u0020.*|.*update\u0020.*";
    
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            //判断当前是不是事务
            boolean synchronizationActive = TransactionSynchronizationManager.isActualTransactionActive();
            //获取crud操作的参数
            Object[] objects = invocation.getArgs();
            //获取第一个参数可以知道,具体是crud哪个操作
            MappedStatement ms = (MappedStatement) objects[0];
            String lookupKey = DynamicDataSourceHolder.DB_MASTER;
            if (synchronizationActive != true) {
                // 读方法
                if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
                    // selectKey 为自增id查询主键(SELECT LAST_INSERT_ID())方法,使用主库
                    if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
                        lookupKey = DynamicDataSourceHolder.DB_MASTER;
                    } else {
                        //获取第二个参数,即sql
                        BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
                        String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\t\n\r]", " ");
                        //查看是不是增删改,若是则用主库,查则用从库
                        if (sql.matches(REGEX)) {
                            lookupKey = DynamicDataSourceHolder.DB_MASTER;
                        } else {
                            lookupKey = DynamicDataSourceHolder.DB_SLAVE;
                        }
                    }
                }
            } else {
                //若为事务,一般就是写等操作,用主库
                lookupKey = DynamicDataSourceHolder.DB_MASTER;
            }
            logger.debug("设置方法[{}] use [{}] Strategy, SqlCommanType [{}]..", ms.getId(), lookupKey,
                    ms.getSqlCommandType().name());
            //最终决定要哪个数据库
            DynamicDataSourceHolder.setDbType(lookupKey);
            return invocation.proceed();
        }
        //若是Excutor就拦截下来
        @Override
        public Object plugin(Object target) {
            //拦截Executor是因为,Execuror支持一系列增删改查
            if (target instanceof Executor) {
                return Plugin.wrap(target, this);
            } else {
                return target;
            }
        }
    
        @Override
        public void setProperties(Properties arg0) {
            // TODO Auto-generated method stub
    
        }
    
    }

    4、修改Mybatis-conf.xml文件

    5、修改Spring-dao.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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 配置整合mybatis过程 -->
        <!-- 1.配置数据库相关参数properties的属性:${url} -->
        <bean class="com.imooc.o2o.util.EncryptPropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                    <value>classpath:redis.properties</value>
                </list>
            </property>
            <property name="fileEncoding" value="UTF-8" />
        </bean>
        <!-- 2.数据库连接池 -->
        <bean id="abstractDataSource" abstract="true"
            class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <!-- c3p0连接池的私有属性 -->
            <property name="maxPoolSize" value="30" />
            <property name="minPoolSize" value="10" />
            <property name="initialPoolSize" value="10"/> 
            <!-- 关闭连接后不自动commit -->
            <property name="autoCommitOnClose" value="false" />
            <!-- 获取连接超时时间 -->
            <property name="checkoutTimeout" value="10000" />
            <!-- 当获取连接失败重试次数 -->
            <property name="acquireRetryAttempts" value="2" />
        </bean>
        <bean id="master" parent="abstractDataSource">
            <!-- 配置连接池属性 -->
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.master.url}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <bean id="slave" parent="abstractDataSource">
            <!-- 配置连接池属性 -->
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.slave.url}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <!-- 配置动态数据源,这儿targetDataSources就是路由数据源所对应的名称 -->
        <bean id="dynamicDataSource" class="com.imooc.o2o.dao.split.DynamicDataSource">
            <property name="targetDataSources">
                <map>
                    <entry value-ref="master" key="master"></entry>
                    <entry value-ref="slave" key="slave"></entry>
                </map>
            </property>
        </bean>
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
            <property name="targetDataSource">
                <ref bean="dynamicDataSource" />
            </property>
        </bean>
    
        <!-- 3.配置SqlSessionFactory对象 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
            <!-- 扫描entity包 使用别名 -->
            <property name="typeAliasesPackage" value="com.imooc.entity" />
            <!-- 扫描sql配置文件:mapper需要的xml文件 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml" />
        </bean>
    
        <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 注入sqlSessionFactory -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <!-- 给出需要扫描Dao接口包 -->
            <property name="basePackage" value="com.imooc.o2o.dao" />
        </bean>
    </beans>

     

  • 相关阅读:
    springSecurity登陆与退出json形式交互
    SQL-Mysql表结构操作
    SQL-Mysql数据类型
    SQL-SQL事物操作
    springboot之Validation参数校验
    springSecurity之java配置篇
    springsecurity入门篇
    springboot集成shiro
    13个不low的JS数组操作
    基于canvas的五子棋
  • 原文地址:https://www.cnblogs.com/hoje/p/11945415.html
Copyright © 2011-2022 走看看