配置数据源
程序员文章站
2022-06-11 08:29:13
如何配置单数据源 搭建项目 主要相关的依赖是H2 和 JDBC, 如果是别的数据库就选别的,入Mysql 查看datasource是否配置成功 启动程序控制台输出: 说明spingboot自动帮我们配置好了dataSource和jdbcTemplate. 当然也可以在配置文件中改变数据源的一些连接信 ......
如何配置单数据源
搭建项目
主要相关的依赖是h2 和 jdbc, 如果是别的数据库就选别的,入mysql
查看datasource是否配置成功
@springbootapplication
@slf4j
public class datasourcedemoapplication implements commandlinerunner {
@autowired
private datasource datasource;
@autowired
private jdbctemplate jdbctemplate;
public static void main(string[] args) {
springapplication.run(datasourcedemoapplication.class, args);
}
@override
public void run(string... args) throws exception {
showconnection();
showdata();
}
private void showconnection() throws sqlexception {
log.info(datasource.tostring());
connection conn = datasource.getconnection();
log.info(conn.tostring());
conn.close();
}
private void showdata() {
jdbctemplate.queryforlist("select * from foo")
.foreach(row -> log.info(row.tostring()));
}
}
启动程序控制台输出:
说明spingboot自动帮我们配置好了datasource和jdbctemplate.
当然也可以在配置文件中改变数据源的一些连接信息:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.hikari.maximumpoolsize=5
spring.datasource.hikari.minimumidle=5
spring.datasource.hikari.idletimeout=600000
spring.datasource.hikari.connectiontimeout=30000
spring.datasource.hikari.maxlifetime=1800000
commandlinerunner解释:在spirngboot启动完成后会执行它的run方法。
如果不是springboot项目,而是spring项目我们需要怎么配置呢?
我们需要手动地配置datasource,transactionmanager以及jdbctemplate这些bean.
@configuration
@enabletransactionmanagement
public class datasourcedemo {
@autowired
private datasource datasource;
public static void main(string[] args) throws sqlexception {
applicationcontext applicationcontext =
new classpathxmlapplicationcontext("applicationcontext*.xml");
showbeans(applicationcontext);
datasourcedemo(applicationcontext);
}
@bean(destroymethod = "close")
public datasource datasource() throws exception {
properties properties = new properties();
properties.setproperty("driverclassname", "org.h2.driver");
properties.setproperty("url", "jdbc:h2:mem:testdb");
properties.setproperty("username", "sa");
return basicdatasourcefactory.createdatasource(properties);
}
@bean
public platformtransactionmanager transactionmanager() throws exception {
return new datasourcetransactionmanager(datasource());
}
private static void showbeans(applicationcontext applicationcontext) {
system.out.println(arrays.tostring(applicationcontext.getbeandefinitionnames()));
}
private static void datasourcedemo(applicationcontext applicationcontext) throws sqlexception {
datasourcedemo demo = applicationcontext.getbean("datasourcedemo", datasourcedemo.class);
demo.showdatasource();
}
public void showdatasource() throws sqlexception {
system.out.println(datasource.tostring());
connection conn = datasource.getconnection();
system.out.println(conn.tostring());
conn.close();
}
}
所以可以看得出springboot帮我们做了很多的配置bean的工作,如果我们没有配置,它会帮我们配置,如果我们自己配置了,那就不会自动配置了
如何配置多数据源
配置多数据源的注意事项
不同数据源的配置要分开
关注每次使⽤用的数据源
• 有多个datasource时系统如何判断
• 对应的设施(事务、orm等)如何选择datasource
spring boot中的多数据源配置
⼿手⼯工配置两组 datasource 及相关内容
与spring boot协同⼯工作(⼆选一)
• 配置@primary类型的bean
• 排除spring boot的⾃自动配置
• datasourceautoconfiguration
• datasourcetransactionmanagerautoconfiguration
• jdbctemplateautoconfiguration
package geektime.spring.data.multidatasourcedemo;
import lombok.extern.slf4j.slf4j;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration;
import org.springframework.boot.autoconfigure.jdbc.datasourceproperties;
import org.springframework.boot.autoconfigure.jdbc.datasourcetransactionmanagerautoconfiguration;
import org.springframework.boot.autoconfigure.jdbc.jdbctemplateautoconfiguration;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.jdbc.datasource.datasourcetransactionmanager;
import org.springframework.transaction.platformtransactionmanager;
import javax.annotation.resource;
import javax.sql.datasource;
@springbootapplication(exclude = { datasourceautoconfiguration.class,
datasourcetransactionmanagerautoconfiguration.class,
jdbctemplateautoconfiguration.class})
@slf4j
public class multidatasourcedemoapplication {
public static void main(string[] args) {
springapplication.run(multidatasourcedemoapplication.class, args);
}
@bean
@configurationproperties("foo.datasource")
public datasourceproperties foodatasourceproperties() {
return new datasourceproperties();
}
@bean
public datasource foodatasource() {
datasourceproperties datasourceproperties = foodatasourceproperties();
log.info("foo datasource: {}", datasourceproperties.geturl());
return datasourceproperties.initializedatasourcebuilder().build();
}
@bean
@resource
public platformtransactionmanager footxmanager(datasource foodatasource) {
return new datasourcetransactionmanager(foodatasource);
}
@bean
@configurationproperties("bar.datasource")
public datasourceproperties bardatasourceproperties() {
return new datasourceproperties();
}
@bean
public datasource bardatasource() {
datasourceproperties datasourceproperties = bardatasourceproperties();
log.info("bar datasource: {}", datasourceproperties.geturl());
return datasourceproperties.initializedatasourcebuilder().build();
}
@bean
@resource
public platformtransactionmanager bartxmanager(datasource bardatasource) {
return new datasourcetransactionmanager(bardatasource);
}
}
上一篇: JDBC begin failed
下一篇: 选哪一个