1.首先咱先分析需求,如下:通过java同时连接mysql和sql server数据库,使得两个数据库数据实现功能开发。(此方法不是用注解来实现的)
2.ssm框架的搭建(这个是基础,有太多大神的博客可以参考)
3.引入jar包,特别说明一下sql server的, 因为是微软的,直接从远程仓库拿不到。需先下载到本地相应文件夹下,然后在pom.xml配置
4.数据库配置文件database.properties(两个数据源写一起,省略部分配置)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.xxx.x.xxx:3306/td_oa?useUnicode=true&characterEncoding=utf-8
user=xxx
password=xxxx
db_url= jdbc:sqlserver://192.xxx.x.xxx:1433;DatabaseName=bserptest
db_driver= com.microsoft.sqlserver.jdbc.SQLServerDriver
db_username=xx
db_password=xxxx
5.配置application.xml(主要有关数据库的配置)
<context:property-placeholder location=“classpath:database.properties”
system-properties-mode=“NEVER” />
<!-- JNDI获取数据源mysql(dbcp连接池) -->
<bean id="mySqldataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--连接数据库sqlserver -->
<bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db_driver}" />
<property name="url" value="${db_url}" />
<property name="username" value="${db_username}"/>
<property name="password" value="${db_password}"/>
</bean>
<!-- 编写spring配置文件配置多数据源映射关系 -->
<bean class="cn.newkena.project.tools.DynamicDataSource" id="pooledDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="mySqldataSource" key="mysql"></entry>
<entry value-ref="sqlServerDataSource" key="mssql"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="mySqldataSource" ></property>
<!-- <property name="defaultTargetDataSource" ref="sqlServerDataSource" ></property> -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 指定数据源 -->
<property name="dataSource" ref="pooledDataSource"></property>
<property name="mapperLocations" value="classpath:dao/*.xml"/>
<property name="typeAliasesPackage" value="cn.newkena.project.pojo"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<!-- <constructor-arg name="executorType" value="BATCH"></constructor-arg> -->
</bean>
<!--============================================= -->
<!-- ===============事务控制的配置 ================-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制住数据源 -->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
6.工具类
(1)package cn.newkena.project.tools;
/**
- 动态配置多数据源
- 数据源的名称常量类
- @author
*/
public class DataSourceConst {
public static final String MYSQL="mysql";
public static final String MSSQL="mssql";
}
(2)
package cn.newkena.project.tools;
/**
- 获得和设置上下文环境 主要负责改变上下文数据源的名称
*/
public class DataSourceContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal(); // 线程本地环境
// 设置数据源类型
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
// 获取数据源类型
public static String getDataSourceType() {
return (String) contextHolder.get();
}
// 清除数据源类型
public static void clearDataSourceType() {
contextHolder.remove();
}
}
(3)
package cn.newkena.project.tools;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
- 建立动态数据源
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// 在进行DAO操作前,通过上下文环境变量,获得数据源的类型
return DataSourceContextHolder.getDataSourceType();
}
}
7.serviceimpl
package cn.newkena.project.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.newkena.project.dao.SqlMapper;
import cn.newkena.project.pojo.TestSql;
import cn.newkena.project.service.TestSqlService;
import cn.newkena.project.tools.DataSourceConst;
import cn.newkena.project.tools.DataSourceContextHolder;
@Service
public class TestSqlServiceImpl implements TestSqlService {
@Autowired
SqlMapper sqlMapper;
@Override
public List<TestSql> selectAllInfo() {
//切换新数据源
DataSourceContextHolder.setDataSourceType(DataSourceConst.MSSQL);
try {
return sqlMapper.selectAll();
} catch (Exception e) {
e.printStackTrace();
}finally {
//切回原来的数据源
DataSourceContextHolder.setDataSourceType(DataSourceConst.MYSQL);
}
return null;
//return sqlMapper.selectAll();
}
}
写到这里就差不多了。剩下的都是基础代码,希望能帮到各位
原创作品,禁止转载