接下来见证奇迹的时刻:
首先要借鉴yandype这位大神的总结的代码:
说明:
springboot2.3.x + mybatisplus + druid + sybase + mysql 多数据源整合
1、项目依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>dbs-yandype</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.21</version> </dependency> <!-- 数据库JDBC3.0连接驱动 --> <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.3.1</version> </dependency> <!-- 阿里 druid数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency> </dependencies> </project>View Code
2、配置文件 application.yml
spring: datasource: dynamic: primary: mysql-db #设置默认的数据源或者数据源组 datasource: sybase-db: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: net.sourceforge.jtds.jdbc.Driver url: jdbc:jtds:sybase://127.0.0.1:6000/TEST;charset=cp936 username: sy password: mysql-db: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test_ac?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root druid: # 连接池的配置信息 # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: falseView Code
3、mybatisplus配置类
package com.yandype.config; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.yandype.mapper") public class MybatisPlusConfig { }View Code
4、com.yandype.mapper 包下mapper接口
4.1、sybase 数据源 mapper 定义
package com.yandype.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @DS("sybase-db") public interface SybaseTestUserMapper extends BaseMapper<SybaseTestUser> { }View Code
4.2、SybaseTestUser entity实体类
package com.yandype.entity; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("TB_TEST_USER") public class SybaseTestUser { @TableId("ID") private String id; @TableField("USERNAME") private String username; @TableField("PASSWORD") private String password; @TableField("NAME") private String name; @TableField("BZ") private String bz; }View Code
4.3、mysql 数据源 mapper 定义
package com.yandype.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.mapper.BaseMapper; // @DS 切换数据源 @DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。 @DS("mysql-db") public interface MysqlTestUserAcMapper extends BaseMapper<MysqlTestUser> { }View Code
4.4、MysqlTestUser entity实体类
package com.yandype.entity; import lombok.Data; @Data public class MysqlTestUser { private Long id; private String userName; private String userPhone; }View Code
单元测试
package com.yandype.mapper; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest public class TestUserAcMapperTest{ @Autowired private SybaseTestUserMapper sybaseTestUserMapper; @Autowired private MysqlTestUserMapper mysqlTestUserMapper; // sybase 数据源测试 @Test public void sybaseSelectTest(){ List<SybaseTestUser> userList = sybaseTestUserMapper.selectList(null); System.out.println(userList); } // mysql 数据源测试 @Test public void mysqlSelectTest(){ List<MysqlTestUser> userList = mysqlTestUserMapper.selectList(null); System.out.println(userList); } }View Code
完整项目代码已提交到码云仓库:
https://gitee.com/yandype/springboot-mybats-plus-dbs
参考资料:
mybatis-plus官网文档多数据源配置:
https://baomidou.com/guide/dynamic-datasource.html
注意事项 :
数据库JDBC3.0连接驱动(net.sourceforge.jtds)
适合sybase 11 -15 版本的数据库
服务启动没问题 第一次查询查询数据为空。再次请求接口 数据时报错如下:
### Error querying database. Cause: java.sql.SQLException: SQL Anywhere Error -110: Item 'jtds000001' already existsView Code
最终经过八十一难 才找到 :
那这怎么解决呢........................?
找度娘 度爹 甚至 不惜重金 用上了chatgpt ,我也记不得问多少次了。最终找到了解决办法!
修改配置文件如下:
首先下载 jconn4.jar ,地址:
链接:https://pan.baidu.com/s/1q9GdZZqVc6Lxtp2DY_OkEQ?pwd=5211 提取码:5211View Code
在pom 引入本地 jar
<dependency> <groupId>com.sybase</groupId> <artifactId>jconn4</artifactId> <version>4.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/jconn-4.0.0.jar</systemPath> </dependency>View Code
如图:
修改配置文件如下:
spring: datasource: dynamic: primary: mysql-adb #设置默认的数据源或者数据源组 datasource: sybase-db: # type: com.alibaba.druid.pool.DruidDataSource # driver-class-name: net.sourceforge.jtds.jdbc.Driver # url: jdbc:jtds:sybase://192.168.70.71:2638/disa_cim_160021 type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.sybase.jdbc4.jdbc.SybDriver url: jdbc:sybase:Tds:127.0.0.1:5000/sss;charset=cp936 username: dba password: sql mysql-adb: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3307/aaa?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123 mysql-bdb: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3307/aaa?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123 druid: # 连接池的配置信息 # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: falseView Code
到此终于大功告成!有什么问题欢迎留言,希望能帮到你!奋青们
截图如下:
标签:jdbc,springboot,数据源,sybase,mysql,import,com From: https://www.cnblogs.com/zxy-come-on/p/17114765.html