本文介绍了用于多个数据源的mybatis-spring java批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在春季使用以下代码来配置多个mybatis数据源.在mybatis-spring中使用Java注释和配置(无xml)的方式是什么?
I can use the following code to configure multiple mybatis datasources in spring. What is the way to do it in mybatis-spring using java annotations and configuration (No xml)?
public class DataSourceSqlSessionFactory {
private Logger logger = LoggerFactory.getLogger(getClass());
private final static String MYBATIS_CONFIG = "mybatis-config-datasource.xml" ;
public final static String AMDB_ENVIRONMENT_ID = "DB1" ;
public final static String AODB_ENVIRONMENT_ID = "DB2" ;
public SqlSessionFactory getSqlSessionFactory(String environment){
InputStream inputStream = null ;
SqlSessionFactory sqlSessionFactory = null ;
try {
inputStream = Resources.getResourceAsStream(MYBATIS_CONFIG);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream , environment);
inputStream.close();
logger.info("Get ["+environment +"] data source connection");
} catch (IOException e) {
logger.error("Get ["+environment +"] data source connection failed, error messages : " + e);
}
return sqlSessionFactory ;
}
}
推荐答案
您只需要使用@MapperScan注释注册您的映射器.但是,可以将结果映射添加到提供给SqlSessionFactoryBuilder的配置对象中.
You simply need to register your mappers with @MapperScan annotation. The result maps however can be added to the configuration object provided to the SqlSessionFactoryBuilder.
在"getSqlSessionFactory"方法中编写以下内容:
Write the following in your 'getSqlSessionFactory' method:
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration(environment);
config.addResultMap(someResultMap);
return new SqlSessionFactoryBuilder().build(config);
您已完成.享受吧!
这篇关于用于多个数据源的mybatis-spring java批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!