- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解spring boot实现多数据源代码实战由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源。在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文件中配置连接参数即可。但是往往随着业务量发展,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源,下面基于之前的JdbcTemplate和Spring-data-jpa例子分别介绍两种多数据源的配置方式.
多数据源配置 。
创建一个Spring配置类,定义两个DataSource用来读取application.properties中的不同配置。如下例子中,主数据源配置为spring.datasource.primary开头的配置,第二数据源配置为spring.datasource.secondary开头的配置.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Configuration
public
class
DataSourceConfig {
@Bean
(name =
"primaryDataSource"
)
@Qualifier
(
"primaryDataSource"
)
@ConfigurationProperties
(prefix=
"spring.datasource.primary"
)
public
DataSource primaryDataSource() {
return
DataSourceBuilder.create().build();
}
@Bean
(name =
"secondaryDataSource"
)
@Qualifier
(
"secondaryDataSource"
)
@Primary
@ConfigurationProperties
(prefix=
"spring.datasource.secondary"
)
public
DataSource secondaryDataSource() {
return
DataSourceBuilder.create().build();
}
}
|
对应的application.properties配置如下:
1
2
3
4
5
6
7
8
|
spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
|
JdbcTemplate支持 。
对JdbcTemplate的支持比较简单,只需要为其注入对应的datasource即可,如下例子,在创建JdbcTemplate的时候分别注入名为primaryDataSource和secondaryDataSource的数据源来区分不同的JdbcTemplate.
1
2
3
4
5
6
7
8
9
10
|
@Bean
(name =
"primaryJdbcTemplate"
)
public
JdbcTemplate primaryJdbcTemplate(
@Qualifier
(
"primaryDataSource"
) DataSource dataSource) {
return
new
JdbcTemplate(dataSource);
}
@Bean
(name =
"secondaryJdbcTemplate"
)
public
JdbcTemplate secondaryJdbcTemplate(
@Qualifier
(
"secondaryDataSource"
) DataSource dataSource) {
return
new
JdbcTemplate(dataSource);
}
|
接下来通过测试用例来演示如何使用这两个针对不同数据源的JdbcTemplate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@RunWith
(SpringJUnit4ClassRunner.
class
)
@SpringApplicationConfiguration
(Application.
class
)
public
class
ApplicationTests {
@Autowired
@Qualifier
(
"primaryJdbcTemplate"
)
protected
JdbcTemplate jdbcTemplate1;
@Autowired
@Qualifier
(
"secondaryJdbcTemplate"
)
protected
JdbcTemplate jdbcTemplate2;
@Before
public
void
setUp() {
jdbcTemplate1.update(
"DELETE FROM USER "
);
jdbcTemplate2.update(
"DELETE FROM USER "
);
}
@Test
public
void
test()
throws
Exception {
// 往第一个数据源中插入两条数据
jdbcTemplate1.update(
"insert into user(id,name,age) values(?, ?, ?)"
,
1
,
"aaa"
,
20
);
jdbcTemplate1.update(
"insert into user(id,name,age) values(?, ?, ?)"
,
2
,
"bbb"
,
30
);
// 往第二个数据源中插入一条数据,若插入的是第一个数据源,则会主键冲突报错
jdbcTemplate2.update(
"insert into user(id,name,age) values(?, ?, ?)"
,
1
,
"aaa"
,
20
);
// 查一下第一个数据源中是否有两条数据,验证插入是否成功
Assert.assertEquals(
"2"
, jdbcTemplate1.queryForObject(
"select count(1) from user"
, String.
class
));
// 查一下第一个数据源中是否有两条数据,验证插入是否成功
Assert.assertEquals(
"1"
, jdbcTemplate2.queryForObject(
"select count(1) from user"
, String.
class
));
}
}
|
完整示例:Chapter3-2-3 。
Spring-data-jpa支持 。
对于数据源的配置可以沿用上例中DataSourceConfig的实现.
新增对第一数据源的JPA配置,注意两处注释的地方,用于指定数据源对应的Entity实体和Repository定义位置,用@Primary区分主数据源.
新增对第二数据源的JPA配置,内容与第一数据源类似,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
(
entityManagerFactoryRef=
"entityManagerFactorySecondary"
,
transactionManagerRef=
"transactionManagerSecondary"
,
basePackages= {
"com.didispace.domain.s"
})
//设置Repository所在位置
public
class
SecondaryConfig {
@Autowired
@Qualifier
(
"secondaryDataSource"
)
private
DataSource secondaryDataSource;
@Bean
(name =
"entityManagerSecondary"
)
public
EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return
entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean
(name =
"entityManagerFactorySecondary"
)
public
LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return
builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties(secondaryDataSource))
.packages(
"com.didispace.domain.s"
)
//设置实体类所在位置
.persistenceUnit(
"secondaryPersistenceUnit"
)
.build();
}
@Autowired
private
JpaProperties jpaProperties;
private
Map<String, String> getVendorProperties(DataSource dataSource) {
return
jpaProperties.getHibernateProperties(dataSource);
}
@Bean
(name =
"transactionManagerSecondary"
)
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return
new
JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
|
完成了以上配置之后,主数据源的实体和数据访问对象位于:com.didispace.domain.p,次数据源的实体和数据访问接口位于:com.didispace.domain.s.
分别在这两个package下创建各自的实体和数据访问接口 。
主数据源下,创建User实体和对应的Repository接口 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Entity
public
class
User {
@Id
@GeneratedValue
private
Long id;
@Column
(nullable =
false
)
private
String name;
@Column
(nullable =
false
)
private
Integer age;
public
User(){}
public
User(String name, Integer age) {
this
.name = name;
this
.age = age;
}
// 省略getter、setter
}
public
interface
UserRepository
extends
JpaRepository<User, Long> {
}
|
从数据源下,创建Message实体和对应的Repository接口 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Entity
public
class
Message {
@Id
@GeneratedValue
private
Long id;
@Column
(nullable =
false
)
private
String name;
@Column
(nullable =
false
)
private
String content;
public
Message(){}
public
Message(String name, String content) {
this
.name = name;
this
.content = content;
}
// 省略getter、setter
}
public
interface
MessageRepository
extends
JpaRepository<Message, Long> {
}
|
接下来通过测试用例来验证使用这两个针对不同数据源的配置进行数据操作.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@RunWith
(SpringJUnit4ClassRunner.
class
)
@SpringApplicationConfiguration
(Application.
class
)
public
class
ApplicationTests {
@Autowired
private
UserRepository userRepository;
@Autowired
private
MessageRepository messageRepository;
@Test
public
void
test()
throws
Exception {
userRepository.save(
new
User(
"aaa"
,
10
));
userRepository.save(
new
User(
"bbb"
,
20
));
userRepository.save(
new
User(
"ccc"
,
30
));
userRepository.save(
new
User(
"ddd"
,
40
));
userRepository.save(
new
User(
"eee"
,
50
));
Assert.assertEquals(
5
, userRepository.findAll().size());
messageRepository.save(
new
Message(
"o1"
,
"aaaaaaaaaa"
));
messageRepository.save(
new
Message(
"o2"
,
"bbbbbbbbbb"
));
messageRepository.save(
new
Message(
"o3"
,
"cccccccccc"
));
Assert.assertEquals(
3
, messageRepository.findAll().size());
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.cnn6.net/html/java/2017723/202385.html 。
最后此篇关于详解spring boot实现多数据源代码实战的文章就讲到这里了,如果你想了解更多关于详解spring boot实现多数据源代码实战的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在编写一个 Java 应用程序,该应用程序检查网页的源代码,并在满足源代码中的条件时在我的默认浏览器中向我显示该网页。我通过以下方式获取源代码: String source = getUrlSou
数周以来,我一直在为 Android 上的蓝牙项目而苦苦挣扎。有谁知道我可以去哪里查看 Google 用于使其蓝牙配对和连接逻辑正常工作的实际代码? 我浏览了所有的文档、BluetoothChat 应
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
Android 源代码有多个目录,其中包含针对不同设备的代码。此外,在特定目录中,存在显示不同分支和标签的路径。举个例子,在“android/platform/external/iptables”目录
在哪里可以找到 SQLMembershipProvider (.NET2.0) 的源代码? 是可用的么? 最佳答案 源代码已经发布。 See ScottGu's blog for further de
我只想知道如何下载特定版本的 Android 源代码。我已经尝试过以下命令 repo init -u https://android.googlesource.com/platform/manifes
我想看看OpenCL框架是如何实现的。我发现的只是已经编译好的可供下载的库。 当然,OpenCL 可以有许多不同的实现,但我想看看其中的一个来了解它是如何完成的。 为了确保我自己清楚,OpenCL 框
latex 源代码列表应该是什么样子才能产生像已知书籍中那样的输出,例如 Spring 框架的输出?我尝试过使用 latex 列表包,但无法生成看起来像下面一样好的东西。因此,我主要对生成类似以下示例
PHP 是用 C 语言编写的吗?我在哪里可以在线找到 PHP 源代码而无需下载全部内容? 最佳答案 PHP 函数是用 C 编写的 - 您可以在 lxr.php.net 找到可浏览的源代码. 例如:ht
我正在使用Elasticsearch OSS的官方Docker镜像(docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4),似乎完全无法使用s
我试图在Cython中同时编译C和C++源代码。这是我当前的设置: -setup.py from distutils.core import setup from Cython.Build impor
好吧,事情是这样的:你们所有人可能都在想同样的事情:您可以使用 driver.getPageSource(); 这部分是正确的。唯一的问题是源代码以一种相当奇怪的方式编译,所有代码都在其中 \&quo
由于 TwoLineListItem 自 API 17 起已被弃用,因此我已采取措施将其替换为自定义 XML 和 ViewHolder。但是,我真的希望我的应用程序看起来与使用 TwoLineList
要从 HttpURLConnection 获取 InputStream,我们的代码如下 urlConnection.getInputStream(); 如果InputStream是一个Abstract
我刚刚开始学习更多关于 C/C++ 的知识,我正在使用 Visual Studio 2013 来管理代码。 我正在使用 Tobii EyeX 眼睛注视系统的项目要求我能够稍微调整此代码,但是我不明白如
我在按钮上有一个IBAction,其中包含以下代码,我尝试使用它来检索 UIWebView 的源代码: - (IBAction)loadInAWebView:(id)sender { [self
我正在 asp.net 中创建一个网站,我只是想知道有什么方法可以使用 JavaScript 从图像生成调色板吗?类似于 1) http://www.cssdrive.com/imagepalette
有人可以分享 WinKill() from AutoIt 的源代码吗? ? 我想知道它如何处理消息(是/否/取消)以确保它得到正确处理。我想用它来清理桌面上的意外弹出窗口。 最佳答案 正如我们在下面的
我的问题与 Opencv 的源代码有关。在我看来不同的平台the Opencv website提供不同的代码结构。我只是想知道是否有可能为所有不同的平台提供一个源代码。使用相同的源代码,我可以针对不同
这个问题在这里已经有了答案: Convert Python program to C/C++ code? [closed] (8 个答案) 关闭 3 年前。 我一直在努力寻找一种方法将 .py 源文
我是一名优秀的程序员,十分优秀!