记得@MapperScan与@Mappered的区别;
    记得@ConfigurationProperties @EnableConfigurationProperties
    新版本-蚂蚁课堂版本-SpringBoot.docx

    上课代码.zip

    我们之前配置多数据源代码:

    1. package com.itmayiedu.datasource;
    2. import javax.sql.DataSource;
    3. import org.apache.ibatis.session.SqlSessionFactory;
    4. import org.mybatis.spring.SqlSessionFactoryBean;
    5. import org.mybatis.spring.SqlSessionTemplate;
    6. import org.mybatis.spring.annotation.MapperScan;
    7. import org.springframework.beans.factory.annotation.Qualifier;
    8. import org.springframework.boot.context.properties.ConfigurationProperties;
    9. import org.springframework.boot.jdbc.DataSourceBuilder;
    10. import org.springframework.context.annotation.Bean;
    11. import org.springframework.context.annotation.Configuration;
    12. import org.springframework.context.annotation.Primary;
    13. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    14. // DataSource01
    15. @Configuration // 注册到springboot容器中
    16. @MapperScan(basePackages = "com.itmayiedu.test01", sqlSessionFactoryRef = "test1SqlSessionFactory")
    17. public class DataSource1Config {
    18. @Bean(name = "test1DataSource")
    19. @ConfigurationProperties(prefix = "spring.datasource.test1")
    20. public DataSource testDataSource() {
    21. return DataSourceBuilder.create().build();
    22. }
    23. @Bean(name = "test1SqlSessionFactory")
    24. public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
    25. throws Exception {
    26. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    27. bean.setDataSource(dataSource);
    28. // bean.setMapperLocations(
    29. // new
    30. // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
    31. return bean.getObject();
    32. }
    33. @Bean(name = "test1SqlSessionTemplate")
    34. public SqlSessionTemplate testSqlSessionTemplate(
    35. @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    36. return new SqlSessionTemplate(sqlSessionFactory);
    37. }
    38. @Bean(name = "test1TransactionManager")
    39. public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
    40. return new DataSourceTransactionManager(dataSource);
    41. }
    42. }

    应用jta+atomickos

    1. package com.itmayiedu.datasource;
    2. import java.sql.SQLException;
    3. import javax.sql.DataSource;
    4. import org.apache.ibatis.session.SqlSessionFactory;
    5. import org.mybatis.spring.SqlSessionFactoryBean;
    6. import org.mybatis.spring.SqlSessionTemplate;
    7. import org.mybatis.spring.annotation.MapperScan;
    8. import org.springframework.beans.factory.annotation.Qualifier;
    9. import org.springframework.context.annotation.Bean;
    10. import org.springframework.context.annotation.Configuration;
    11. import com.atomikos.jdbc.AtomikosDataSourceBean;
    12. import com.itmayiedu.config.DBConfig2;
    13. import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
    14. @Configuration
    15. @MapperScan(basePackages = "com.itmayiedu.test02", sqlSessionTemplateRef = "test2SqlSessionTemplate")
    16. public class MyBatisConfig2 {
    17. // 配置数据源
    18. @Bean(name = "test2DataSource")
    19. public DataSource testDataSource(DBConfig2 testConfig) throws SQLException {
    20. MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
    21. mysqlXaDataSource.setUrl(testConfig.getUrl());
    22. mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
    23. mysqlXaDataSource.setPassword(testConfig.getPassword());
    24. mysqlXaDataSource.setUser(testConfig.getUsername());
    25. mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
    26. AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
    27. xaDataSource.setXaDataSource(mysqlXaDataSource);
    28. xaDataSource.setUniqueResourceName("test2DataSource");
    29. xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
    30. xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
    31. xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
    32. xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
    33. xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
    34. xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
    35. xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
    36. xaDataSource.setTestQuery(testConfig.getTestQuery());
    37. return xaDataSource;
    38. }
    39. @Bean(name = "test2SqlSessionFactory")
    40. public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
    41. throws Exception {
    42. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    43. bean.setDataSource(dataSource);
    44. return bean.getObject();
    45. }
    46. @Bean(name = "test2SqlSessionTemplate")
    47. public SqlSessionTemplate testSqlSessionTemplate(
    48. @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    49. return new SqlSessionTemplate(sqlSessionFactory);
    50. }
    51. }