首页>代码>Spring Boot学习(七)之Web应用使用jdbctemplate多数据源配置​博客源码>/springbootstudy-demo7-jdbctemplate-more/src/test/java/com/xiaojingg/SpringbootstudyDemo7JdbctemplateMoreApplicationTests.java
package com.xiaojingg;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootstudyDemo7JdbctemplateMoreApplicationTests {

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

	}

}
最近下载更多
zaizai21312  LV10 2020年6月12日
start111  LV9 2019年9月10日
1jj15j  LV6 2019年8月2日
低调人  LV38 2019年2月23日
wangwenyi  LV6 2018年10月19日
lwpwork  LV16 2018年8月27日
sinbero  LV8 2018年4月27日
故事_sun  LV26 2018年4月20日
miracle1228  LV8 2018年2月28日
最代码官方  LV167 2017年11月21日
最近浏览更多
漫步的海星  LV4 2023年9月26日
crazy11crazy  LV30 2023年6月5日
王乐22222  LV10 2022年5月15日
你算哪块小鱼干  LV8 2021年4月22日
水光浮藻  LV6 2021年3月25日
lyn520  LV3 2021年1月5日
丶空城过客  LV12 2020年6月25日
xiaoche117  LV17 2020年6月19日
zaizai21312  LV10 2020年6月12日
yyq559  LV2 2020年6月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友