- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.32version>
- dependency>
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>dynamic-datasource-spring-boot-starterartifactId>
- <version>3.5.2version>
- dependency>
-
- <dependency>
- <groupId>com.dmgroupId>
- <artifactId>DmJdbcDriverartifactId>
- <version>1.8.0version>
- dependency>
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.5.3.2version>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- spring:
- datasource:
- dynamic:
- druid:
- initial-size: 5
- min-idle: 5
- maxActive: 20
- # 配置获取连接等待超时的时间
- maxWait: 60000
- # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
- timeBetweenEvictionRunsMillis: 6000
- # 配置一个连接在池中最小生存的时间,单位是毫秒
- minEvictableIdleTimeMillis: 60000
- # 配置一个连接在池中最大生存的时间,单位是毫秒
- maxEvictableIdleTimeMillis: 900000
- validationQuery: SELECT 1 FROM DUAL
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- # 打开PSCache,并且指定每个连接上PSCache的大小
- poolPreparedStatements: true
- maxPoolPreparedStatementPerConnectionSize: 20
- primary: dm #设置默认的数据源或者数据源组,默认值即为dm
- strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
- datasource:
- dm:
- driver-class-name: dm.jdbc.driver.DmDriver
- url: jdbc:dm://localhost:5236
- username: xxxx
- password: xxxxxxxxx
- mysql:
- driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
- url: jdbc:mysql://localhost:3306/lps?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
- username: xxxxx
- password: xxxxxxxxxxx
- @Data
- public class CfdData implements Serializable {
- /**
- *
- */
- @TableField(value = "TIME")
- private LocalDateTime time;
-
- /**
- *
- */
- @TableField(value = "VAL")
- private String val;
-
- @TableField(exist = false)
- private static final long serialVersionUID = 1L;
- }
- @Mapper
- public interface CfdDataMapper extends BaseMapper
{ -
- void insertForMysql(CfdData cfdData);
-
- Long selectCountFromMysql();
-
- List
selectFromDm(); -
- }
-
"insertForMysql"> - INSERT INTO CFD_DATA (TIME, VAL)
- VALUES (#{time}, #{val});
-
-
- SELECT * FROM LPS.CFD_DATA
-
-
- SELECT COUNT(1) FROM CFD_DATA
-
- /**
- * @author 阿水
- * @ClassName CfdDataMySql
- * @description: TODO
- * @date 2023年10月21日
- * @version: 1.0
- */
- public interface CfdDataMySqlService extends IService
{ -
- Long insertForMysql(List
cfdData) ; - Long selectCountFromMysql();
- }
- /**
- * @author 19449
- * @description 针对表【CFD_DATA】的数据库操作Service
- * @createDate 2023-10-21 23:32:05
- */
- public interface CfdDataService extends IService
{ - List
selectFromDm(); - }
- @Service
- @DS("mysql")
- public class CfdDataMySqlServiceImpl extends ServiceImpl
- implements CfdDataMySqlService {
- @Resource
- private SqlSessionFactory sqlSessionFactory;
- @Resource
- private CfdDataMapper cfdDataMapper;
- @Override
- public Long insertForMysql(List
cfdData) { - SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
- try{
- CfdDataMapper mapper = session.getMapper(CfdDataMapper.class);
- cfdData.forEach(item->{
- mapper.insertForMysql(item);
- });
- session.commit();
- }
- finally {
- session.close();
- }
- return (long) cfdData.size();
- }
-
- @Override
- public Long selectCountFromMysql() {
- return cfdDataMapper.selectCountFromMysql();
- }
- }
- /**
- * @author 19449
- * @description 针对表【CFD_DATA】的数据库操作Service实现
- * @createDate 2023-10-21 23:32:05
- */
- @Service
- @DS("dm")
- public class CfdDataServiceImpl extends ServiceImpl
- implements CfdDataService {
- @Autowired
- private CfdDataMapper cfdDataMapper;
-
- @Override
- public List
selectFromDm() { - return cfdDataMapper.selectFromDm();
- }
- }
-
- @SpringBootTest
- class JdbcTemplateDemo1ApplicationTests {
- //达梦数据库
- @Resource
- CfdDataService dmService;
- //mysql数据库
- @Resource
- CfdDataMySqlService mySqlService;
-
- @Test
- void contextLoads() {
- List
cfdData = dmService.selectFromDm(); - for (CfdData cfdDatum : cfdData) {
- System.out.println(cfdDatum);
- }
- System.out.println("本次DM数据数据条数为"+cfdData.size()+"条");
- Long countFromMysql = mySqlService.selectCountFromMysql();
- System.out.println("mysql当前条数为"+countFromMysql+"条");
- System.out.println("开始插入");
- mySqlService.insertForMysql(cfdData);
- System.out.println("插入完成");
- Long countAfterInsert = mySqlService.selectCountFromMysql();
- System.out.println("mysql插入完成后当前条数为"+countAfterInsert+"条");
- Long count = mySqlService.getBaseMapper().selectCount(null);
- /**
- * 这个走的是默认
- * primary: dm #设置默认的数据源或者数据源组,默认值即为dm
- */
- System.out.println("mybatisplus查询出来的count为"+count+"条");
- }
-
- }


| 注解 | 结果 |
|---|---|
| 没有@DS | 默认数据源 |
| @DS("dsName") | dsName可以为组名也可以为具体某个库的名称 |