Spring Boot項目中使用jdbctemplate 操作MYSQL資料庫

來源:互聯網
上載者:User

標籤:line   exce   sys   hand   host   autoconf   etl   xsd   span   

不廢話,先來代碼

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-8spring.datasource.username=rootspring.datasource.password=1234spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.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;@SpringBootApplicationpublic class ApplicationMain implements CommandLineRunner {private Logger log = Logger.getLogger(ApplicationMain.class);@Autowiredprivate JdbcTemplate jdbcTemplate;public static void main(String[] args) {SpringApplication springApplication = new SpringApplication(ApplicationMain.class);springApplication.run(args);}@Overridepublic 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;@Servicepublic class Movedata {@Autowiredprivate static JdbcTemplate jdbcTemplate ;public static void ccc(){System.out.println("++++++++++++++++++"+jdbcTemplate.queryForMap("SELECT * FROM channel_info WHERE channel_id = ? ","ios")); }}

  

  

Spring Boot項目中使用jdbctemplate 操作MYSQL資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.