ssm+maven+mysql+sql server多数据源使用(新手上路,欢迎大神留言指教)

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();
}

}
写到这里就差不多了。剩下的都是基础代码,希望能帮到各位
原创作品,禁止转载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值