网上很多教程的配置比较老,并且不支持多数据源。踩坑后记录下,看了下MybatisPlus在github中提问非常不优化,文档也没有标注这块,只能看了下源码。
(1).首先需要加载MybatisPlus的多数据源组件,maven坐标如下
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
(2).配置多数据库src/main/resources/application.yml
spring:
datasource:
dynamic:
primary: ds1 #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
ds1: # X2会员档案
url: jdbc:oracle:thin:@152.80.196.29:1523:orcl
username: a_gls_10247622
password: 123456
driver-class-name: oracle.jdbc.driver.OracleDriver
ds2: # Erp数据库
url: jdbc:mysql://181.179.241.251:3309/ellassay_erp
username: gaojiufeng
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: false
(3).分页组件配置src/main/java/com/ellassay/x2/sync/config/MybatisPlusConfig.java
package com.ellassay.x2.sync.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
切记此处不需要设置DB_TYPE,切记此处不需要设置DB_TYPE,切记此处不需要设置DB_TYPE!!!
(4).查询代码如下:
// 分页设置
Page<X2UserEntity> x2Page = new Page<>(i, pageSize);
// 查询分页结果
Page<X2UserEntity> x2UserPage = x2UserMapper.selectPage(x2Page, queryWrapper);
List<X2UserEntity> x2UserList = x2UserPage.getRecords();
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.ellassay.x2.sync.mapper.ErpForX2UserMapper">
<insert id="replaceInto">
replace into fa_x2_source_vip_user (
vip_id,
card_id,
grade_id,
grade_name,
depot_id,
depot_name,
open_depotid,
open_depot_name,
extend_user_id,
extend_user_name,
online_extend_user_id,
online_extend_user_name,
open_user_id,
open_user_name,
open_date,
vip_score,
vip_purse,
last_upgrade_date,
last_degrade_date,
vip_name,
vip_sex_id,
vip_sex_name,
vip_mobile,
vip_birthday_year,
vip_birthday_month,
vip_birthday_day,
vip_birth_date,
vip_birth_type_id,
vip_birth_type_name,
vip_idcode,
vip_email,
vip_qq,
vip_avatar_url,
vip_unionid,
vip_channel_id,
vip_channel_name,
vip_remark,
company_id,
depot_group_id,
create_name,
create_date,
update_name,
update_date,
wx_union_id,
vip_nick_name,
vip_outside_id,
fans_id,
vip_wechat_id,
vip_lose_id,
vip_lose_Name,
vip_activation_date,
vip_invite_id,
vip_recycle_id,
vip_rovince_id,
vip_rovince_name,
vip_city_id,
vip_city_name,
vip_area_id,
vip_area_name,
vip_address,
vip_wedding_day,
vip_degrade_batch_id,
vip_degrade_batch_date,
vip_degrade_check_date,
vip_bitth_task_runyear,
vip_bitth_sms_runyear,
vip_system_type
) values
<foreach collection="userList" item="user" separator=",">
(
#{user.vipId},
#{user.cardId},
#{user.gradeId},
#{user.gradeName},
#{user.depotId},
#{user.depotName},
#{user.openDepotId},
#{user.openDepotName},
#{user.extendUserId},
#{user.extendUserName},
#{user.onlineExtendUserId},
#{user.onlineExtendUserName},
#{user.openUserId},
#{user.openUserName},
#{user.openDate},
#{user.vipScore},
#{user.vipPurse},
#{user.lastUpgradeDate},
#{user.lastDegradeDate},
#{user.vipName},
#{user.vipSexId},
#{user.vipSexName},
#{user.vipMobile},
#{user.vipBirthdayYear},
#{user.vipBirthdayMonth},
#{user.vipBirthdayDay},
#{user.vipBirthDate},
#{user.vipBirthTypeId},
#{user.vipBirthTypeName},
#{user.vipIdCode},
#{user.vipEmail},
#{user.vipQQ},
#{user.vipAvatarUrl},
#{user.vipUnionId},
#{user.vipChannelId},
#{user.vipChannelName},
#{user.vipRemark},
#{user.companyId},
#{user.depotGroupId},
#{user.createName},
#{user.createDate},
#{user.updateName},
#{user.updateDate},
#{user.wxUnionId},
#{user.vipNickName},
#{user.vipOutsideId},
#{user.fansId},
#{user.vipWechatId},
#{user.vipLoseId},
#{user.vipLoseName},
#{user.vipActivationDate},
#{user.vipInviteId},
#{user.vipRecycleId},
#{user.vipProvinceId},
#{user.vipProvinceName},
#{user.vipCityId},
#{user.vipCityName},
#{user.vipAreaId},
#{user.vipAreaName},
#{user.vipAddress},
#{user.vipWeddingDay},
#{user.vipDegradeBatchId},
#{user.vipDegradeBatchDate},
#{user.vipDegradeCheckDate},
#{user.vipBirthTaskRunYear},
#{user.vipBirthSmsRunYear},
#{user.vipSystemType}
)
</foreach>
</insert>
</mapper>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。