一些技术总结

一.Mybatis

1.Integer类型传值为0时

在mybatis中会自动把0当成null,所以if判断为false,如果要传值为0时判断为true,只要将判断为空串的判断去掉即可

2.在java中拼sql

 

二.多事务手动提交

// 1.1 配置类
@Configuration 
public class TransactionManagementConfig implements TransactionManagementConfigurer{
    
    @Resource(name="selfErpTransactionManager")
    private PlatformTransactionManager selfErpTransactionManager;

    @Bean(name = "selfErpTransactionManager")
    public PlatformTransactionManager selfTesttransactionManager(@Qualifier("erpDataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    
    // 实现接口 TransactionManagementConfigurer 方法,其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return selfErpTransactionManager;
    }

}
// 1.2 service 里
@Service
public class ErpFileTaskServiceImpl {
    @Resource(name = "selfErpTransactionManager")
    private PlatformTransactionManager transactionManager;

    public void updateStatus(List taskList){  // 具体的方法
        //DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) SpringContextHolder.getBean("transactionManager");
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务,这样会比较安全些。
        TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
        erpTaskMapper.batchUpdateFileStatus(taskList); // TODO sth.
        transactionManager.commit(status);
    }
        
}

三:静态方法依赖注入

@Component
@Lazy(false)
public class FileUtils {

    @Autowired 
    private ErpUtilMapper erpUtilMapper; 

    private static FileUtils fileUtils; 

    @PostConstruct 
    public void init() { 
        fileUtils = this; 
        fileUtils.erpUtilMapper = this.erpUtilMapper; 
    } 

    public static void doSth(){

        fileUtils.erpUtilMapper.insert(Bean);
    }

}

四 配置 : druid支持多条sql执行在一个方法里

@Configuration 
@MapperScan(basePackages = { "com.diditech.bigdata.datasync.erp.dao.erp" }, sqlSessionFactoryRef = "erpSqlSessionFactoryBean")
public class ErpDruidDBConfig {  

    @Bean(name = "wallConfig")
    WallConfig wallFilterConfig(){
        WallConfig wc = new WallConfig ();
        wc.setMultiStatementAllow(true);
        return wc;
    }

    @Autowired
    WallFilter wallFilter;

    @Bean(name = "wallFilter")
    @DependsOn("wallConfig")
    WallFilter wallFilter(WallConfig wallConfig){
        WallFilter wfilter = new WallFilter ();
        wfilter.setConfig(wallConfig);
        return wfilter;
    }

    @Bean(name = "erpDataSource")
    public DataSource accTestdataSource(){  
        DruidDataSource datasource = new DruidDataSource();  
        datasource.setUrl(dbUrl);  
        datasource.setUsername(username);  
        datasource.setPassword(password);  
        datasource.setDriverClassName(driverClassName);  
        //configuration  
        datasource.setInitialSize(initialSize);  
        datasource.setMinIdle(minIdle);  
        datasource.setMaxActive(maxActive);  
        datasource.setMaxWait(maxWait);  
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        datasource.setValidationQuery(validationQuery);  
        datasource.setTestWhileIdle(testWhileIdle);  
        datasource.setTestOnBorrow(testOnBorrow);  
        datasource.setTestOnReturn(testOnReturn);  
        datasource.setPoolPreparedStatements(poolPreparedStatements);  
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); 
        datasource.setDefaultAutoCommit(false);
        /**  modify by ff 20171102 为了多条sql执行   BEGIN
        try {
            datasource.setFilters(filters);  
        } catch (SQLException e) {  
        }
         */
        List filters = new ArrayList<>();
        StatFilter statFilter = new StatFilter();
        filters.add(statFilter);
        filters.add(wallFilter);
        datasource.setProxyFilters(filters);
        // modify by ff 20171102 为了多条sql执行   END
        datasource.setConnectionProperties(connectionProperties);
        return datasource;  
    }  
}

五 未整理 : static 里 线程安全的配置

你可能感兴趣的:(一些技术总结)