欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot中使用jdbctemplate 操作MYSQL数据库实例

程序员文章站 2024-03-01 08:35:52
最近在学习使用spring boot连接数据库,今天学习了使用jdbctemplate 操作mysql数据库,下面就留个笔记 不废话,先来代码 pom文件:...

最近在学习使用spring boot连接数据库,今天学习了使用jdbctemplate 操作mysql数据库,下面就留个笔记

不废话,先来代码

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框架默认使用这个名字,放在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 

启动类:

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);

  }

}

至此一个简单的springboot+jdbctemplate+mysql的demo搭建完成;

如果不想在启动类里面直接进行数据库操作,可以按照下面的方式:

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;

  }   

} 
package versionupdate;

import org.springframework.jdbc.core.jdbctemplate;

/** 操作数据库 */

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")); 

  }

} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。