SpringBoot整合JdbcTemplate连接Mysql
Posted 凉城
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合JdbcTemplate连接Mysql相关的知识,希望对你有一定的参考价值。
import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; @SpringBootApplication public class Application { public static void main(String[] args) throws IOException { SpringApplication.run(Application.class, args); } @Bean public JdbcTemplate jdbcTemplate() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test"); dataSource.setUsername("root"); dataSource.setPassword("123456"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } }
添加依赖:
apply plugin: ‘org.springframework.boot‘
apply plugin: ‘io.spring.dependency-management‘
springBoot {
executable = true
mainClass = ‘com.test.Application‘
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:${verSpringBoot}"
compile "mysql:mysql-connector-java:5.1.21"
}
以上是关于SpringBoot整合JdbcTemplate连接Mysql的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合JdbcTemplate连接Mysql
SpringBoot3(springboot_jdbctemplate以及MyBatis和Dubbo整合)