2, configure a number of data sources (H2 database). 3, configure a number of data sources (Mysql database). 【 attach source 】Spring Boot actual combat series – four, log management 【 attach source 】Spring Boot actual combat series – five, transaction abstraction 【 attach source 】Spring Boot actual combat series – six, currency storage database processing
Introducing H2 dependencies
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Copy the code
Create tables and data
When the H2 database is started, schema. SQL and data. SQL in the resource folder are executed by default
schema.sql
create table foo(id int identity, value varchar(64));
Copy the code
data.sql
insert into foo(id, value) values(1.'one');
insert into foo(id, value) values(2.'two');
Copy the code
The core code
* View database connection information * View table dataCopy the code
@SpringBootApplication
@Slf4j
public class SpringDemoApplication implements CommandLineRunner {
@Autowired
private DataSource dataSource;
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
@Override
public void run(String. args) throws Exception { showConnection(); showData(); }/** * Prints database connection information *@throws Exception* /
private void showConnection() throws Exception{
log.info("The dataSource =" +dataSource.toString());
Connection conn = dataSource.getConnection();
log.info("Connection=" + conn.toString());
conn.close();
}
/** * display data */
private void showData(){
jdbcTemplate
.queryForList("select * from foo")
.forEach(row -> log.info(row.toString()));
Copy the code
The results
Source code download address
Github.com/qiulanzhu/S…