欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫(kù)實(shí)例

 更新時(shí)間:2017年04月19日 16:45:42   作者:Mr.ning  
本篇文章主要介紹了Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫(kù)實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。

最近在學(xué)習(xí)使用Spring Boot連接數(shù)據(jù)庫(kù),今天學(xué)習(xí)了使用jdbctemplate 操作MYSQL數(shù)據(jù)庫(kù),下面就留個(gè)筆記

不廢話,先來(lái)代碼

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 

 <groupId>test</groupId>

 <artifactId>test</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 <packaging>jar</packaging>

 

 <name>test</name>

 <url>http://maven.apache.org</url>

 

 <properties>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 </properties>

  

 <dependencies>

  <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter</artifactId>

    <version>1.4.2.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jdbc</artifactId>

    <version>1.4.2.RELEASE</version>

  </dependency>

   

  <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>5.1.21</version>

   </dependency>

  

 </dependencies>

</project> 

配置文件:application.properties(springboot框架默認(rèn)使用這個(gè)名字,放在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 

啟動(dòng)類:

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<Map<String, Object>> list = jdbcTemplate.queryForList(queryMerchandiseInfoSql);

    log.debug(list);

  }

}

至此一個(gè)簡(jiǎn)單的SpringBoot+Jdbctemplate+MYSQL的DEMO搭建完成;

如果不想在啟動(dòng)類里面直接進(jìn)行數(shù)據(jù)庫(kù)操作,可以按照下面的方式:

package versionUpdate;

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

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Component;

/** 獲取jdbctemplate實(shí)例 */

@Component

public class EnterJdbcTemplate {   

  private static JdbcTemplate jdbcTemplate; 

  @Autowired

  public EnterJdbcTemplate(JdbcTemplate jdbcTemplate) {

    this.jdbcTemplate = jdbcTemplate;

  }   

  public static JdbcTemplate getJdbcTemplate(){

    return jdbcTemplate;

  }   

} 
package versionUpdate;

import org.springframework.jdbc.core.JdbcTemplate;

/** 操作數(shù)據(jù)庫(kù) */

public class Movedata extends EnterJdbcTemplate{

   

  public Movedata(JdbcTemplate jdbcTemplate) {

    super(jdbcTemplate);

  }
 public static void ccc(){

    System.out.println("++++++++++++++++++"+getJdbcTemplate().queryForMap("SELECT * FROM channel_info WHERE channel_id = ? ","cccc")); 

  }

} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談java switch如果case后面沒(méi)有break,會(huì)出現(xiàn)什么情況?

    淺談java switch如果case后面沒(méi)有break,會(huì)出現(xiàn)什么情況?

    這篇文章主要介紹了淺談java switch如果case后面沒(méi)有break,會(huì)出現(xiàn)什么情況?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想小編過(guò)來(lái)看看吧
    2020-09-09
  • 探究實(shí)現(xiàn)Aware接口的原理及使用

    探究實(shí)現(xiàn)Aware接口的原理及使用

    這篇文章主要為大家介紹了探究實(shí)現(xiàn)Aware接口的原理及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • MyBatis深入分析數(shù)據(jù)庫(kù)交互與關(guān)系映射

    MyBatis深入分析數(shù)據(jù)庫(kù)交互與關(guān)系映射

    這篇文章主要介紹了MyBatis中的數(shù)據(jù)庫(kù)交互與關(guān)系映射,MyBatis是一款優(yōu)秀的持久層框架,它支持定制化SQL、存儲(chǔ)過(guò)程以及高級(jí)映射,MyBatis避免了幾乎所有的JDBC代碼和手動(dòng)設(shè)置參數(shù)以及獲取結(jié)果集,需要的朋友可以參考下
    2024-05-05
  • SpringBoot接口實(shí)現(xiàn)百萬(wàn)并發(fā)的代碼示例

    SpringBoot接口實(shí)現(xiàn)百萬(wàn)并發(fā)的代碼示例

    隨著互聯(lián)網(wǎng)的發(fā)展,越來(lái)越多的應(yīng)用需要支持高并發(fā),在這種情況下,如何實(shí)現(xiàn)高并發(fā)成為了一個(gè)重要的問(wèn)題,Spring Boot是一個(gè)非常流行的Java框架,它提供了很多方便的功能來(lái)支持高并發(fā),本文將介紹如何使用Spring Boot來(lái)實(shí)現(xiàn)百萬(wàn)并發(fā)
    2023-10-10
  • Java之通過(guò)OutputStream寫入文件與文件復(fù)制問(wèn)題

    Java之通過(guò)OutputStream寫入文件與文件復(fù)制問(wèn)題

    這篇文章主要介紹了Java之通過(guò)OutputStream寫入文件與文件復(fù)制問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java?SpringAOP技術(shù)之注解方式詳解

    Java?SpringAOP技術(shù)之注解方式詳解

    這篇文章主要為大家詳細(xì)介紹了Java?SpringAOP技術(shù)之注解方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • Java之idea @NotNull @Nullable 注解使用

    Java之idea @NotNull @Nullable 注解使用

    這篇文章主要介紹了Java之idea @NotNull @Nullable 注解使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 詳解Java中的時(shí)區(qū)類TimeZone的用法

    詳解Java中的時(shí)區(qū)類TimeZone的用法

    TimeZone可以用來(lái)獲取或者規(guī)定時(shí)區(qū),也可以用來(lái)計(jì)算時(shí)差,這里我們就來(lái)詳解Java中的時(shí)區(qū)類TimeZone的用法,特別要注意下面所提到的TimeZone相關(guān)的時(shí)間校準(zhǔn)問(wèn)題.
    2016-06-06
  • 進(jìn)一步理解Java中的多態(tài)概念

    進(jìn)一步理解Java中的多態(tài)概念

    這篇文章主要介紹了進(jìn)一步理解Java中的多態(tài)概念,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-08-08
  • Java實(shí)現(xiàn)線程同步的四種方式總結(jié)

    Java實(shí)現(xiàn)線程同步的四種方式總結(jié)

    Java線程同步屬于Java多線程與并發(fā)編程的核心點(diǎn),需要重點(diǎn)掌握,下面我就來(lái)詳解Java線程同步的4種主要的實(shí)現(xiàn)方式,需要的可以參考一下
    2022-09-09

最新評(píng)論