Springboot配置MySQL数据库
一、创建springboot项目,并添加如下依赖
xml
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
二、在application.properties中配置连接数据库信息
properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名称
spring.datasource.username=root
spring.datasource.password=密码
三、在启动类中创建代码,并运行测试:
java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(DemoApplication.class, args);
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
List<Map<String, Object>> result =
jdbcTemplate.queryForList("SELECT * FROM student");
System.out.println(result);
}
}
运行结果:
数据库数据: