今天是2017年3月28日,天气晴,空闲了好几天,今天终于接受到一个任务,笼统的说是一个数据抽取吧,其中涉及到一个知识点,就是要配置多个数据源,身为一名刚毕业不到一年的菜鸟,立马去请教了组长这该如何做,组长很给力,直接给了我一个样例,让我跟着做,其中缘由是如何的呢,还需我自己查阅资料慢慢消化,在此,我也先记录一下Java配置多数据源的用法,一共需要配置四个地方,很简单。
注:这个知识点必须在对Java框架有一定了解的基础下学习才行。
第一步,是最重要的配置文件,在此我给它取名为spring-lt.xml,它是一个独立的配置文件,就是你需要配置数据源的适配器,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd ">
<description>这里设置大表远传的适配器。</description>
<bean id="sqlSessionFactoryLT" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ltDataSource"></property> <!-- ltDataSource 对应的是spring-core.xml中bean的id -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/wx/app/ygp/dao/externalConnection/*.xml"> </property> <!-- 此路径就是*mapper.xml文件对应的路径 -->
</bean>
<bean id="myLtPlugs" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.wx.app.ygp.dao.externalConnection.LtPlugs" /> <!-- 此路径就是dao接口的路径 -->
<property name="sqlSessionFactory" ref="sqlSessionFactoryLT" />
</bean>
</beans>
第二步,是在spring-core.xml中增加对这个数据源的配置,主要的作用是获取db.properties中的数据源的值,代码如下:
<!-- 加载配置属性文件 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath*:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${base.url}"/>
<property name="username" value="${base.username}"/>
<property name="password" value="${base.password}"/>
<property name="driverClassName" value="${base.driver}"/>
</bean> <!-- 数据库1 -->
<bean id="htDataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${ht.url}"/>
<property name="username" value="${ht.username}"/>
<property name="password" value="${ht.password}"/>
<property name="driverClassName" value="${ht.driver}"/>
</bean> <!-- 数据库2 -->
<bean id="ltDataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${lt.url}"/>
<property name="username" value="${lt.username}"/>
<property name="password" value="${lt.password}"/>
<property name="driverClassName" value="${lt.driver}"/>
</bean> <!-- 数据库3 -->
<bean id="multipleDataSource" class="com.wx.app.ygp.comm.MultipleDataSource">
<property name="defaultTargetDataSource" ref="dataSource"/>
<property name="targetDataSources">
<map>
<entry key="ygp" value-ref="dataSource"/>
<entry key="ht" value-ref="htDataSource"/>
<entry key="lt" value-ref="ltDataSource"/>
</map>
</property>
</bean>
第三步,是在web.xml文件中增加spring-lt.xml的引入,代码如下:
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-core.xml;
classpath:spring-mybatis.xml;
classpath:spring-ht.xml;
classpath:spring-lt.xml;
classpath:spring-task.xml
</param-value>
</context-param>
最后一步,就是在db.properties文件中配置你的数据源,像账号,密码,啥的。。
lt.driver=com.mysql.jdbc.Driver
lt.url=jdbc:mysql://127.0.0.1:3306/ygp?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
lt.username=root
lt.password=admin
完毕,这样就可以在项目中使用多个数据库啦~