最近遇到一个项目,需要访问都多个数据源,并且数据库是不同厂商(mysql,  sqlserver).

所以对此做了一些研究,这里咱们采用渐进的方式来展开,先谈谈单一数据源配置。(稍后有时间会陆续补充其他文章)

先上代码吧:

1.   database.properties 配置文件(配置多个数据源的 属性值)

 #MYSQL
mysql.jdbc.driverClassName=com.mysql.jdbc.Driver
mysql.jdbc.url=jdbc:mysql://localhost:3306/dragonfly?useUnicode=true&characterEncoding=utf-8
mysql.jdbc.username=master
mysql.jdbc.password=Master_12345
# SQL SERVER
mssql.jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver
mssql.jdbc.url=jdbc:jtds:sqlserver://192.168.1.153:1433/MES
mssql.jdbc.username=SA
mssql.jdbc.password=123456

2.   引入上述配置文件到applicationContext.xml 中

    <bean  id="configProperties"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false" />
<property name="locations">
<list>
<!-- 这里支持多种寻址方式:classpath和file -->
<value>classpath:/database.properties</value>
<value>classpath:/config.properties</value>
<!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
<!-- <value>file:/opt/demo/config/demo-message.properties</value>-->
</list>
</property>
</bean>

3.  配置datasource  /   sessionFactory  / MapperScannerConfigure

 <bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${mysql.jdbc.driverClassName}</value></property>
<property name="url"><value>${mysql.jdbc.url}</value></property>
<property name="username"><value>${mysql.jdbc.username}</value></property>
<property name="password"><value>${mysql.jdbc.password}</value></property>
</bean> <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mysqlDataSource" />
<!-- 指定sqlMapConfig总配置文件,订制的environment在spring容器中不在生效-->
<property name="configLocation" value="classpath:mybatis-config-mysql.xml"/>
<!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一个即可,
当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
<property name="mapperLocations">
<list>
<value>classpath*:/mysqlmapper/*Mapper.xml</value>
</list>
</property>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.robin.it.permission.dao" />
<!-- optional unless there are multiple session factories defined -->
<property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory" />
</bean>

注: 黄色高亮部分大家请注意,这个要和下一张将要讲的多数据源有关系。

4.    Spring + Mybatis  的分页控件配置文件: mybatis-config-mysql.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="lazyLoadingEnabled" value="false" />
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
<plugins>
<plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
<property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"/>
</plugin>
</plugins> </configuration>

至此, 单一数据源的配置就完成了! (暂时不提供源代码, 请见谅)

Spring 数据源配置一:单一数据源的更多相关文章

  1. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  2. spring+mybatis配置多个数据源

    http://www.cnblogs.com/lzrabbit/p/3750803.html

  3. Spring 数据源配置二:多数据源

    通过上一节  Spring 数据源配置一: 单一数据源  我们了解单一数据源的配置, 这里我们继续多个数据源的配置 如下(applicationContent.xml 内容) 一:  Spring   ...

  4. spring基于通用Dao的多数据源配置详解【ds1】

    spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...

  5. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  6. spring基于通用Dao的多数据源配置

    有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断.这样的方式和资源文件冲突:扫描映射文件的话,Sql ...

  7. Springboot 多数据源配置,结合tk-mybatis

    一.前言 作为一个资深的CRUD工程师,我们在实际使用springboot开发项目的时候,难免会遇到同时使用多个数据库的情况,比如前脚刚查询mysql,后脚就要查询sqlserver. 这时,我们很直 ...

  8. 在Spring中配置SQL server 2000

    前言 Lz主要目的是在Spring中配置SQL server 2000数据库,但实现目的的过程中参差着许多SQL server 2000的知识,也包罗在本文记载下来!(Lz为什么要去搞sql serv ...

  9. 搞定SpringBoot多数据源(2):动态数据源

    目录 1. 引言 2. 动态数据源流程说明 3. 实现动态数据源 3.1 说明及数据源配置 3.1.1 包结构说明 3.1.2 数据库连接信息配置 3.1.3 数据源配置 3.2 动态数据源设置 3. ...

随机推荐

  1. 配置LINUX为路由

    配置:关闭防火墙 linux1    地址1: 192.168.10.10/24 地址2:192.168.20.10/24(不指定网关,做为路由,自己就是网关) linux2    地址1: 192. ...

  2. matrix computing optimization schemes

    * stackoverflow: how does BLAS get such extern performance * Howto optimizate GEMM http://wiki.cs.ut ...

  3. 微信、微博、qq图标服务实现

    实现原理:变化前的图标和变化后的图标在一张图片上,用这张图片作为背景,通过定义背景的位置来实现显示哪个图标,其中还带着滑动的动画效果. <!DOCTYPE html> <html l ...

  4. 修改Atom 隐藏.gitignore忽略的文件/文件夹的配置

    参考链接:.gitignored files are hidden from tree view regardless of setting 假如Atom打开的文件夹有.gitignore 文件,会隐 ...

  5. 巧用selectKey

    在Ibatis中,insert()的返回值为一个Object的主键,其实这个Object的主键是这样的来的:如果在bean的xml文件中设置了插入的keyProperty,则insert()方法返回的 ...

  6. xml的语法与创建

    xml语法很简单,但很严格,如果出现错误则不能正常解析,而HTML如果出现局部的错误,照样解析 xml第一行必须写xml头<?xml version='1.0' encoding='utf8'? ...

  7. js中的null与undefined

    null undefined

  8. asp.net导入2013版本的excel问题解决

    net中导入2013excel的故障解决办法. 修改导入excel的连接字符串 string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data ...

  9. android adb:电池与电量

    手机每个硬件的耗电量是不一样的!比如屏幕就是耗电大户!其它一些元件则耗电量非常小! 使用android dumpsys工具可以获取电池以及电量消耗信息! dumpsys工具:battery.batte ...

  10. 一个layer可以跟着画完的线移动ios程序 好玩啊。

    用法:采用的是关键帧实现的. 实验目的:让上层的layer子层能够跟着在另一个子层上花的线进行移动 .即当线画完之后,图形开始移动,并且能够停在最后的那个位置 效果图: 采用是直接在layer图层上进 ...