JFinal事务配置,将事务交给Spring管理

import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import com.alibaba.druid.pool.DruidDataSource;
import com.boss.sms.entity._MappingKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;

/**
 * @Data : 2020年4月27日
 * @author : Leaual
 * @description : JFinal配置类
 */

@Configuration
public class JFinalConfig  {
    
 // 将全局配置提出来 方便其它地方重用
//    private static Prop prop = PropKit.use("application-dev.properties");
    
    /**
     * 初始化连接池
     * @return
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
    
    /**
     * 配置数据源代理
     * @return
     */
    @Bean
    public TransactionAwareDataSourceProxy transactionAwareDataSourceProxy() {
        TransactionAwareDataSourceProxy transactionAwareDataSourceProxy = new TransactionAwareDataSourceProxy();
        transactionAwareDataSourceProxy.setTargetDataSource(druidDataSource());
        return transactionAwareDataSourceProxy;
    }
    
    /**
     * 配置事务管理
     * @return
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(transactionAwareDataSourceProxy());
        return dataSourceTransactionManager;
    }

    

    /**
     * 配置ActiveRecord
     * @return
     */
    @Bean
    public ActiveRecordPlugin getActiveRecordPlugin() {
          
//        DruidPlugin druidPlugin = new DruidPlugin(prop.get("url"), prop.get("user"), prop.get("password"));
//        druidPlugin.setDriverClass("com.mysql.jdbc.Driver");
//        druidPlugin.start();
        
        ActiveRecordPlugin arp = new ActiveRecordPlugin(transactionAwareDataSourceProxy());
        arp.setShowSql(true);
        arp.setDialect(new MysqlDialect());

        _MappingKit.mapping(arp);
        arp.start();
        return arp;
    }
}

配置文件:image.png

测试过了,按理说是没有问题的,如果有问题大家多多指出!因为之前遇到需要将这个事务交给Spring管理的问题,网上找的解决办法比较少,很多都用用那个切面去做,我认为没必要。这样子简单配置应该就可以了

评论区

热门分享

扫码入社