Spring Boot+Mybatis+druid動的マルチデータソース自由切替

76870 ワード

Spring Boot+Mybatis+druid動的マルチデータソース自由切替
データソースのダイナミックな切り替えを実現し、必要な人を助けたいなら、コードcopyで使えます.
  • pomファイル
  • 
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>1.4.7.RELEASEversion>
        parent>
    
        <groupId>com.crr.zglgroupId>
        <artifactId>apiartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <java.version>1.8java.version>
            <jackson-dataformat-yaml.version>2.8.3jackson-dataformat-yaml.version>
            <junit.version>4.12junit.version>
            <log4j.version>2.6.2log4j.version>
            <druid.version>1.1.3druid.version>
            <apache-commons-pool2.version>2.4.2apache-commons-pool2.version>
            <mysql-connector-java.version>6.0.5mysql-connector-java.version>
            <mybatis-spring-boot-starter.version>1.1.1mybatis-spring-boot-starter.version>
            <jersey-client.version>2.23.1jersey-client.version>
            <jedis.version>2.9.0jedis.version>
            <apache-commons-lang3.version>3.4apache-commons-lang3.version>
            <commons-lang.version>2.6commons-lang.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-loggingartifactId>
                    exclusion>
                exclusions>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-loggingartifactId>
                    exclusion>
                exclusions>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-redisartifactId>
                <version>1.4.7.RELEASEversion>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.3.1version>
            dependency>
            <dependency>
                <groupId>com.jayway.jsonpathgroupId>
                <artifactId>json-pathartifactId>
                <scope>testscope>
            dependency>
            <dependency> 
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql-connector-java.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.dataformatgroupId>
                <artifactId>jackson-dataformat-yamlartifactId>
                <version>${jackson-dataformat-yaml.version}version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
            dependency>
    
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-coreartifactId>
                <version>${log4j.version}version>
            dependency>
    
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
    
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-pool2artifactId>
                <version>${apache-commons-pool2.version}version>
            dependency>
    
            <dependency>
                <groupId>org.apache.tomcatgroupId>
                <artifactId>tomcat-jdbcartifactId>
            dependency>
    
            <dependency> 
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql-connector-java.version}version>
            dependency>
    
            <dependency>
                <groupId>org.glassfish.jersey.coregroupId>
                <artifactId>jersey-clientartifactId>
                <version>${jersey-client.version}version>
            dependency>
    
            <dependency>
                <groupId>redis.clientsgroupId>
                <artifactId>jedisartifactId>
                <version>${jedis.version}version>
            dependency>
    
            <dependency> 
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-log4j2artifactId>
            dependency>
            <dependency>  
                <groupId>com.fasterxml.jackson.dataformatgroupId>
                <artifactId>jackson-dataformat-yamlartifactId>
            dependency>
            <dependency>
                <groupId>commons-beanutilsgroupId>
                <artifactId>commons-beanutilsartifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>commons-loggingartifactId>
                        <groupId>commons-logginggroupId>
                    exclusion>
                exclusions>
            dependency>
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-lang3artifactId>
                <version>${apache-commons-lang3.version}version>
            dependency>
    
            <dependency>
                <groupId>commons-langgroupId>
                <artifactId>commons-langartifactId>
                <version>${commons-lang.version}version>
            dependency>
    
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>3.5.3-betaversion>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-clientartifactId>
                <version>4.0.0version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-frameworkartifactId>
                <version>2.7.1version>
            dependency>
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.8.10version>
            dependency>
        dependencies>
    
    
        <build>
            <finalName>apifinalName>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/resourcesdirectory>
                    <includes>
                        <include>**/*include>
                    includes>
                resource>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    includes>
                    
                    <filtering>falsefiltering>
                resource>
            resources>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
  • データソース構成
  • 
    spring.datasource.masterMapperLocations=classpath:com/rrc/zgl/dao/*/impl/*Mapper.xml
    #master   
    spring.datasource.master.url=jdbc:mysql://localhost:3306/zgl?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
    spring.datasource.master.username=root
    spring.datasource.master.password=123456
    spring.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.master.initialSize=5
    spring.datasource.master.minIdle=5
    spring.datasource.master.maxActive= 20
    spring.datasource.master.maxWait= 60000
    spring.datasource.master.timeBetweenEvictionRunsMillis= 60000
    spring.datasource.master.minEvictableIdleTimeMillis= 300000
    spring.datasource.master.validationQuery= SELECT 1 
    spring.datasource.master.testWhileIdle= true
    spring.datasource.master.testOnBorrow= false
    spring.datasource.master.testOnReturn= false
    spring.datasource.master.poolPreparedStatements= true
    spring.datasource.master.maxPoolPreparedStatementPerConnectionSize= 20
    spring.datasource.master.filters= stat,wall,log4j
    spring.datasource.master.connectionProperties= druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    spring.datasource.master.useGlobalDataSourceStat= true
    
    
    #cluster   
    spring.datasource.cluster.url=jdbc:mysql://localhost:3306/zgl1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
    spring.datasource.cluster.username=root
    spring.datasource.cluster.password=123456
    spring.datasource.cluster.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.cluster.initialSize=5
    spring.datasource.cluster.minIdle=5
    spring.datasource.cluster.maxActive= 20
    spring.datasource.cluster.maxWait= 60000
    spring.datasource.cluster.timeBetweenEvictionRunsMillis= 60000
    spring.datasource.cluster.minEvictableIdleTimeMillis= 300000
    spring.datasource.cluster.validationQuery= SELECT 1 
    spring.datasource.cluster.testWhileIdle= true
    spring.datasource.cluster.testOnBorrow= false
    spring.datasource.cluster.testOnReturn= false
    spring.datasource.cluster.poolPreparedStatements= true
    spring.datasource.cluster.maxPoolPreparedStatementPerConnectionSize= 20
    spring.datasource.cluster.filters= stat,wall,log4j
    spring.datasource.cluster.connectionProperties= druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    spring.datasource.cluster.useGlobalDataSourceStat= true
  • 起動クラス
  • @SpringBootApplication
    @PropertySource(value = {"classpath:dataSource.properties"})
    @ServletComponentScan
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    4.データソース1の構成
    @Configuration
    @EnableTransactionManagement
    public class ClusterDruidDataSourceConfig {
        @Value("${spring.datasource.masterMapperLocations}")
        private String clusterMapperLocations;
    
        @ConfigurationProperties(prefix = "spring.datasource.cluster")
        @Bean(name = "clusterDataSource")
        public DataSource clusterDataSource() {
            return new DruidDataSource();
        }
    
        /**
         * SqlSessionFactory  
         *
         * @return
         * @throws Exception
         */
        @Bean(name = "clusterSqlSessionFactory")
        public SqlSessionFactory clusterSqlSessionFactory(
                @Qualifier("clusterDataSource") DataSource dataSource
        ) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
    
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            //  mapper    
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources(clusterMapperLocations));
    
            return sqlSessionFactoryBean.getObject();
        }
    
        /**
         *        
         *
         * @return
         */
        @Bean(name = "clusterTransactionManager")
        public DataSourceTransactionManager clusterTransactionManager(
                @Qualifier("clusterDataSource") DataSource dataSource
        ) {
            DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
            dataSourceTransactionManager.setDataSource(dataSource);
            return dataSourceTransactionManager;
        }
  • データソース2
  • @Configuration
    @EnableTransactionManagement
    public class MasterDruidDataSourceConfig {
    
        @Value("${spring.datasource.masterMapperLocations}")
        private String masterMapperLocations;
    
        @ConfigurationProperties(prefix = "spring.datasource.master")
        @Bean(name = "masterDataSource")
        @Primary
        public DataSource masterDataSource() {
            return new DruidDataSource();
        }
    
        /**
         * SqlSessionFactory  
         *
         * @return
         * @throws Exception
         */
        @Bean(name = "masterSqlSessionFactory")
        @Primary
        public SqlSessionFactory masterSqlSessionFactory(
                @Qualifier("masterDataSource") DataSource dataSource
        ) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
    
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            //   mapper    
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources(masterMapperLocations));
            return sqlSessionFactoryBean.getObject();
        }
        /**
         *        
         *
         * @return
         */
        @Bean(name = "masterTransactionManager")
        @Primary
        public DataSourceTransactionManager masterTransactionManager(
                @Qualifier("masterDataSource") DataSource dataSource
        ) {
            DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
            dataSourceTransactionManager.setDataSource(dataSource);
            return dataSourceTransactionManager;
        }
    }
  • データソース動的切り替え
  • @Aspect
    @Component
    public class DynamicDataSourceAspect {
        private Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);
    
        @Around("execution(* com.crr.zgl.dao..*.*(..))")
        public Object  around(ProceedingJoinPoint pjp) throws Throwable {
            Signature signature = pjp.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            boolean methodAnnotation = method.isAnnotationPresent(TargetDataSource.class);
    
            TargetDataSource targetDataSource = null;
            if (methodAnnotation) {
                targetDataSource = method.getAnnotation(TargetDataSource.class);
            } else {
                Class clazz[] = pjp.getTarget().getClass().getInterfaces();
                targetDataSource = (TargetDataSource) clazz[0].getAnnotation(TargetDataSource.class);
            }
            if (targetDataSource != null) {
                DynamicDataSourceHolder.setDataSource(targetDataSource.dataSource());
                logger.debug("mybatis  : " + (method.getDeclaringClass() + "." + method.getName()) + "       key is " + targetDataSource.dataSource());
            }
            Object result = pjp.proceed();//    
            DynamicDataSourceHolder.clearDataSource();
            return result;
        }
    }
  • データソース切替
  • /**
     *        
     */
    public class DynamicDataSourceHolder {
            //  ThreadLocal           
            private static final ThreadLocal dataSources = new ThreadLocal();
    
            public static void setDataSource(String dataSourceName) {
                dataSources.set(dataSourceName);
            }
    
            public static String getDataSource() {
                return (String) dataSources.get();
            }
    
            public static void clearDataSource() {
                dataSources.remove();
            }
        }
  • 取得データソース
  • /**
     *        sessionfactory
     * @Author:zhangguangliang
     */
    public class DynamicSqlSessionTemplate extends SqlSessionTemplate {
    
        private static Logger logger = LoggerFactory.getLogger(DynamicSqlSessionTemplate.class);
    
        private SqlSessionFactory sqlSessionFactory;
        private ExecutorType executorType;
        private SqlSession sqlSessionProxy;
        private PersistenceExceptionTranslator exceptionTranslator;
        private Map targetSqlSessionFactorys;
        private SqlSessionFactory defaultTargetSqlSessionFactory;
    
        public void setTargetSqlSessionFactorys(Map targetSqlSessionFactorys) {
            this.targetSqlSessionFactorys = targetSqlSessionFactorys;
        }
    
        public void setDefaultTargetSqlSessionFactory(SqlSessionFactory defaultTargetSqlSessionFactory) {
            this.defaultTargetSqlSessionFactory = defaultTargetSqlSessionFactory;
        }
    
        public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
            this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
        }
    
        public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
            this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true));
        }
    
        public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {
            super(sqlSessionFactory, executorType, exceptionTranslator);
            this.sqlSessionFactory = sqlSessionFactory;
            this.executorType = executorType;
            this.exceptionTranslator = exceptionTranslator;
            this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionInterceptor());
            this.defaultTargetSqlSessionFactory = sqlSessionFactory;
        }
    
        @Override
        public SqlSessionFactory getSqlSessionFactory() {
            String dataSourceType = DynamicDataSourceHolder.getDataSource();
            SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactorys.get(dataSourceType);
            if (targetSqlSessionFactory != null) {
                return targetSqlSessionFactory;
            } else if (defaultTargetSqlSessionFactory != null) {
                if (dataSourceType != null) {
                    logger.warn(" [" + dataSourceType + "]dataSourceType        targetSqlSessionFactorys,    defaultTargetSqlSessionFactory        ");
                }
                return defaultTargetSqlSessionFactory;
            } else {
                Assert.notNull(targetSqlSessionFactorys, "Property 'targetSqlSessionFactorys' or 'defaultTargetSqlSessionFactory' are required");
                Assert.notNull(defaultTargetSqlSessionFactory, "Property 'defaultTargetSqlSessionFactory' or 'targetSqlSessionFactorys' are required");
            }
            return this.sqlSessionFactory;
        }
    
        @Override
        public Configuration getConfiguration() {
            return this.getSqlSessionFactory().getConfiguration();
        }
        @Override
        public ExecutorType getExecutorType() {
            return this.executorType;
        }
        @Override
        public PersistenceExceptionTranslator getPersistenceExceptionTranslator() {
            return this.exceptionTranslator;
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  T selectOne(String statement) {
            return this.sqlSessionProxy.selectOne(statement);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  T selectOne(String statement, Object parameter) {
            return this.sqlSessionProxy.selectOne(statement, parameter);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  Map selectMap(String statement, String mapKey) {
            return this.sqlSessionProxy.selectMap(statement, mapKey);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  Map selectMap(String statement, Object parameter, String mapKey) {
            return this.sqlSessionProxy.selectMap(statement, parameter, mapKey);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
            return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  List selectList(String statement) {
            return this.sqlSessionProxy.selectList(statement);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  List selectList(String statement, Object parameter) {
            return this.sqlSessionProxy.selectList(statement, parameter);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  List selectList(String statement, Object parameter, RowBounds rowBounds) {
            return this.sqlSessionProxy.selectList(statement, parameter, rowBounds);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void select(String statement, ResultHandler handler) {
            this.sqlSessionProxy.select(statement, handler);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void select(String statement, Object parameter, ResultHandler handler) {
            this.sqlSessionProxy.select(statement, parameter, handler);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
            this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public int insert(String statement) {
            return this.sqlSessionProxy.insert(statement);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public int insert(String statement, Object parameter) {
            return this.sqlSessionProxy.insert(statement, parameter);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public int update(String statement) {
            return this.sqlSessionProxy.update(statement);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public int update(String statement, Object parameter) {
            return this.sqlSessionProxy.update(statement, parameter);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public int delete(String statement) {
            return this.sqlSessionProxy.delete(statement);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public int delete(String statement, Object parameter) {
            return this.sqlSessionProxy.delete(statement, parameter);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public  T getMapper(Class type) {
            return getConfiguration().getMapper(type, this);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void commit() {
            throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void commit(boolean force) {
            throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void rollback() {
            throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void rollback(boolean force) {
            throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void close() {
            throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void clearCache() {
            this.sqlSessionProxy.clearCache();
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public Connection getConnection() {
            return this.sqlSessionProxy.getConnection();
        }
    
        /**
         * {@inheritDoc}
         *
         * @since 1.0.2
         */
        @Override
        public List flushStatements() {
            return this.sqlSessionProxy.flushStatements();
        }
    
        /**
         * Proxy needed to route MyBatis method calls to the proper SqlSession got from Spring's Transaction Manager It also unwraps exceptions thrown by
         * {@code Method#invoke(Object, Object...)} to pass a {@code PersistenceException} to the {@code PersistenceExceptionTranslator}.
         */
        private class SqlSessionInterceptor implements InvocationHandler {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                SqlSession sqlSession = getSqlSession(DynamicSqlSessionTemplate.this.getSqlSessionFactory(), DynamicSqlSessionTemplate.this.executorType, DynamicSqlSessionTemplate.this.exceptionTranslator);
                try {
                    Object result = method.invoke(sqlSession, args);
                    if (!isSqlSessionTransactional(sqlSession, DynamicSqlSessionTemplate.this.getSqlSessionFactory())) {
                        // force commit even on non-dirty sessions because some databases require / a commit/rollback before calling close()
                        sqlSession.commit(true);
                    }
                    return result;
                } catch (Throwable t) {
                    Throwable unwrapped = unwrapThrowable(t);
                    if (DynamicSqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
                        // release the connection to avoid a deadlock if the translator is no loaded. See issue #22
                        closeSqlSession(sqlSession, DynamicSqlSessionTemplate.this.getSqlSessionFactory());
                        sqlSession = null;
                        Throwable translated = DynamicSqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
                        if (translated != null) {
                            unwrapped = translated;
                        }
                    }
                    throw unwrapped;
                } finally {
                    if (sqlSession != null) {
                        closeSqlSession(sqlSession, DynamicSqlSessionTemplate.this.getSqlSessionFactory());
                    }
                }
            }
        }
    }
    
    
    
    /**
     *      
     * 
     * @Author:zhangguangliang
     * @Time:2017 9 16 14:44:10
     */
    public class ReflectionUtil extends ReflectionUtils {
    
        private static Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
    
        /**
         *   obj  fieldName Field
         * 
         * @param obj
         * @param fieldName
         * @return
         */
        public static Field getFieldByFieldName(Object obj, String fieldName) {
            if (obj == null || fieldName == null) {
                return null;
            }
            for (Class> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
                try {
                    return superClass.getDeclaredField(fieldName);
                } catch (Exception e) {
    
                }
            }
            return null;
        }
    
        /**
         *   obj  fieldName    
         * 
         * @param obj
         * @param fieldName
         * @return
         */
        public static Object getValueByFieldName(Object obj, String fieldName) {
            Object value = null;
            try {
                Field field = getFieldByFieldName(obj, fieldName);
                if (field != null) {
                    if (field.isAccessible()) {
                        value = field.get(obj);
                    } else {
                        field.setAccessible(true);
                        value = field.get(obj);
                        field.setAccessible(false);
                    }
                }
            } catch (Exception e) {
                logger.error("             :");
            }
            return value;
        }
    
        /**
         *   obj  fieldName    
         * 
         * @param obj
         * @param fieldName
         * @return
         */
        @SuppressWarnings("unchecked")
        public static  T getValueByFieldType(Object obj, Class fieldType) {
            Object value = null;
            for (Class> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
                try {
                    Field[] fields = superClass.getDeclaredFields();
                    for (Field f : fields) {
                        if (f.getType() == fieldType) {
                            if (f.isAccessible()) {
                                value = f.get(obj);
                                break;
                            } else {
                                f.setAccessible(true);
                                value = f.get(obj);
                                f.setAccessible(false);
                                break;
                            }
                        }
                    }
                    if (value != null) {
                        break;
                    }
                } catch (Exception e) {
                     logger.error("             :");
                }
            }
            return (T) value;
        }
    
        /**
         *   obj  fieldName    
         * 
         * @param obj
         * @param fieldName
         * @param value
         * @throws SecurityException
         * @throws NoSuchFieldException
         * @throws IllegalArgumentException
         * @throws IllegalAccessException
         */
        public static boolean setValueByFieldName(Object obj, String fieldName, Object value) {
            try {
                // java.lang.Class.getDeclaredField()         -
                //       Field  ,    Class                  。
                //                Field  
                Field field = obj.getClass().getDeclaredField(fieldName);
                /**
                 * public void setAccessible(boolean flag) throws SecurityException      accessible            。   true                  Java       。   false              Java       。   ,         ,  
                 * ReflectPermission("suppressAccessChecks")       checkPermission   。    flag   true,              (  ,         Class    Constructor   ),     SecurityException。        java.lang.Class    Constructor   ,  
                 * flag   true,     SecurityException。   : flag - accessible         : SecurityException -        。
                 */
                if (field.isAccessible()) {//        accessible     。
                    field.set(obj, value);//           Field                
                } else {
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                return true;
            } catch (Exception e) {
                logger.error("             :");
            }
            return false;
        }
    
        /**
         *         
         * 
         * @param owner
         * @param methodName
         * @param args
         * @return
         * @throws Exception
         */
        public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
            Class extends Object> cls = owner.getClass();
            @SuppressWarnings("rawtypes")
            Class[] argclass = new Class[args.length];
            for (int i = 0, j = argclass.length; i < j; i++) {
                argclass[i] = args[i].getClass();
            }
            Method method = cls.getMethod(methodName, argclass);
            return method.invoke(owner, args);
        }
    
        /**
         *         
         * 
         * @param className
         * @param methodName
         * @param args
         * @return
         * @throws Exception
         */
        public Object invokeStaticMethod(String className, String methodName, Object[] args) throws Exception {
            Class> cls = Class.forName(className);
            @SuppressWarnings("rawtypes")
            Class[] argclass = new Class[args.length];
            for (int i = 0, j = argclass.length; i < j; i++) {
                argclass[i] = args[i].getClass();
            }
            Method method = cls.getMethod(methodName, argclass);
            return method.invoke(null, args);
        }
    
        public Object newInstance(String className, Object[] args) throws Exception {
            Class> clss = Class.forName(className);
            @SuppressWarnings("rawtypes")
            Class[] argclass = new Class[args.length];
            for (int i = 0, j = argclass.length; i < j; i++) {
                argclass[i] = args[i].getClass();
            }
            Constructor> cons = clss.getConstructor(argclass);
            return cons.newInstance();
        }
    
    }
  • druid構成クラス
  • @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
        initParams={
            @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//     
    })
    public class DruidStatFilter extends WebStatFilter {
    
    }
    
    @WebServlet(urlPatterns = "/druid/*",
            initParams = {
                    @WebInitParam(name = "allow", value = "127.0.0.1"),// IP    (        ,       )
                    //@WebInitParam(name="deny",value="192.168.16.111"),// IP    (     ,deny   allow)
                    //@WebInitParam(name="loginUsername",value="zgl"),//    
                    //@WebInitParam(name="loginPassword",value="zgl"),//   
                    @WebInitParam(name = "resetEnable", value = "false")//   HTML    “Reset All”  
            })
    public class DruidStatViewServlet extends StatViewServlet {
    
        //   BeanId        bean   
       /* @Bean(value = "druid-stat-interceptor")
        public DruidStatInterceptor DruidStatInterceptor() {
            DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
            return druidStatInterceptor;
        }
    
        @Bean
        public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
            BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
            beanNameAutoProxyCreator.setProxyTargetClass(true);
            //       bean id
            beanNameAutoProxyCreator.setBeanNames("clusterSqlSessionFactory","masterTransactionManager");
            beanNameAutoProxyCreator.setInterceptorNames("druid-stat-interceptor");
            return beanNameAutoProxyCreator;
        }*/
    
    }
  • 使用
  • @TargetDataSource(dataSource = DataSourceConfig.masterDataSource_)
    @Repository(value = "userMapper")
    public interface UserMapper {
        int deleteByPrimaryKey(Long id);
        @TargetDataSource(dataSource = DataSourceConfig.clusterDataSource_)
        int insert(User record);
    
        int insertSelective(User record);
    
        User selectByPrimaryKey(Long id);
    
        int updateByPrimaryKeySelective(User record);
    
        int updateByPrimaryKey(User record);
    }
    
    
    @Transactional(transactionManager = "clusterTransactionManager",rollbackFor=Exception.class)
        public String test() {
            User user = new User();
            user.setName("124");
            user.setPassword("setPassword");
            int insert1 = zgl1UserMapper.insert(user);
            //int insert = userMapper.insert(user);
            return "success";
        }