• mybatis 多数据源


    1.配置多数据源

    spring:

    # mybatis 连接多数据源配置
    db1-mybatis:
    datasource:
    jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    db2-mybatis:
    datasource:
    jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main2?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    2.db1 配置
    package com.example.demo.mybatis.config;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.example.demo.mybatis.dbs.db1.dao", sqlSessionFactoryRef = "db1SessionFactory")
    public class DB1Config {
    
        @Primary
        @Bean(name = "db1Mybatis")
        @ConfigurationProperties(prefix = "spring.db1-mybatis.datasource")
        public DataSource db1DataSource() {
    
            return DataSourceBuilder.create().build();
        }
    
        @Primary
        @Bean(name = "db1TransactionManager")
        public DataSourceTransactionManager transactionManager(@Qualifier("db1Mybatis") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Primary
        @Bean(name = "db1SessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("db1Mybatis") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            bean.setConfiguration(configuration);
            bean.setDataSource(dataSource);
            ResourcePatternResolver rsourcePatternResolver = new PathMatchingResourcePatternResolver();
            bean.setMapperLocations(rsourcePatternResolver.getResources("com/example/demo/mybatis/dbs/db1/mapper/*.xml"));
            return bean.getObject();
        }
    
        @Primary
        @Bean(name = "db1Template")
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db1SessionFactory") SqlSessionFactory sqlSessionFactory) {
            SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
            return sqlSessionTemplate;
        }
    }
    

      3.db2配置

    package com.example.demo.mybatis.config;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.example.demo.mybatis.dbs.db2.mapper", sqlSessionFactoryRef = "db2SessionFactory")
    public class DB2Config {
    
    
        @Bean(name = "db2Mybatis")
        @ConfigurationProperties(prefix = "spring.db2-mybatis.datasource")
        public DataSource mysqlDataSource() {
    
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "db2TransactionManager")
        public DataSourceTransactionManager transactionManager(@Qualifier("db1Mybatis") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "db2SessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("db1Mybatis") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            bean.setConfiguration(configuration);
            bean.setDataSource(dataSource);
            ResourcePatternResolver rsourcePatternResolver = new PathMatchingResourcePatternResolver();
            bean.setMapperLocations(rsourcePatternResolver.getResources("classpath*:com/example/demo/mybatis/dbs/db2/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "db2Template")
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db2SessionFactory") SqlSessionFactory sqlSessionFactory) {
            SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
            return sqlSessionTemplate;
        }
    }
    

      3.实体Modle

    public class User {
    ………………
    }
    

    4.*Mapper.xml  

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.example.demo.mybatis.dbs.db1.dao.UserMapper" >
    <resultMap id="BaseResultMap" type="com.example.demo.mybatis.dbs.db1.model.User" >
    <constructor >
    <idArg column="USER_ID" jdbcType="CHAR" javaType="java.lang.String" />
    <arg column="LOGIN_NAME" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="PASSWORD" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="PHONE" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="EMAIL" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="FULL_NAME" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="AVATAR_URL" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="JOB_TITLE" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="COM_ID" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="DEPT_ID" jdbcType="CHAR" javaType="java.lang.String" />
    <arg column="ROLE_ID" jdbcType="CHAR" javaType="java.lang.String" />
    <arg column="IS_DISABLED" jdbcType="CHAR" javaType="java.lang.String" />
    <arg column="QUICK_CODE" jdbcType="VARCHAR" javaType="java.lang.String" />
    <arg column="CREATED_BY" jdbcType="CHAR" javaType="java.lang.String" />
    <arg column="CREATED_ON" jdbcType="TIMESTAMP" javaType="java.util.Date" />
    <arg column="MODIFIED_ON" jdbcType="TIMESTAMP" javaType="java.util.Date" />
    <arg column="MODIFIED_BY" jdbcType="CHAR" javaType="java.lang.String" />
    <arg column="TOKEN" jdbcType="VARCHAR" javaType="java.lang.String" />
    </constructor>
    </resultMap>

    …………
    </mapper>

    5.调用
     @Autowired
        @Resource(name = "db1Template")
        private SqlSessionTemplate sqlSessionTemplate;
    
    
        public Test getTest(Long id) {
            return sqlSessionTemplate.selectOne("com.example.demo.mybatis.dbs.db1.dao.UserMapper.getuser",id);
        }
    

      注意:

      2、3注意路径, 不需要dao层

  • 相关阅读:
    flash 观察帧频率的工具 fps counter
    flash builder 找不到所需要的AdobeFlashPlayer调试器版本
    好文好文
    win7下flash builder 4.5无法设置Courier New字体
    AS3日期工具
    png图片 透明区域如何 让其不响应鼠标事件?
    Access插入数据
    异步正则
    log4net异步刷新 TextBox (试运行版)
    Python对Xpath的支持
  • 原文地址:https://www.cnblogs.com/heshana/p/13188689.html
Copyright © 2020-2023  润新知