单独使用mybatis创建多数据源

单独使用mybatis连接数据库,并创建多个数据源连接(Encoding是一个DES加密工具类)

private static SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//读取properties配置文件,读取不需要后缀
ResourceBundle bundle = ResourceBundle.getBundle(mybatisConfig, new Locale("zh_CN"));
//properties配置文件中的配置名称
String configFilename = bundle.getString("configFilename");

//读取配置文件中密文密码,对密码进行解密,c创建db1数据连接
InputStream is = Resources.getResourceAsStream(mybatisConfig + ".properties");
properties.load(is);
String encodeMnpPassword = properties.getProperty("db1.password");
properties.setProperty("db1.password", Encoding.decoding(encodeMnpPassword, false));
SqlSessionFactory mnpBuild = ssfb.build(PushDataSeparation.class.getClassLoader().getResourceAsStream(mnpConfigFilename), properties);
db1Session = mnpBuild.openSession(ExecutorType.BATCH, false);

String encodeMnpPassword = properties.getProperty("db2.password");
properties.setProperty("db2.password", Encoding.decoding(encodeMnpPassword, false));
SqlSessionFactory mnpBuild = ssfb.build(PushDataSeparation.class.getClassLoader().getResourceAsStream(mnpConfigFilename), properties);
db2Session = mnpBuild.openSession(ExecutorType.BATCH, false);
SysConfigMapper sysConfigMapper = db1Session.getMapper(SysConfigMapper.class);
sysConfigMapper.selectAll();

mybatisCofig.properties

数据库1
db1.driver=oracle.jdbc.driver.OracleDriver
db1.user=USERNAME
db1.password=密文
db1.url=jdbc:oracle:thin:@127.0.0.1:1521:db1
db1.poolTimeToWait=2000
db1.poolPingQuery=select sysdate from dual
db1.poolPingEnabled=true
db1.configFilename=db1MybatisConifg.xml
数据库2
db2.driver=oracle.jdbc.driver.OracleDriver
db2.user=USERNAME
db2.password=密文
db2.url=jdbc:oracle:thin:@127.0.0.1:db1
db2.poolTimeToWait=2000
db2.poolPingQuery=select sysdate from dual
db2.poolPingEnabled=true
db2.configFilename=db2DbMybatisConifg.xml

db1MybatisConifg.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>
        <environments default="development">
                <environment id="development">
                        <transactionManager type="JDBC" />
                        <dataSource type="POOLED">
                                <property name="driver" value="${mnp.driver}" />
                                <property name="url" value="${mnp.url}"/>
                                <property name="username" value="${mnp.user}" />
                                <property name="password" value="${mnp.password}" />
                                <property name="poolPingEnabled" value="${mnp.poolPingEnabled}"/> 
                                <property name="poolTimeToWait" value="${mnp.poolTimeToWait}"/>
                                <property name="poolPingQuery" value="${mnp.poolPingQuery}" />
                        </dataSource>
                </environment>
        </environments>
        <mappers>
         <mapper resource="com/sitech/ah/fiveGMsg/mapper/MsgInfoMapper.xml" />
         <mapper resource="com/sitech/ah/fiveGMsg/mapper/BusinessConfigMapper.xml" />
        </mappers>
</configuration>

异常1:
【mybatis】=: Type interface com.sitech.ah.fiveGMsg.dao.SysConfigMapper is not known to the MapperRegistry

SysConfigMapper sysConfigMapper = db1Session.getMapper(SysConfigMapper.class);
sysConfigMapper.selectAll();

原因:
SysConfigMapper.xml未添加db1MybatisConifg.xml文件中去

异常2

### Error building SqlSession.
### The error may exist in com/sitech/ah/fiveGMsg/mapper/WhileListSynMapper.xml
### The error occurred while processing mapper_resultMap[BaseResultMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class . Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.sitech.ah.fiveGMsg.po.WhileListSyn'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.sitech.ah.fiveGMsg.po.WhileListSyn
org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/sitech/ah/fiveGMsg/mapper/WhileListSynMapper.xml
### The error occurred while processing mapper_resultMap[BaseResultMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class . Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.sitech.ah.fiveGMsg.po.WhileListSyn'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.sitech.ah.fiveGMsg.po.WhileListSyn
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:41)
	at com.sitech.ah.fiveGMsg.util.DBSqlSessionFactory.mnpSqlSessionInit(DBSqlSessionFactory.java:107)
	at com.sitech.ah.fiveGMsg.util.DBSqlSessionFactory.init(DBSqlSessionFactory.java:63)
	at com.sitech.ah.fiveGMsg.util.DBSqlSessionFactory.<clinit>(DBSqlSessionFactory.java:54)
	at com.sitech.ah.fiveGMsg.SynFileMainThread.run(SynFileMainThread.java:69)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.ibatis.builder

原因:
mapper的xml配置文件命名空间类写错,修改为正确的类路径
image

posted @ 2021-05-20 19:07  光头_强  阅读(251)  评论(0编辑  收藏  举报