MySql教程

Mysql配置多数据源

本文主要是介绍Mysql配置多数据源,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<dependency>
            <groupId>xin.altitude.cms</groupId>
            <artifactId>ucode-cms-db-datasource</artifactId>
            <version>1.5.5</version>
        </dependency>
spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.10.200:3306/db_dispathcher_dev_ice?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    username: root
    password: Aa123456
    druid:
      slave:
        enabled: true
        url: jdbc:mysql://192.168.10.200:3306/db_dev_alarm_service?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
        username: root
        password: Aa123456
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.icealarm.mapper.AlarmRecordMapper;
import com.hz.icealarm.pojo.AlarmRecordEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xin.altitude.cms.db.datasource.annotation.Ds;
import xin.altitude.cms.db.datasource.enums.DataSourceType;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2020-05-13
 */
@Service
public class IAlarmRecordServiceImpl extends ServiceImpl<AlarmRecordMapper, AlarmRecordEntity> implements IAlarmRecordService {

    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    @Override
    @Ds(value = DataSourceType.SLAVE)
    public List<AlarmRecordEntity> findAll() {
        return alarmRecordMapper.findAll();
    }

    @Override
    @Ds(value = DataSourceType.SLAVE)
    public AlarmRecordEntity findOne(Long deviceId, String code) {
        QueryWrapper<AlarmRecordEntity> wrapper = new QueryWrapper<>();
        QueryWrapper<AlarmRecordEntity> queryWrapper = wrapper.eq("device_id", deviceId).eq("alarm_type", code)
                .isNull("alarm_handle_time").orderByDesc("alarm_time");
        List<AlarmRecordEntity> alarmRecord = alarmRecordMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(alarmRecord)) {
            return null;
        }
        return alarmRecord.stream().findFirst().get();
    }

    @Override
    @Ds(value = DataSourceType.SLAVE)
    public boolean save(AlarmRecordEntity alarmRecord) {
        return super.save(alarmRecord);
    }

    @Override
    @Ds(value = DataSourceType.SLAVE)
    public boolean update(AlarmRecordEntity alarmRecord) {
        QueryWrapper<AlarmRecordEntity> wrapper = new QueryWrapper<>();
        QueryWrapper<AlarmRecordEntity> updateWrapper = wrapper.eq("id", alarmRecord.getId());
        return super.update(alarmRecord, updateWrapper);
    }
}

 

这篇关于Mysql配置多数据源的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!