应用平台 APPSTAGE-使用Rainbow SDK(Spring Cloud框架):初始化Rainbow SDK

时间:2024-10-21 16:40:47

初始化Rainbow SDK

代码如下:

package com.huawei.demo.serviceb.config;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import com.huawei.nuwa.map.client.NuwaMapClient;
import com.huawei.nuwa.map.spring.boot.autoconfigure.NuwaCloudMapAutoConfiguration;
import com.huawei.rainbow.jdbc.DbGroupDataSource;
import com.huawei.wisesecurity.sts.springboot.security.configuration.StsEncryptablePropertiesConfiguration;

/**
 * Rainbow启动
 */
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@Import(value = {NuwaCloudMapAutoConfiguration.class, StsEncryptablePropertiesConfiguration.class})
public class ApplicationAutoConfig implements EnvironmentAware {
    private Environment environment;

    /**
     * Rainbow需要依赖STS和Cloud Map启动,创建dataSource
     *
     * @param client NuwaMapClient值对象
     * @return DataSource 初始化完成的数据源数据源
     */
    @Bean
    @DependsOn({"stsBootStrap"})
    public DataSource createDataSource(NuwaMapClient client) {
        DbGroupDataSource dataSource = new DbGroupDataSource();
        dataSource.setUseSts(true);
        dataSource.setAppName(environment.getProperty("wiseDba.appName"));
        dataSource.setDbGroupKey(environment.getProperty("wiseDba.groupKey"));
        dataSource.setDbName(environment.getProperty("wiseDba.dbName"));
        dataSource.init();
        return dataSource;
    }

    /**
     * 事务管理创建
     *
     * @param dataSource 数据源
     * @return DataSourceTransactionManager 事务管理
     */
    @Bean
    public DataSourceTransactionManager createTransaction(DataSource dataSource) {
        DataSourceTransactionManager transaction = new DataSourceTransactionManager();
        transaction.setDataSource(dataSource);
        return transaction;
    }

    @Bean
    public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean("sqlSessionFactoryBean")
    public SqlSessionFactoryBean createMybatisSqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);

        // 数据源配置项
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();

        // 允许jdbc自动生成主键
        configuration.setUseGeneratedKeys(true);

        // 使用列标签代替列名
        configuration.setUseColumnLabel(true);

        // 打开下划线命名自动转换为驼峰命名开关
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactoryBean.setConfiguration(configuration);
        return sessionFactoryBean;
    }

    @Bean
    public TransactionTemplate createTransactionTemplate(DataSourceTransactionManager dataSourceTransactionManager) {
        TransactionTemplate template = new TransactionTemplate();
        template.setTransactionManager(dataSourceTransactionManager);
        return template;
    }

    @Bean
    public MapperScannerConfigurer createMapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.huawei.demo");
        configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        return configurer;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
support.huaweicloud.com/sdkreference-appstage/appstage_08_0022.html