You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/11/28 15:35:41 UTC

[GitHub] [shardingsphere] dbin0123 commented on issue #22387: rewrite sql to lose the schema

dbin0123 commented on issue #22387:
URL: https://github.com/apache/shardingsphere/issues/22387#issuecomment-1329309709

   ##### spring config
   `
   
       @Slf4j
       @Configuration
       @EnableTransactionManagement
       @EnableJpaRepositories(basePackages = {"com.example.demo.dao.repository"})
       public class JPAConfig {
               @Primary
               @Bean(name = "entityManagerFactory")
               public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("dataSource") DataSource dataSource) {
                   LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
                   em.setDataSource(dataSource);
                   em.setPackagesToScan("com.example.demo.dao.entity");
                   JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
                   em.setJpaVendorAdapter(vendorAdapter);
                   em.setJpaProperties(additionalProperties());
                   return em;
               }
           
               private Properties additionalProperties() {
                   Properties properties = new Properties();
                   properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DmDialect");
                   return properties;
               }
           
               @Primary
               @Bean(name = "transactionManager")
               public JpaTransactionManager transactionManager(@Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
                   JpaTransactionManager manager = new JpaTransactionManager();
                   manager.setEntityManagerFactory(entityManagerFactory.getObject());
                   return manager;
               }
               @Primary
               @Bean(name = "dataSource")
               public DataSource dataSource() throws SQLException {
                   //全局是否保存明文字段
                   Boolean shardingSavePlain = Boolean.FALSE;
                   //使用加密字段查询
                   Boolean queryWithCipherColumn = Boolean.TRUE;
                   //分表, 加密, 读写分离等配置
                   List<RuleConfiguration> listShardingRuleConfig = new ArrayList<>();
                   //数据源
                   String dataSourceName = "healthcloud_platform_support";
                   Map<String, DataSource> dataSourceMap = new HashMap<>(1);
                   dataSourceMap.put(dataSourceName, getDataSource());
           
                   log.info("代理数据源名称:{}, 使用加密字段查询:{},是否保存明文字段:{}, 初始化加密字段信息...开始...", dataSourceName, queryWithCipherColumn, shardingSavePlain);
                   //表, 字段加密配置
                   Map<String, List<EncryptColumnRuleConfiguration>> listEncryptTableMap = new HashMap<>();
                   //是否使用密码列查询 默认true
           
                   //逻辑列, 加密类, HASH列, 明文列
                   listEncryptTableMap.put("tb_sharding_encrypt", Arrays.asList(
                           new EncryptColumnRuleConfiguration("name", "name_e", "name_m", null, "AES", "MD5", true)));
                   //表字段加密信息
                   List<EncryptTableRuleConfiguration> listEncryptTableRule = new ArrayList<>();
                   for (Map.Entry<String, List<EncryptColumnRuleConfiguration>> encryptColumnRuleEntry : listEncryptTableMap.entrySet()) {
                       listEncryptTableRule.add(new EncryptTableRuleConfiguration(encryptColumnRuleEntry.getKey(), encryptColumnRuleEntry.getValue(), queryWithCipherColumn));
                   }
                   //加密配置
                   Map<String, AlgorithmConfiguration> encryptorsMap = new HashMap<>(2);
                   Properties aesProperties = new Properties();
                   aesProperties.setProperty("aes-key-value", "12345");
                   encryptorsMap.put("AES", new AlgorithmConfiguration("AES", aesProperties));
                   encryptorsMap.put("MD5", new AlgorithmConfiguration("MD5", new Properties()));
           
                   //表名称, 字段加密信息
                   listShardingRuleConfig.add(new EncryptRuleConfiguration(listEncryptTableRule, encryptorsMap, queryWithCipherColumn));
                   log.info("代理数据源名称:{}, 使用加密字段查询:{}, 初始化数据分片配置在...开始...", dataSourceName, queryWithCipherColumn);
                   try {
                       Class.forName("org.h2.Driver");
                   } catch (ClassNotFoundException e) {
                   }
                   ModeConfiguration modeConfiguration = new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("JDBC", new Properties()));
           
                   Properties properties = new Properties();
                   properties.put("sql-show", true);
                   properties.put("sharding-save-plain", shardingSavePlain);
                   properties.put("query-with-cipher-column", queryWithCipherColumn);
                   return ShardingSphereDataSourceFactory.createDataSource(dataSourceName, modeConfiguration, dataSourceMap, listShardingRuleConfig, new Properties());
               }
           
               public DataSource getDataSource() throws SQLException {
                   DruidDataSource dataSource = new DruidDataSource();
                   dataSource.setDriverClassName("dm.jdbc.driver.DmDriver");
                   dataSource.setUrl("jdbc:dm://127.0.0.1:5236/test?characterEncoding=utf-8&useSSL=false");
                   dataSource.setUsername("sys_dev");
                   dataSource.setPassword("123456");
                   dataSource.setInitialSize(10);
                   dataSource.setMinIdle(1);
                   dataSource.setMaxActive(2000);
                   dataSource.setMaxWait(60000L);
                   dataSource.setTimeBetweenEvictionRunsMillis(60000L);
                   dataSource.setMinEvictableIdleTimeMillis(300000L);
                   dataSource.setValidationQuery("SELECT 1");
                   dataSource.setTestWhileIdle(true);
                   dataSource.setTestOnBorrow(true);
                   dataSource.setTestOnReturn(false);
                   return dataSource;
               }
       }
   `
   @strongduanmu 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org