温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jspxcms支持多数据源吗

发布时间:2022-01-19 15:41:00 来源:亿速云 阅读:107 作者:iii 栏目:开发技术

本文小编为大家详细介绍“Jspxcms支持多数据源吗”,内容详细,步骤清晰,细节处理妥当,希望这篇“Jspxcms支持多数据源吗”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

首先要确定多数据源是指什么。

如果多数据源是指系统中的表分别放到不同数据库里(比如,栏目表cms_node放到A数据库,文章表cms_info放到B数据库),这种情况是不支持的。

如果是系统中的表放到一个数据库里,但还希望通过二次开发从其它数据库里读取一些数据,这种情况是可以的。

Jspxcms系统中使用的框架是spring-boot、spring-data-jpa。本质上说,是否支持多数据源只和这些框架有关,和系统本身无关。spring-boot官方文档里有介绍多个数据源的配置方法 https://docs.spring.io/spring-boot/docs/1.5.20.RELEASE/reference/htmlsingle/#howto-two-datasources ,网上也有大量的教程。

修改数据库连接配置

配置文件src/main/resources/application.properties。

将默认数据库配置的spring.datasource前缀改为app.datasource.first,另外再创建第二个数据源app.datasource.second。

#spring.datasource.url=jdbc:mysql://localhost/jspxcms?characterEncoding=utf8
#spring.datasource.username=root
#spring.datasource.password=password
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver

app.datasource.first.url=jdbc:mysql://localhost/jspxcms?characterEncoding=utf8
app.datasource.first.username=root
app.datasource.first.password=password
app.datasource.first.driver-class-name=com.mysql.jdbc.Driver

app.datasource.second.url=jdbc:mysql://localhost/second_database?characterEncoding=utf8
app.datasource.second.username=root
app.datasource.second.password=password
app.datasource.second.driver-class-name=com.mysql.jdbc.Driver

增加数据源配置代码

在Java配置文件中增加数据源配置代码com.jspxcms.core.Application。第二个数据源使用JdbcTemplate访问数据。

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.first")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.first")
    public DataSource dataSource() {
        return dataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    @ConfigurationProperties("app.datasource.second")
    public DataSourceProperties secondDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("app.datasource.second")
    public DataSource secondDataSource() {
        return secondDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(secondDataSource());
    }

使用范例

至此多个数据源配置完成。使用范例如下:

@Controller
public class MyController {
    @GetMapping("/second_data_source")
    public String index(HttpServletRequest request, org.springframework.ui.Model modelMap) {
        List<Map<String, Object>> data = jdbcTemplate.queryForList("select * from my_table");
        for (Map<String, Object> d : data) {
            System.out.println(d.get("my_field"));
        }
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;
}

读到这里,这篇“Jspxcms支持多数据源吗”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI