SpringBoo#Mybatis多個數據源配置,Sqlite&Mysql


第一步:
排除數據源的自動配置類:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

第二步:
定義好兩個數據源的配置文件

#mysql數據庫配置
mysql.spring.datasource.jdbc-url =jdbc:mysql://localhost/ts?characterEncoding=utf8&useSSL=true&serverTimezone=GMT%2b8
mysql.spring.datasource.username=root
mysql.spring.datasource.password=root

#sqlite數據庫的配置
sqlite.spring.datasource.url = jdbc:sqlite:C:/Users/haonan/Desktop/i-workspace/ss/src/main/resources/idatasrv.db
sqlite.spring.datasource.driver-class-name = org.sqlite.JDBC
sqlite.spring.datasource.username =
sqlite.spring.datasource.password =

#mybatis
mybatis.configuration.map-underscore-to-camel-case=true
logging.level.com.tl.ss.mapper=debug

第三步:
定義數據源Bean,注意配置文件的key要與數據源實例的屬性一一對應,否則,肯定失敗

@Bean(name = "sqliteDataSource")
@Qualifier(value = "sqliteDataSource")
@ConfigurationProperties(prefix = "sqlite.spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().type(SQLiteDataSource.class).build();
}

@Bean(name = "mysqlDataSource")
@Qualifier(value = "mysqlDataSource")
@ConfigurationProperties(prefix = "mysql.spring.datasource")
public DataSource mysqlDataSource() {
    return DataSourceBuilder.create().build();
}

第四步:
定義執行sql或者獲取mapper的sqlSessionTemplate/或者sqlSessionFactory
mybatis的基本原理是通過sqlSession去執行sql語句操作數據庫,或者通過sqlSession獲取mapper來操作數據庫[個人理解,可能不對]另外,模板構造一般需要Factory實例。
MySql使用:sqlSessionTemplate方式
Sqlite使用:sqlSessionFactory方式

代碼1MySql:

@Configuration
@MapperScan(basePackages = {"com.tl.ss.mapper.mysql"}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlSqlSessionTemplateConfig {

    @Bean(name = "mysqlSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/mysql/*.xml"));
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "mysqlSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

代碼2Sqlite:

@Configuration
@MapperScan(basePackages = {"com.tl.ss.mapper.sqlite"}, sqlSessionFactoryRef = "sqliteSqlSessionFactory")
public class SqliteSqlSessionFactoryConfig {

    @Bean(name = "sqliteSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("sqliteDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/sqlite/*.xml"));
        return bean.getObject();
    }

}

第五步:
通過工具生成代碼,可以開始干了。

其他:
代碼示例:

 

補充:

org.apache.ibatis.session.Configuration conf = new org.apache.ibatis.session.Configuration();
conf.setMapUnderscoreToCamelCase(true);
把這個conf設置到sqlSessionFactoryBean實例,開啟駝峰轉換,當然這個方式並不優雅。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM