mybatis 多数据源配置

mybatis-config.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="defaultStatementTimeout" value="25"/>
    </settings>

    <!-- 这个配置文件的各个配置还是有顺序要求的 typeAliases 放到最后会报错 具体顺序官网看看例子 或运行看看报错 -->
    <typeAliases>
        <typeAlias alias="tenantWithdraws" type="com.xwhb.inte.entity.TenantWithdraws"/>
        <typeAlias alias="admin" type="com.xwhb.inte.entity.Admin"/>
        <typeAlias alias="tenant" type="com.xwhb.inte.entity.Tenant"/>
        <typeAlias alias="tenantCard" type="com.xwhb.inte.entity.TenantCard"/>
        <typeAlias alias="tenantAccount" type="com.xwhb.inte.entity.TenantAccount"/>
        <typeAlias alias="tenantSecurity" type="com.xwhb.inte.entity.TenantSecurity"/>

        <!--以下是 postgres 用的-->
        <typeAlias alias="xwhBankLog" type="com.xwhb.inte.entity.XwhBankLog"/>

        <!-- <package name="com.xwhbadminwebweb.entity" /> -->
    </typeAliases>

    <environments default="development">

        <environment id="development">
            
            <transactionManager type="JDBC"/>
            
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://x.x.x.x:8066/TESTDB?useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="wuhan85123_"/>

                <property name="poolPingEnabled" value="true"/>
                <property name="poolPingQuery" value="select 1"/>
                <property name="poolPingConnectionsNotUsedFor" value="1800000"/>
            </dataSource>
        </environment>

        <environment id="postgres">

            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="org.postgresql.Driver"/>
                <property name="url" value="jdbc:postgresql://x.x.x.x:5432/postgres"/>
                <property name="username" value="postgres"/>
                <property name="password" value="wuhan85123_"/>

                <property name="poolPingEnabled" value="true"/>
                <property name="poolPingQuery" value="select 1"/>
                <property name="poolPingConnectionsNotUsedFor" value="1800000"/>
            </dataSource>
        </environment>

    </environments>


    <mappers>
        <mapper resource="mybatis/mappers/withdraws.xml"/>
        <mapper resource="mybatis/mappers/admin.xml"/>
        <mapper resource="mybatis/mappers/tenant.xml"/>
        <mapper resource="mybatis/mappers/tenantCard.xml"/>
        <mapper resource="mybatis/mappers/tenantAccount.xml"/>
        <mapper resource="mybatis/mappers/tenantSecurity.xml"/>

        <!--以下是 postgres 用的-->
        <mapper resource="mybatis/mappers/xwhBankLog.xml"/>
    </mappers>


</configuration>
复制代码
MybatisSqlSessionFactory
复制代码
package com.xwhbadminweb.config;

import java.io.InputStream;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisSqlSessionFactory {
    
    private static SqlSessionFactory sqlSessionFactory;
    private static SqlSessionFactory sqlSessionFactoryPostgres;

    public MybatisSqlSessionFactory() {

        System.out.println("反射大军");

    }

    public MybatisSqlSessionFactory(String origin) {
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream;
        try {

            System.out.println("MybatisSqlSessionFactory");

            inputStream = MybatisSqlSessionFactory.class.getClassLoader().getResourceAsStream(resource);
            if(origin.equals("mysql")){

                System.out.println("mysql+++++++++++++++++++++++++++++++++++++++++++++");
                MybatisSqlSessionFactory.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            }else if(origin.equals("postgres")){
                System.out.println("postgres==========================================");
                MybatisSqlSessionFactory.sqlSessionFactoryPostgres = new SqlSessionFactoryBuilder().build(inputStream,"postgres");
            }
        } catch (Exception e) {
            System.out.println("出问题啦");
            e.printStackTrace();
        }
    }
    
    public synchronized  static SqlSessionFactory getOne() {
        
        if (null == MybatisSqlSessionFactory.sqlSessionFactory) {
            new MybatisSqlSessionFactory("mysql");
        }
        
        return sqlSessionFactory;
        
    }
    public synchronized  static SqlSessionFactory getOnePostgres() {

        if (null == MybatisSqlSessionFactory.sqlSessionFactoryPostgres) {
            new MybatisSqlSessionFactory("postgres");
        }

        return sqlSessionFactoryPostgres;

    }

}
复制代码
以下俩个跟postgres有关
XwhBankLogService
复制代码
package com.xwhbadminweb.service;

import com.xwhb.inte.entity.XwhBankLog;
import java.util.List;

public interface XwhBankLogService {

    /**
     * 通过流水号获取
     * @param id  流水号
     * @return
     */
    public List<XwhBankLog> get(String id);

}
复制代码
XwhBankLogServiceI
复制代码
package com.xwhbadminweb.service.imp;

import com.xwhb.inte.entity.XwhBankLog;
import com.xwhbadminweb.config.MybatisSqlSessionFactory;
import com.xwhbadminweb.service.XwhBankLogService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XwhBankLogServiceI implements XwhBankLogService {

    private SqlSessionFactory sqlSessionFactory = MybatisSqlSessionFactory.getOnePostgres();

    @Override
    public List<XwhBankLog> get(String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        List<XwhBankLog> list = null;
        try {
           list = sqlSession.selectList("com.xwhbank.cn.config.mybatis.namespace.xwhBankLogMapper.get", id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (null != sqlSession){
                sqlSession.close();
            }
        }
        return list;
    }
}
复制代码

以下俩个跟myslq有关
AdminService

复制代码
package com.xwhbadminweb.service;

import com.xwhb.inte.entity.Admin;

public interface AdminService {

    /**
     * 通过 账号密码获得一个管理员
     * @param admin
     * @return
     */
    public Admin getOne(Admin admin);


}
复制代码
AdminServiceI
复制代码
package com.xwhbadminweb.service.imp;

import com.xwhb.inte.entity.Admin;
import com.xwhbadminweb.config.MybatisSqlSessionFactory;
import com.xwhbadminweb.service.AdminService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceI implements AdminService {

    private SqlSessionFactory sqlSessionFactory = MybatisSqlSessionFactory.getOne();

    @Override
    public Admin getOne(Admin admin) {

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        try {
            admin = sqlSession.selectOne("com.xwhbank.cn.config.mybatis.namespace.AdminMapper.getOne",admin);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (null != sqlSession){
                sqlSession.close();
            }
        }

        if (null == admin.getSuper_flag()){
            return null;
        }

        return admin;
    }
}
复制代码
xwhBankLog.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.xwhbank.cn.config.mybatis.namespace.xwhBankLogMapper"><!-- namespace可以自己设个 -->
  <select id="get" resultType="xwhBankLog">
    SELECT * FROM xwhbanklog WHERE no = #{id};
  </select>

</mapper>
复制代码

  admin.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.xwhbank.cn.config.mybatis.namespace.AdminMapper"><!-- namespace可以自己设个 -->
  <select id="getOne"  resultType="admin">
    select * from admin where login_name = #{login_name} and login_pass = #{login_pass};
  </select>
  
</mapper>
复制代码
顺便说下 XwhBankLog 里面接收了postgres的jsonb 对应java是Object

XwhBankLog
复制代码
package com.xwhb.inte.entity;

public class XwhBankLog {

    private String no;
    private Object mes;

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public Object getMes() {
        return mes;
    }

    public void setMes(Object mes) {
        this.mes = mes;
    }
}
复制代码

 

 
 

posted on   --LP--  阅读(801)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示