首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot整合JdbcTemplate连接Mysql

SpringBoot整合JdbcTemplate连接Mysql

作者头像
JQ实验室
发布2022-02-09 17:50:51
发布2022-02-09 17:50:51
5730
举报
文章被收录于专栏:实用技术实用技术
代码语言:javascript
复制
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"

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-02-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档