Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫實(shí)例
最近在學(xué)習(xí)使用Spring Boot連接數(shù)據(jù)庫,今天學(xué)習(xí)了使用jdbctemplate 操作MYSQL數(shù)據(jù)庫,下面就留個(gè)筆記
不廢話,先來代碼
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è)簡單的SpringBoot+Jdbctemplate+MYSQL的DEMO搭建完成;
如果不想在啟動(dòng)類里面直接進(jìn)行數(shù)據(jù)庫操作,可以按照下面的方式:
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ù)庫 */
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"));
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring操作JdbcTemplate數(shù)據(jù)庫的方法學(xué)習(xí)
- spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)管理
- Spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)參數(shù)
- Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式
- SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
- Spring boot 使用JdbcTemplate訪問數(shù)據(jù)庫
- springboot使用JdbcTemplate完成對數(shù)據(jù)庫的增刪改查功能
- Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解
相關(guān)文章
java中 Set與Map排序輸出到Writer詳解及實(shí)例
這篇文章主要介紹了 java中 Set與Map排序輸出到Writer詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
SpringBoot使用JavaMailSender實(shí)現(xiàn)發(fā)送郵件+Excel附件
項(xiàng)目審批完畢后,需要發(fā)送郵件通知相關(guān)人員,并且要附帶數(shù)據(jù)庫表生成的Excel表格,這就要求不光是郵件發(fā)送功能,還要臨時(shí)生成Excel表格做為附件,本文詳細(xì)介紹了SpringBoot如何使用JavaMailSender實(shí)現(xiàn)發(fā)送郵件+Excel附件,需要的朋友可以參考下2023-10-10
SpringCloud通過MDC實(shí)現(xiàn)分布式鏈路追蹤
在DDD領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)中,我們使用SpringCloud來去實(shí)現(xiàn),但排查錯(cuò)誤的時(shí)候,通常會想到Skywalking,但是引入一個(gè)新的服務(wù),增加了系統(tǒng)消耗和管理學(xué)習(xí)成本,對于大型項(xiàng)目比較適合,但是小的項(xiàng)目顯得太過臃腫了,所以本文介紹了SpringCloud通過MDC實(shí)現(xiàn)分布式鏈路追蹤2024-11-11
解決RestTemplate 的getForEntity調(diào)用接口亂碼的問題
這篇文章主要介紹了解決RestTemplate 的getForEntity調(diào)用接口亂碼的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

