被多数据源折腾晕了 为了让自己记住写下这篇博客
第一步:配置 application.properties
server.port=8081
server.session-timeout=1000000
server.context-path=/
spring.thymeleaf.cache=false
server.tomcat.uri-encoding=UTF-8
server.tomcat.compression=on
#ccs第一个数据源
spring.datasource.ccs.url=jdbc:oracle:thin:@xx.x.x.xxx/kf
spring.datasource.ccs.username=xxxx
spring.datasource.ccs.password=xxx
spring.datasource.ccs.driver-class-name=oracle.jdbc.driver.OracleDriver
#bssp第二个数据源
spring.datasource.bssp.url=jdbc:oracle:thin:@xx.x.x.xxx/kf
spring.datasource.bssp.username=1111
spring.datasource.bssp.password=2222
spring.datasource.bssp.driver-class-name=oracle.jdbc.driver.OracleDriver
第二步:数据源配置:分别配置两个
creatdasourcebssp.java @Primary为主数据源
package org.sang.controller;
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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "org.sang.mapper.bssp", sqlSessionTemplateRef = "bsspSqlSessionTemplate")
public class Creatdasourcebssp {
@Primary
@Bean(name = "bsspDataSource")
@ConfigurationProperties(prefix = "spring.datasource.bssp")
public DataSource bsspDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "bsspSqlSessionFactory")
public SqlSessionFactory bsspSqlSessionFactory(@Qualifier("bsspDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:org/sang/mapper/bssp/*.xml"));
return bean.getObject();
}
@Primary
@Bean(name = "bsspTransactionManager")
public DataSourceTransactionManager bsspTransactionManager(@Qualifier("bsspDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "bsspSqlSessionTemplate")
public SqlSessionTemplate bsspSqlSessionTemplate(@Qualifier("bsspSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Primary 定义主数据源
Creatdasourceccs.java
package org.sang.controller;
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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "org.sang.mapper.ccs", sqlSessionTemplateRef = "ccsSqlSessionTemplate")
public class Creatdasourceccs {
@Bean(name = "ccsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ccs")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "ccsSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ccsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:org/sang/mapper/ccs/*.xml"));
return bean.getObject();
}
@Bean(name = "ccsTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("ccsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "ccsSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ccsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第三步:配置mapper.配置两个mapper下面就列出一个:bssp的mapper 这两个最好放在同一个目录下:
UsersMapper.java
package org.sang.mapper.bssp;
import org.sang.bean.Bsspbusiness;
public interface UsersMapper {
public Bsspbusiness findMisdnByBusinessId(int business_id);}
UsersMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.sang.mapper.bssp.UsersMapper">
<resultMap id="BaseResultMap" type="org.sang.bean.Bsspbusiness">
<result column="business_id" property="business_id" />
<result column="show_name" property="show_name" />
<result column="group_id" property="group_id" />
<result column="type" property="type" />
<result column="status" property="status" />
<result column="inure_date" property="inure_date" />
<result column="expire_date" property="expire_date" />
<result column="description" property="description" />
</resultMap>
<parameterMap id="users" type="org.sang.bean.Bsspbusiness" />
<sql id="BaseColumnList">
business_id,show_name, group_id, group_id, type, status, inure_date, expire_date, description
</sql>
<select id="findMisdnByBusinessId" resultMap="BaseResultMap" parameterType="int">
select
<include refid="BaseColumnList" />
from bssp_business
where business_id = #{business_id}
</select>
</mapper>
Indexcontroller.java 验证数据源是否联通
package org.sang.controller;
import org.mybatis.spring.annotation.MapperScan;
import org.sang.bean.Bsspbusiness;
import org.sang.bean.Ib2cmcTransformCfg;
import org.sang.mapper.bssp.UsersMapper;
import org.sang.mapper.ccs.Ib2cmcTransformCfgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@EnableAutoConfiguration
@MapperScan(basePackages = "org.sang.mapper")
@ComponentScan(value = "org.sang.mapper")
public class Indexcontroller {
@Autowired
private UsersMapper mapper;
@Autowired
private Ib2cmcTransformCfgMapper ib2cmcTransformCfgMapper;
@Controller
public class GreetingController {
//用两个页面跳转
@GetMapping("/bsspbusiness")
@RequestMapping("/")
public String index(Model model) {
//验证bssp
model.addAttribute("bsspbusiness", new Bsspbusiness());
Bsspbusiness bsspbusiness1 = mapper.findMisdnByBusinessId(19092064);
model.addAttribute("bsspbusiness1", bsspbusiness1);
System.out.println(bsspbusiness1+":"+bsspbusiness1.getGroup_id());
//验证ccs
Ib2cmcTransformCfg ib2cmcTransformCfg =ib2cmcTransformCfgMapper.findMisdnBySysfuncid(19092064);
model.addAttribute("ib2cmc_transform_cfg", ib2cmcTransformCfg);
System.out.println(ib2cmcTransformCfg+":"+ib2cmcTransformCfg.getOut_rule());
return "index";}
}
当启动成功并看到打印信息就算是成功连接了 整个项目的目录如下图
由于我是直接用thymeleaf模板 因此Index页面一定要放在templates下
注意:由于一开始我将Indexcontroller的内容写在Test20Thymeleaf1Application.java里导致出现了Cannot determine embedded database driver class for database type NONE的问题这个问题耽误了我很久,后来抱着试一下的态度将这两个文件分开就解决了,具体问题我还是没能搞清楚 ,如果有人知道了可以告诉我一下谢谢,第一次写博客有点粗糙不要介意。