若依集成多数据源sqlserver

若依集成多数据源sqlserver

驱动包 放在admin模块的pom文件中

		<!--sql server驱动-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>

修改数据源配置 application-druid.yml

  # OMS数据源
            oms:
                enabled: true
                driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
                url: jdbc:sqlserver://xxx;DatabaseName=xxx;
                username: xxx
                password: xxx

其他配置修改

	# 配置检测连接是否有效
	 validationQuery: SELECT 1
	 或者直接注释掉
	 #validationQuery: SELECT 1 FROM DUAL
	 直接注释掉还要注释掉 DruidProperties文中的
	 //@Value("${spring.datasource.druid.validationQuery}")
	 //private String validationQuery;
	 /**
	* 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
*/
	// datasource.setValidationQuery(validationQuery);

当时validationQuery改成select * 一直报 “必须指定要从中选择的表。”,一直以为是sql 写错了。

增加数据源枚举类型 DataSourceType

/**
 * 数据源
 * 
 * @author ruoyi
 */
public enum DataSourceType
{
    /**
     * 主库
     */
    MASTER,

    /**
     * 从库
     */
    SLAVE,

    /**
     * OMS数据库 SQL SERVER 数据库
     */
    OMS
}

数据源读取注入 DruidConfig

  	@Bean
    @ConfigurationProperties("spring.datasource.druid.oms")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.oms", name = "enabled", havingValue = "true")
    public DataSource omsDataSource(DruidProperties druidProperties)
    {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

	@Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource)
    {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
        setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
        setDataSource(targetDataSources, DataSourceType.OMS.name(), "omsDataSource");
        return new DynamicDataSource(masterDataSource, targetDataSources);
    }
    

使用 在需要使用多数据源方法或类上添加@DataSource注解,其中value用来表示数据源

@DataSource(value = DataSourceType.SLAVE)
public List<SysUser> selectUserList(SysUser user)
{
	return userMapper.selectUserList(user);
}

@Service
@DataSource(value = DataSourceType.SLAVE)
public class SysUserServiceImpl
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot可以很方便地集成SQL Server数据库,只需要在pom.xml文件中添加相应的依赖,配置数据源和JdbcTemplate即可。 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>8.4.1.jre11</version> </dependency> ``` 2. 配置数据源 在application.properties文件中配置数据源: ``` spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydb spring.datasource.username=sa spring.datasource.password=123456 spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver ``` 其中,url中的localhost和1433分别为SQL Server的地址和端口号,databaseName为数据库名称,username和password为登录SQL Server的用户名和密码。 3. 配置JdbcTemplate 在配置类中注入数据源,并创建JdbcTemplate: ``` @Configuration public class DataSourceConfig { @Autowired private DataSource dataSource; @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource); } } ``` 4. 使用JdbcTemplate操作数据库 在需要操作数据库的类中注入JdbcTemplate,并使用其提供的方法进行数据库操作: ``` @Service public class UserService { @Autowired private JdbcTemplate jdbcTemplate; public List<User> getUsers() { String sql = "SELECT * FROM users"; return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); } } ``` 以上就是Spring Boot集成SQL Server的基本步骤。需要注意的是,SQL Server的驱动版本需要与JDK版本对应,否则会出现兼容性问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值