Coder Social home page Coder Social logo

litchi's Introduction

荔枝(Litchi)

OSCS LICENSE Maven Java Code Size

关于

一款基于Spring+Mybatis开发的轻量化本地多数据源事务控制插件,荔枝很好吃😁

快速开始

添加依赖

<dependency>
    <groupId>com.yhzdys</groupId>
    <artifactId>litchi</artifactId>
    <version>${litchi.version}</version>
</dependency>

多数据源配置

@Bean
public DataSource masterDataSource() {
    // ....
    return dataSource;
}

@Bean
public DataSource slave1DataSource() {
    // ....
    return dataSource;
}

@Bean
public DataSource slave2DataSource() {
    // ....
    return dataSource;
}

@Bean
@Primary
public DataSource multiDataSource(@Qualifier("master") DataSource masterDataSource,
                                  @Qualifier("slave1") DataSource slave1DataSource,
                                  @Qualifier("slave1") DataSource slave2DataSource) {
    Map<String, DataSource> map = new HashMap<>(4);
    map.put("master", masterDataSource);
    map.put("slave1", slave1DataSource);
    map.put("slave2", slave2DataSource);

    MultiDataSource dataSource = new MultiDataSource();
    // 多数据源
    dataSource.setDataSources(map);
    // 默认数据源
    dataSource.setDefaultDataSource("master");
    return dataSource;
}

多数据源事务控制

/**
 * 多数据源事务切面配置
 */
@Bean
public PointcutAdvisor multiTransactionAdvisor() {
    return new MultiTransactionAdvisor();
}

多数据源自动切换

以下两种方案任选其一即可! 以下两种方案任选其一即可! 以下两种方案任选其一即可!

1. 使用Litchi动态代理(推荐)

import com.yhzdys.litchi.support.mybatis.MapperFactoryBean;

// 使用LitchiMapperFactoryBean替换Mybatis原生的MapperFactoryBean
@MapperScan(basePackages = "com.xxx.xxx.mapper", factoryBean = MapperFactoryBean.class)

2. 使用Mybatis插件

@Bean
public Interceptor multiDataSourceInterceptor() {
    return new MultiDataSourceInterceptor();
}

Mapper接口定义切换的数据源

@RoutingDataSource("slave1")
public interface UserMapper {
  // ...
}

事务控制

@Service
public class PaperMoonService {

    @Resource
    private MasterMapper masterMapper;
    @Resource
    private Salve1Mapper salve1Mapper;
    @Resource
    private Salve2Mapper salve2Mapper;

    @MultiTransactional(rollbackFor = {BizException.class, RpcException.class}, noRollbackFor = {IgnoreException.class}, propagation = Propagation.REQUIRED)
    public void doService() {
        // ...
        masterMapper.insert();
        // ...
        salve1Mapper.update();
        // ...
        salve2Mapper.delete();
    }
}

支持的事务传播方式

public enum Propagation {

    /**
     * 支持当前事务,如果当前没有事务,就新建一个事务。
     */
    REQUIRED,

    /**
     * 支持当前事务,如果当前没有事务,就以非事务方式执行。
     */
    SUPPORTS,

    /**
     * 支持当前事务,如果当前没有事务,就抛出异常。
     */
    MANDATORY,

    /**
     * 新建事务,如果当前存在事务,把当前事务挂起。
     */
    REQUIRES_NEW,

    /**
     * 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
     */
    NOT_SUPPORTED,

    /**
     * 以非事务方式执行,如果当前存在事务,则抛出异常。
     */
    NEVER,
}

litchi's People

Contributors

yhzdys avatar

Stargazers

liuhuacong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.