SpringBoot——多数据源-mybatis(十七)
配置文件
与jpa多数据源相同
boot数据源配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package com.qn.config.DataSourceConfig.mybatis;
import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@SpringBootConfiguration @MapperScan(basePackages = {"com.qn.dao.boot", "com.qn.mapper"}, sqlSessionTemplateRef = "sqlSessionTemplateBoot") public class BootMybatisConfig { @Autowired @Qualifier("bootDataSource") private DruidDataSource bootDataSource;
@Bean @Primary SqlSessionFactory sqlSessionFactoryBoot() { SqlSessionFactory sqlSessionFactory = null; try { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(bootDataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*Mapper.xml")); sqlSessionFactory = factoryBean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sqlSessionFactory; }
@Bean @Primary SqlSessionTemplate sqlSessionTemplateBoot() { return new SqlSessionTemplate(sqlSessionFactoryBoot()); }
@Bean @Primary DataSourceTransactionManager transactionManagerBootMybatis() { return new DataSourceTransactionManager(bootDataSource); } }
|
app数据源配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.qn.config.DataSourceConfig.mybatis;
import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@SpringBootConfiguration @MapperScan(basePackages = "com.qn.dao.app", sqlSessionTemplateRef = "sqlSessionTemplateApp") public class AppMybatisConfig {
@Autowired @Qualifier("appDataSource") private DruidDataSource appDataSource;
@Bean SqlSessionFactory sqlSessionFactoryApp() { SqlSessionFactory sqlSessionFactory = null; try { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(appDataSource); sqlSessionFactory = factoryBean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sqlSessionFactory; }
@Bean SqlSessionTemplate sqlSessionTemplateApp() { return new SqlSessionTemplate(sqlSessionFactoryApp()); }
@Bean DataSourceTransactionManager transactionManagerAppMybatis() { return new DataSourceTransactionManager(appDataSource); }
}
|
Mapper接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.qn.dao.boot;
import com.qn.eneity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Mapper public interface UserMapperAnocation {
@Select("SELECT * FROM user") List<User> getAllUsers(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.qn.dao.app;
import com.qn.eneity.Person; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Mapper public interface PersonMapperAnocation { @Select("SELECT * FROM person") List<Person> getAll(); }
|
业务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.qn.service;
import com.qn.eneity.User; import com.qn.dao.boot.UserMapperAnocation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class UserServiceAnocation { @Autowired UserMapperAnocation userMapperAnocation;
List<User> getAllUsers() { return userMapperAnocation.getAllUsers(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.qn.service;
import com.qn.dao.app.PersonMapperAnocation; import com.qn.eneity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class PersonServiceAnocation { @Autowired PersonMapperAnocation personMapperAnocation;
List<Person> getAll() { return personMapperAnocation.getAll(); } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.qn.service;
import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest @RunWith(SpringRunner.class) class UserServiceAnocationTest {
@Autowired UserServiceAnocation userServiceAnocation;
@Test void getAllUsers() { userServiceAnocation.getAllUsers() .forEach(user -> System.out.println(user.toString())); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.qn.service;
import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest @RunWith(SpringRunner.class) class PersonServiceAnocationTest { @Autowired PersonServiceAnocation personServiceAnocation;
@Test void getAll() { personServiceAnocation.getAll().forEach( person -> System.out.println(person.toString()) ); } }
|