Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mybatis Spring multiple databases Java configuration

Tags:

I'm working with Spring and Mybatis and I have two databases, the configuration for the first database was relative easy, but I can't get to work the second database with Spring and transactions, here is my code

@Configuration @ComponentScan(basePackages = {"hernandez.service", "hernandez.dao"}) @EnableTransactionManagement @MapperScan(basePackages="hernandez.mapper" )  @Import(DbConfig2.class) public class AppConfig {  @Bean(name = "dataSource") public DataSource dataSource() {     DriverManagerDataSource ds = new DriverManagerDataSource("com.mysql.jdbc.Driver",             "jdbc:mysql://localhost:3306/northwind", "root", "");     return ds; }  @Bean public SqlSessionFactoryBean sqlSessionFactory() {     SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();     factoryBean.setDataSource(dataSource());      return factoryBean; }  @Bean(name = "transactionManager") public PlatformTransactionManager transactionManager() {     return new DataSourceTransactionManager(dataSource()); } }  @Configuration @MapperScan("loli.mapper" )  public class DbConfig2 { @Bean(name = "dataSource_2") public DataSource dataSource2() {     DriverManagerDataSource ds = new DriverManagerDataSource("com.mysql.jdbc.Driver",             "jdbc:mysql://localhost:3306/dmsolut_dmsms", "root", "");     return ds; }  @Bean public SqlSessionFactory sqlSessionFactory2() throws Exception{     SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();     factoryBean.setDataSource(dataSource2());     return factoryBean.getObject(); }  @Bean(name = "transactionManager_2") public PlatformTransactionManager transactionManager() {     return new DataSourceTransactionManager(dataSource2()); } } 

Is there a way to get this working with pure Spring Java configuration or at least with some XML? There's no official documentation to get two databases working in the Mybatis-Spring project

like image 546
Nestor Hernandez Loli Avatar asked Aug 13 '13 05:08

Nestor Hernandez Loli


People also ask

Does MyBatis support Spring?

MyBatis is a SQL Mapping framework with support for custom SQL, stored procedures and advanced mappings. SpringBoot doesn't provide official support for MyBatis integration, but the MyBatis community built a SpringBoot starter for MyBatis.

How do I integrate MyBatis with spring boot?

To use the MyBatis-Spring-Boot-Starter module, you just need to include the mybatis-spring-boot-autoconfigure. jar file and its dependencies( mybatis. jar , mybatis-spring. jar and etc …) in the classpath.

Does MyBatis use JDBC?

MyBatis does four main things: It executes SQL safely and abstracts away all the intricacies of JDBC. It maps parameter objects to JDBC prepared statement parameters. It maps rows in JDBC result sets to objects.


1 Answers

Multi datasources with mybatis are used in my project right now. This is an Example, add to your application.xml

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>     <property name="url" value="${center.connectionURL}"/>     <property name="username"  value="${userName}"/>     <property name="password" value="${password}"/> </bean>  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">     <property name="basePackage" value="com.xxx.dao.center"/>     <property name="sqlSessionFactoryBeanName" value="cneterSqlSessionFactory"/> </bean>  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="cneterSqlSessionFactory">     <property name="dataSource" ref="dataSource"></property>     <property name="mapperLocations" value="classpath*:mapperConfig/center/*.xml"/>     <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <!--center db end--> <!--exdb--> <bean id="dataSourceEx" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>     <property name="url" value="${ex.connectionURL}"/>     <property name="username"  value="${userName}"/>     <property name="password" value="${password}"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">     <property name="basePackage" value="com.xxx.dao.ex"/>     <property name="sqlSessionFactoryBeanName" value="exSqlSessionFactory"/> </bean> <bean id="sqlSessionFactoryEx" class="org.mybatis.spring.SqlSessionFactoryBean" name="exSqlSessionFactory">     <property name="dataSource" ref="dataSourceEx"></property>     <property name="mapperLocations" value="classpath*:mapperConfig/ex/*.xml"/>     <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean id="transactionManagerEx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     <property name="dataSource" ref="dataSourceEx"/> </bean> 
like image 82
Larry.Z Avatar answered Oct 13 '22 18:10

Larry.Z