Skip to content

Commit

Permalink
增加多SqlSessionFactory测试.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Oct 20, 2023
1 parent 8b7cd6c commit e01024b
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.h2.Driver;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
* @author nieqiurong
*/
@ComponentScan("com.baomidou.mybatisplus.test.multisqlsessionfactory")
@MapperScans(
{
@MapperScan(value = "com.baomidou.mybatisplus.test.multisqlsessionfactory.a.mapper", sqlSessionFactoryRef = "sqlSessionFactory1"),
@MapperScan(value = "com.baomidou.mybatisplus.test.multisqlsessionfactory.b.mapper", sqlSessionFactoryRef = "sqlSessionFactory2")
}
)
@Configuration
public class AppConfig {

@Bean
public DataSource dataSourceA() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(new Driver());
dataSource.setUrl("jdbc:h2:mem:testa;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}

@Bean
public DataSourceTransactionManager transactionManagerA(DataSource dataSourceA) {
return new DataSourceTransactionManager(dataSourceA);
}

@Bean
public TransactionTemplate transactionTemplateA(DataSourceTransactionManager transactionManagerA) {
return new TransactionTemplate(transactionManagerA);
}

@Bean
public SqlSessionFactory sqlSessionFactory1(DataSource dataSourceA) throws Exception {
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSourceA);
mybatisSqlSessionFactoryBean.setConfiguration(new MybatisConfiguration());
return mybatisSqlSessionFactoryBean.getObject();
}

@Bean
public DataSource dataSourceB() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(new Driver());
dataSource.setUrl("jdbc:h2:mem:testb;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}

@Bean
public DataSourceTransactionManager transactionManagerB(DataSource dataSourceB) {
return new DataSourceTransactionManager(dataSourceB);
}

@Bean
public TransactionTemplate transactionTemplateB(DataSourceTransactionManager transactionManagerB) {
return new TransactionTemplate(transactionManagerB);
}


@Bean
public SqlSessionFactory sqlSessionFactory2(DataSource dataSourceB) throws Exception {
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSourceB);
mybatisSqlSessionFactoryBean.setConfiguration(new MybatisConfiguration());
return mybatisSqlSessionFactoryBean.getObject();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory;

import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.entity.AEntity;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.service.AEntityService;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.entity.BEntity;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.service.BEntityService;
import org.apache.ibatis.jdbc.SqlRunner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.sql.DataSource;
import java.util.List;

/**
* @author nieqiurong
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {AppConfig.class})
public class MultiSqlSessionFactoryTest {

@Autowired
private AEntityService aEntityService;

@Autowired
private BEntityService bEntityService;

@Autowired
private DataSource dataSourceA;

@Autowired
private DataSource dataSourceB;

@Test
void test() throws Exception {
new SqlRunner(dataSourceA.getConnection()).run(
"""
CREATE TABLE IF NOT EXISTS t_entity_a (
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NULL DEFAULT NULL ,
PRIMARY KEY (id)
);
"""
);

new SqlRunner(dataSourceB.getConnection()).run(
"""
CREATE TABLE IF NOT EXISTS t_entity_b (
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NULL DEFAULT NULL ,
PRIMARY KEY (id)
);
"""
);
Assertions.assertEquals(0L, aEntityService.count());
Assertions.assertEquals(0L, bEntityService.count());
aEntityService.testSaveBath(List.of(new AEntity("test1"), new AEntity("test2")));
Assertions.assertEquals(2L, aEntityService.count());
bEntityService.testSaveBath(List.of(new BEntity("test1"), new BEntity("test2"), new BEntity("test3")));
Assertions.assertEquals(3L, bEntityService.count());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.a.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
* @author nieqiurong
*/
@Data
@TableName("t_entity_a")
public class AEntity {

private Long id;

private String name;

public AEntity() {
}

public AEntity(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.a.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.entity.AEntity;
import org.apache.ibatis.annotations.Mapper;

/**
* @author nieqiurong
*/
@Mapper
public interface AEntityMapper extends BaseMapper<AEntity> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.a.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.entity.AEntity;

import java.util.List;

/**
* @author nieqiurong
*/
public interface AEntityService extends IService<AEntity> {

void testSaveBath(List<AEntity> aEntityList);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.a.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.entity.AEntity;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.mapper.AEntityMapper;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.a.service.AEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
* @author nieqiurong
*/
@Service
public class AEntityServiceImpl extends ServiceImpl<AEntityMapper, AEntity> implements AEntityService {

@Autowired
protected TransactionTemplate transactionTemplateA;

@Override
@Transactional(rollbackFor = RuntimeException.class, transactionManager = "transactionManagerA")
public void testSaveBath(List<AEntity> aEntityList) {
transactionTemplateA.execute((c) -> {
this.saveBatch(aEntityList);
return null;
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.b.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
* @author nieqiurong
*/
@Data
@TableName("t_entity_b")
public class BEntity {

private Long id;

private String name;

public BEntity() {
}

public BEntity(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.b.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.entity.BEntity;
import org.apache.ibatis.annotations.Mapper;

/**
* @author nieqiurong
*/
@Mapper
public interface BEntityMapper extends BaseMapper<BEntity> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.b.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.entity.BEntity;

import java.util.List;

/**
* @author nieqiurong
*/
public interface BEntityService extends IService<BEntity> {

void testSaveBath(List<BEntity> bEntityList);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baomidou.mybatisplus.test.multisqlsessionfactory.b.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.entity.BEntity;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.mapper.BEntityMapper;
import com.baomidou.mybatisplus.test.multisqlsessionfactory.b.service.BEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
* @author nieqiurong
*/
@Service
public class BEntityServiceImpl extends ServiceImpl<BEntityMapper, BEntity> implements BEntityService {

@Autowired
private TransactionTemplate transactionTemplateB;

@Override
@Transactional(rollbackFor = RuntimeException.class, transactionManager = "transactionManagerB")
public void testSaveBath(List<BEntity> bEntityList) {
transactionTemplateB.execute((c) -> {
saveBatch(bEntityList);
return null;
});
}

}

0 comments on commit e01024b

Please sign in to comment.