-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b7cd6c
commit e01024b
Showing
10 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...is-plus/src/test/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...java/com/baomidou/mybatisplus/test/multisqlsessionfactory/MultiSqlSessionFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
.../src/test/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/a/entity/AEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...est/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/a/mapper/AEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...t/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/a/service/AEntityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...m/baomidou/mybatisplus/test/multisqlsessionfactory/a/service/impl/AEntityServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
.../src/test/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/b/entity/BEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...est/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/b/mapper/BEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...t/java/com/baomidou/mybatisplus/test/multisqlsessionfactory/b/service/BEntityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...m/baomidou/mybatisplus/test/multisqlsessionfactory/b/service/impl/BEntityServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
|
||
} |