最近在做项目迁移时,发现了并没有接触过的数据库SAP,故在此记录一下。
配置步骤:
1.引入依赖
<dependency>
<groupId>com.sap.cloud.db.jdbc</groupId>
<artifactId>ngdbc</artifactId>
<version>2.3.48</version>
</dependency>
这里的版本号与源项目一致,因为不熟,就没升级新版本,避免出现问题。
2.添加application.yml配置
初始化连接信息
spring:
datasource:
med:
medDbUser: user
medDbHost: 127.0.0.1
dbType: 1
medbDbInstance: demo
jdbcUrl: jdbc:oracle:thin:@127.0.0.1:1521/demo
sap:
jdbc-url: jdbc:sap:127.0.01:31015
username: user
password: passwd
driver-class-name: com.sap.db.jdbc.Driver
3.配置数据源
读取数据源信息,创建sql工厂
package com.demo.datasourceFactory;
import com.xxl.job.core.log.XxlJobLogger;
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.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @author Administrator
* sql会话工厂,初始化SAP数据库连接信息。
*/
@Configuration
@MapperScan(basePackages = "com.demo.sap.mapper", sqlSessionFactoryRef = "sapSqlSessionFactory")
public class SqlSessionFactory_Sap {
@Bean(name = "sapDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sap")
public DataSource getDateSourceSap() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sapSqlSessionFactory")
public SqlSessionFactory sapSqlSessionFactory(@Qualifier("sapDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/sapMappers/*.xml"));
return bean.getObject();
}
@Bean("sapSqlSessionTemplate")
public SqlSessionTemplate sapSqlSessionTemplate(
@Qualifier("sapSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}
4.开始使用
正常编写接口文件就可以了。
评论前必须登录!
注册