新建实体类User
package org.hx.springboot_jdbctemplate_demo26.model;
public class User {
private Integer id;
private String name;
private String passwd;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", passwd='" + passwd + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
新建数据配置类DataSourceConfig
package org.hx.springboot_jdbctemplate_demo26.config;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.one")
DataSource dataSourceOne(){
return new HikariDataSource();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.two")
DataSource dataSourceTwo(){
return new HikariDataSource();
}
}
新建jdbc配置类JdbctemplateConfig
package org.hx.springboot_jdbctemplate_demo26.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class JdbctemplateConfig {
@Bean
JdbcTemplate jdbcTemplateOne(@Qualifier("dataSourceOne")DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean
JdbcTemplate jdbcTemplateTwo(@Qualifier("dataSourceTwo")DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
配置application.properties文件
spring.datasource.one.jdbcUrl=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
spring.datasource.one.username=root
spring.datasource.one.password=123456
spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.two.jdbcUrl=jdbc:mysql://localhost:3306/test01?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
spring.datasource.two.username=root
spring.datasource.two.password=123456
spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
在测试类中进行测试
package org.hx.springboot_jdbctemplate_demo26;
import org.hx.springboot_jdbctemplate_demo26.model.User;
import org.junit.jupiter.api.Test;
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.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
class SpringbootJdbctemplateDemo26ApplicationTests {
@Autowired
@Qualifier("jdbcTemplateOne")
JdbcTemplate jdbcTemplateOne;
@Resource(name = "jdbcTemplateTwo")
JdbcTemplate jdbcTemplatetwo;
@Test
void contextLoads() {
List<User> list1 = jdbcTemplateOne.query("select * from t1",new BeanPropertyRowMapper<>(User.class));
List<User> list2 = jdbcTemplatetwo.query("select * from user",new BeanPropertyRowMapper<>(User.class));
System.out.println(list1);
System.out.println(list2);
}
}
注意这里连接的两个数据库表字段都是相同的