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 = @[email protected]
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搭建完成;

但是!!!问题来了,在启动文件中自动注入Jdbctemplate是OK的,可是在下面的代码中使用注入的时候Jdbctemplate是null这是为什么呢?  目前找问题中,期待解决,还是boot用的不熟练呀

package versionUpdate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class Movedata {

	@Autowired
	private static JdbcTemplate jdbcTemplate ;

	public static void ccc(){
		System.out.println("++++++++++++++++++"+jdbcTemplate.queryForMap("SELECT * FROM channel_info WHERE channel_id = ? ","ios"));
	}
}

  

  

时间: 2024-08-04 23:41:08

Spring Boot项目中使用jdbctemplate 操作MYSQL数据库的相关文章

在Spring Boot项目中使用Redis集群

Redis安装 Mac 系统安装Redis brew方式安装 在命令汗执行命令 brew install redis 安装完成之后的提示信息 ==> Downloading https://homebrew.bintray.com/bottles/redis-5.0.2.mojave.bottle.tar.gz ######################################################################## 100.0% ==> Pouring

在Spring Boot项目中使用Spock测试框架

摘自:https://www.cnblogs.com/javaadu/p/11748473.html 本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目中使用该框架写优雅.高效以及DSL化的测试用例.Spock通过@RunWith注解与JUnit框架协同使用,另外,Spock也可以和Mockito(Spring Boot应用的测试——Mockito

Spring Boot 项目中使用JSP

1    第2-2课:Spring Boot 项目中使用JSP JSP(Java Server Pages,Java 服务器页面)是一个简化的 Servlet 设计,它是由 Sun Microsystems 公司倡导.许多公司参与一起建立的一种动态网页技术标准.JSP 技术类似 ASP 技术,它是在传统的网页 HTML(标准通用标记语言的子集)文件(.html)中插入 Java 程序段(Scriptlet)和 JSP 标记(tag),从而形成 JSP 文件,后缀名为(*.jsp).用 JSP 开

Spring Boot项目中使用Mockito

本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring Boot可以跟BDD(Behavier Driven Development)工具.Cucumber和Spock协同工作,对应用程序进行测试. 进行软件开发的时候,我们会写很多代码,不过,再过六个月(甚至一年以上)你知道自己的代码怎么运作么?通过测试(单元测试.集成测试.接口测试)可

Spring Boot项目中如何定制PropertyEditors

本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据,实际上,在一次请求的完成过程中还发生了其他的转换,我们这次关注将参数转换成多种类型的对象,如:字符串转换成Date对象或字符串转换成Integer对象. 在编写控制器中的action方法时,Spring允许我们使用具体的数据类型定义函数签名,这是通过PropertyEditor实现的.Propert

Spring Boot项目中如何定制拦截器

本文首发于个人网站:Spring Boot项目中如何定制拦截器 Servlet 过滤器属于Servlet API,和Spring关系不大.除了使用过滤器包装web请求,Spring MVC还提供HandlerInterceptor(拦截器)工具.根据文档,HandlerInterceptor的功能跟过滤器类似,但拦截器提供更精细的控制能力:在request被响应之前.request被响应之后.视图渲染之前以及request全部结束之后.我们不能通过拦截器修改request内容,但是可以通过抛出异

Spring Boot入门(2)使用MySQL数据库

介绍 ??本文将介绍如何在Spring项目中连接.处理MySQL数据库. ??该项目使用Spring Data JPA和Hibernate来连接.处理MySQL数据库,当然,这仅仅是其中一种方式,你也可以使用Spring JDBC或者MyBatis. ??Spring Data JPA是Spring Data的一个子项目,主要用于简化数据访问层的实现,使用Spring Data JPA可以轻松实现增删改查.分页.排序等.Spring Data拥有很多子项目,除了Spring Data Jpa外,

Spring Boot项目中@SpringBootTest测试的时候卡住,一直Resolving Maven dependencies...

问题 用过idea(笔者经常用2018.3.x)创建 spring boot项目的时候默认会创建一个以下骨架的测试代码 package com.atguigu; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class DemoApplicationTests { @Test void contextLoads() {

spring boot项目中处理Schedule定时任务

初始化之后,我们在spring boot的入口类Application.java中,允许支持schedule @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 然后,新建一个执行类Jobs.java @Component pub