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&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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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】