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);
    }
}

 

posted @ 2022-06-30 17:58  蔡徐坤1987  阅读(566)  评论(0编辑  收藏  举报