Home  >  Article  >  Java  >  Code example of jdbctemplate running MYSQL in SpringBoot

Code example of jdbctemplate running MYSQL in SpringBoot

Y2J
Y2JOriginal
2017-05-05 14:37:241130browse

This article mainly introduces the use of jdbctemplate in Spring Boot to operate MYSQL database instances. It has certain reference value. Those who are interested can learn more.

Recently I am learning to use Spring Boot to connect to the database. Today I learned to use jdbctemplate to operate the MYSQL database. I will leave a note below

No nonsense, let’s start with the code

pom file:



 4.0.0

 test

 test

 0.0.1-SNAPSHOT

 jar

 test

 http://maven.apache.org

 

  UTF-8

 

 

  

    org.springframework.boot

    spring-boot-starter

    1.4.2.RELEASE

  

  

    org.springframework.boot

    spring-boot-starter-jdbc

    1.4.2.RELEASE

  

  

    mysql

    mysql-connector-java

    5.1.21

   

 

Configuration file: application.properties (the springboot framework uses this name by default, placed under resources)

spring.datasource.url=jdbc:mysql://localhost:3306/service_lucky_draw?autoReconnect=true&useUnicode=true&characterEncoding=utf-8

spring.datasource.username=root

spring.datasource.password=1234

spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

spring.application.name = @pom.artifactId@

server.port=33333

Startup class:

package versionUpdate;

import java.util.List;

import java.util.Map;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication

public class ApplicationMain implements CommandLineRunner {

  private Logger log = Logger.getLogger(ApplicationMain.class); 

  @Autowired

  private JdbcTemplate jdbcTemplate; 

  public static void main(String[] args) {

    SpringApplication springApplication = new SpringApplication(ApplicationMain.class);

    springApplication.run(args);

  } 

  @Override

  public void run(String... args) throws Exception {

    String queryMerchandiseInfoSql = "SELECT id,worth,channel_id,template_id FROM merchandise_info";

    List> list = jdbcTemplate.queryForList(queryMerchandiseInfoSql);

    log.debug(list);

  }

}

So far a simple SpringBoot+Jdbctemplate + The DEMO construction of MYSQL is completed;

If you don’t want to perform database operations directly in the startup class, you can follow the following method:

package versionUpdate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Component;

/** 获取jdbctemplate实例 */

@Component

public class EnterJdbcTemplate {   

  private static JdbcTemplate jdbcTemplate; 

  @Autowired

  public EnterJdbcTemplate(JdbcTemplate jdbcTemplate) {

    this.jdbcTemplate = jdbcTemplate;

  }   

  public static JdbcTemplate getJdbcTemplate(){

    return jdbcTemplate;

  }   

}
rrree

[Related recommendations]

1. Java Free Video Tutorial

2. JAVA Tutorial Manual

3. JAVA Elementary Introduction Video Tutorial

The above is the detailed content of Code example of jdbctemplate running MYSQL in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn