Spring Data Source Configuration one: Single data source

Source: Internet
Author: User
Tags mssql

Recently encountered a project that requires access to multiple data sources, and the database is a different vendor (MySQL, SQL Server).

So we've done some research on this, and we're going to start with a gradual approach to the single data source configuration. (Additional articles will be added later)

First, the code:

1. database.properties configuration file (Configure attribute values for multiple data sources)

1 #MYSQL2Mysql.jdbc.driverclassname=Com.mysql.jdbc.Driver3Mysql.jdbc.url=jdbc:mysql://Localhost:3306/dragonfly?useunicode=true&characterencoding=utf-84Mysql.jdbc.username=Master5mysql.jdbc.password=master_123456 # sql SERVER7Mssql.jdbc.driverclassname=Net.sourceforge.jtds.jdbc.Driver8Mssql.jdbc.url=jdbc:jtds:sqlserver://192.168.1.153:1433/mes9Mssql.jdbc.username=SATenmssql.jdbc.password=123456

2. Introduce the above configuration file into Applicationcontext.xml

    <bean  id= "Configproperties"  class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">        <property name=" Ignoreresourcenotfound "value=" false "/>        <property name=" Locations ">            <list>                <!-- Multiple addressing modes are supported: Classpath and file--                 <value>classpath:/database.properties</value>                 <value >classpath:/config.properties</value>                  <!--is recommended by using file, so that you can detach the configuration and code--               <!--< value>file:/opt/demo/config/demo-message.properties</value>-->            </list>        </ Property>    </bean>

3. Configure 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"/> & lt;! --Specify Sqlmapconfig general profile, custom environment not valid in spring container--<property name= "configlocation" value= "Classpath:mybatis-config-mysql.xml"/><!--Specify the Entity class mapping file, you can specify that both a package and all configuration files under the child package are specified, mapperlocations and configlocation have one, and when you need to specify an alias for an entity class, you can specify the Configlocation property. Then introduce the Entity class mapping file in the MyBatis general configuration file using Mapper--<property name= "Mapperlocations" > <list><value>classpath*:/mysqlmapper/*  mapper.xml</value> </list> </property> </bean> <bean CLA ss= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "Basepackage" value= "       Com.robin.it.permission.dao "/> <!--optional unless there is multiple session factories defined- <property name= "Sqlsessionfactorybeanname" value= "mysqlsessionfactory"/> </bean> 

Note: Yellow highlighting section Please note that this is related to the next multi-data source to be told.

4. Spring + Mybatis Paging control configuration file: Mybatis-config-mysql.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configuration    " -//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>

At this point, the configuration of a single data source is complete! (The source code is not available for the time being, please forgive me)

Spring Data Source Configuration one: Single data source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.