springboot mybatis優雅的添加多數據源


springboot的原則是簡化配置,本文試圖不通過xml配置,使用configuration配置數據源,並進行簡單的數據訪問。

並且配置了多數據源,在開發過程中這種場景很容易遇到。

 

1、依賴

springboot的starter

mybatis的springboot集成包

jdbc

<dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
      <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.1.1</version>
   </dependency>
</dependencies>

 

2、在application中打開configuration

@Configuration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

3、寫主數據源的configuration

1)多數據源中有一個是主數據源,注意@primary注解的書寫位置

2)MapperScan basePackages配置了掃描主數據源mapper的路徑

3)//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/sentinel/*.xml"));

注釋掉了通過xml配置擴展sql的方式。如果頻繁使用多表連接查詢,可以打開自定義sql

package com.dqa.sentinel.configuration;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.dqa.sentinel.mapper.sentinel", sqlSessionTemplateRef  = "sentinelSqlSessionTemplate")
public class SentinelDataSource {

    @Bean(name = "sentinelData")
    @ConfigurationProperties(prefix = "spring.datasource.sentinel") // application.properteis中對應屬性的前綴
    @Primary
    public DataSource sentinelData() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sentinelSqlSessionFactory")
    @Primary
    public SqlSessionFactory sentinelSqlSessionFactory(@Qualifier("sentinelData") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/sentinel/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "sentinelTransactionManager")
    @Primary
    public DataSourceTransactionManager sentinelTransactionManager(@Qualifier("sentinelData") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sentinelSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sentinelSqlSessionTemplate(@Qualifier("sentinelSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

 

4、比如你還有一個外部數據源,再寫一個configuration

tips:這里不能有@primary注解,不然會有沖突

package com.dqa.sentinel.configuration;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.dqa.sentinel.mapper.outer", sqlSessionTemplateRef  = "outerSqlSessionTemplate")
public class OuterDataSource {

    @Bean(name = "outerData")
    @ConfigurationProperties(prefix = "spring.datasource.outer") // application.properteis中對應屬性的前綴
    public DataSource outData() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "outerSqlSessionFactory")
    public SqlSessionFactory outerSqlSessionFactory(@Qualifier("outerData") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/outer/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "outerTransactionManager")
    public DataSourceTransactionManager outerTransactionManager(@Qualifier("outerData") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "outerSqlSessionTemplate")
    public SqlSessionTemplate outerSqlSessionTemplate(@Qualifier("outerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

 

5、在mapper包中定義你需要的sql

路徑要和剛才在configuration中配置的一樣

package com.dqa.sentinel.mapper.sentinel;

import com.dqa.sentinel.model.SentinelClan;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SentinelMapper {
    @Select("SELECT * FROM sentinelClan;")
    List<SentinelClan> getAllClan();

    @Select("SELECT * FROM sentinelClan WHERE id = #{id}")
    SentinelClan getOneClan(@Param("id") Integer id);

    @Insert("INSERT INTO sentinelClan (id,clanName,topicNames,bufferTime,countWidth,countPercent,alarmGroup,status,createTime,updateTime) " +
            "VALUES( #{id}, #{clanName}, #{topicNames}, #{bufferTime}, #{countWidth}, #{countPercent}, #{alarmGroup}, #{status}, #{createTime}, #{updateTime})")
    int insertOne(SentinelClan sentinelClan);

    @Update("UPDATE sentinelClan SET clanName = #{clanName},topicNames = #{topicNames},bufferTime = #{bufferTime}," +
            "countWidth = #{countWidth},countPercent = #{countPercent},alarmGroup = #{alarmGroup},status = #{status}," +
            "createTime=#{createTime}, updateTime=#{updateTime}" +
            "WHERE id = #{id}")
    int updateOne(SentinelClan sentinelClan);
}

 

6、model中是實體類

個人愛好寫的班班程程的,或者你可以在數據庫建表之后使用generator自動生成。

package com.dqa.sentinel.model;

import java.sql.Blob;
import java.util.Date;

public class SentinelClan {
    private Integer id ;
    private String clanName;
    private String topicNames;
    private Integer bufferTime;
    private Integer countWidth;
    private Integer countPercent;
    private String alarmGroup;
    private Integer status;
    private String createTime;
    private String updateTime;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getClanName() {
        return clanName;
    }
    public void setClanName(String clanName) {
        this.clanName = clanName;
    }
    public String getTopicNames() {
        return topicNames;
    }
    public void setTopicNames(String topicNames) {
        this.topicNames = topicNames;
    }
    public Integer getBufferTime() {
        return bufferTime;
    }
    public void setBufferTime(Integer bufferTime) {
        this.bufferTime = bufferTime;
    }
    public Integer getCountWidth() {
        return countWidth;
    }
    public void setCountWidth(Integer countWidth) {
        this.countWidth = countWidth;
    }
    public Integer getCountPercent() {
        return countPercent;
    }
    public void setCountPercent(Integer countPercent) {
        this.countPercent = countPercent;
    }
    public String getAlarmGroup() {
        return alarmGroup;
    }
    public void setAlarmGroup(String alarmGroup) {
        this.alarmGroup = alarmGroup;
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public String getCreateTime() {
        return createTime;
    }
    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
    public String getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM