Use JdbcTemplate to operate MySQL database in Spring boot project

Source: Internet
Author: User

No nonsense, first come the code

Pom file:

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

Profile: Application.properties (the Springboot framework uses this name by default and is placed under 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

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

At this point a simple springboot+jdbctemplate+mysql of the demo build completed;

But!!! The problem is that the automatic injection of jdbctemplate in the boot file is OK, but when injected in the following code jdbctemplate is null what is it for? currently looking for problems, look forward to solving, or boot with the unskilled AH

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;p ublic static void CCC () {System.out.println ("+++++++++++ +++++++ "+jdbctemplate.queryformap (" select * from Channel_info WHERE channel_id =? "," ios ")); }}

  

  

Use JdbcTemplate to operate MySQL database in Spring boot project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.