摘要:微信公眾號(hào)一個(gè)優(yōu)秀的廢人如有問(wèn)題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問(wèn)題。前言如題,今天介紹通過(guò)訪問(wèn)關(guān)系型通過(guò)的去訪問(wèn)。我這里已經(jīng)全部測(cè)試通過(guò),請(qǐng)放心使用。源碼下載后語(yǔ)以上用訪問(wèn)的教程。另外,關(guān)注之后在發(fā)送可領(lǐng)取免費(fèi)學(xué)習(xí)資料。
微信公眾號(hào):一個(gè)優(yōu)秀的廢人前言
如有問(wèn)題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問(wèn)題。
如題,今天介紹 springboot 通過(guò)jdbc訪問(wèn)關(guān)系型mysql,通過(guò) spring 的 JdbcTemplate 去訪問(wèn)。
準(zhǔn)備工作SpringBoot 2.x
jdk 1.8
maven 3.0
idea
mysql
構(gòu)建 SpringBoot 項(xiàng)目,不會(huì)的朋友參考舊文章:如何使用 IDEA 構(gòu)建 Spring Boot 工程
項(xiàng)目目錄結(jié)構(gòu) pom 文件引入依賴application.yaml 配置數(shù)據(jù)庫(kù)信息xml org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime com.alibaba druid 1.1.13 org.springframework.boot spring-boot-starter-test test
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true username: 數(shù)據(jù)庫(kù)用戶名 password: 數(shù)據(jù)庫(kù)密碼實(shí)體類
package com.nasus.domain; /** * Project Name:jdbctemplate_demodao 層
* Package Name:com.nasus.domain
* Date:2019/2/3 10:55
* Description: TODO: 描述該類的作用
* * @author nasus
* Copyright Notice ========================================================= * This file contains proprietary information of Eastcom Technologies Co. Ltd. * Copying or reproduction without prior written approval is prohibited. * Copyright (c) 2019 ======================================================= */ public class Student { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name="" + name + """ + ", age=" + age + "}"; } }
package com.nasus.dao; import com.nasus.domain.Student; import java.util.List; /** * Project Name:jdbctemplate_demo
* Package Name:com.nasus.dao
* Date:2019/2/3 10:59
* Description: TODO: 描述該類的作用
* @author nasus
*/ public interface IStudentDao { int add(Student student); int update(Student student); int delete(int id); Student findStudentById(int id); ListfindStudentList(); }
具體實(shí)現(xiàn)類:
package com.nasus.dao.impl; import com.nasus.dao.IStudentDao; import com.nasus.domain.Student; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; /** * Project Name:jdbctemplate_demoservice 層
* Package Name:com.nasus.dao.impl
* Date:2019/2/3 11:00
* Description: TODO: 描述該類的作用
* * @author nasus
*/ @Repository public class IStudentDaoImpl implements IStudentDao{ @Autowired private JdbcTemplate jdbcTemplate; @Override public int add(Student student) { return jdbcTemplate.update("insert into student(name, age) values(?, ?)", student.getName(),student.getAge()); } @Override public int update(Student student) { return jdbcTemplate.update("UPDATE student SET NAME=? ,age=? WHERE id=?", student.getName(),student.getAge(),student.getId()); } @Override public int delete(int id) { return jdbcTemplate.update("DELETE from TABLE student where id=?",id); } @Override public Student findStudentById(int id) { // BeanPropertyRowMapper 使獲取的 List 結(jié)果列表的數(shù)據(jù)庫(kù)字段和實(shí)體類自動(dòng)對(duì)應(yīng) Listlist = jdbcTemplate.query("select * from student where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Student.class)); if(list!=null && list.size()>0){ Student student = list.get(0); return student; }else{ return null; } } @Override public List findStudentList() { // 使用Spring的JdbcTemplate查詢數(shù)據(jù)庫(kù),獲取List結(jié)果列表,數(shù)據(jù)庫(kù)表字段和實(shí)體類自動(dòng)對(duì)應(yīng),可以使用BeanPropertyRowMapper List list = jdbcTemplate.query("select * from student", new Object[]{}, new BeanPropertyRowMapper(Student.class)); if(list!=null && list.size()>0){ return list; }else{ return null; } } }
package com.nasus.service; import com.nasus.domain.Student; import java.util.List; /** * Project Name:jdbctemplate_demo
* Package Name:com.nasus.service
* Date:2019/2/3 11:17
* Description: TODO: 描述該類的作用
* * @author nasus
*/ public interface IStudentService { int add(Student student); int update(Student student); int delete(int id); Student findStudentById(int id); ListfindStudentList(); }
實(shí)現(xiàn)類:
package com.nasus.service.impl; import com.nasus.dao.IStudentDao; import com.nasus.domain.Student; import com.nasus.service.IStudentService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * Project Name:jdbctemplate_democontroller 構(gòu)建 restful api
* Package Name:com.nasus.service.impl
* Date:2019/2/3 11:18
* Description: TODO: 描述該類的作用
* * @author nasus
* Copyright Notice ========================================================= * This file contains proprietary information of Eastcom Technologies Co. Ltd. * Copying or reproduction without prior written approval is prohibited. * Copyright (c) 2019 ======================================================= */ @Repository public class IStudentServiceImpl implements IStudentService { @Autowired private IStudentDao iStudentDao; @Override public int add(Student student) { return iStudentDao.add(student); } @Override public int update(Student student) { return iStudentDao.update(student); } @Override public int delete(int id) { return iStudentDao.delete(id); } @Override public Student findStudentById(int id) { return iStudentDao.findStudentById(id); } @Override public ListfindStudentList() { return iStudentDao.findStudentList(); } }
package com.nasus.controller; import com.nasus.domain.Student; import com.nasus.service.IStudentService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Project Name:jdbctemplate_demo演示結(jié)果
* Package Name:com.nasus.controller
* Date:2019/2/3 11:21
* Description: TODO: 描述該類的作用
* * @author nasus
*/ @RestController @RequestMapping("/student") public class StudentController { @Autowired private IStudentService iStudentService; @PostMapping("") public int addStudent(@RequestBody Student student){ return iStudentService.add(student); } @PutMapping("/{id}") public String updateStudent(@PathVariable Integer id, @RequestBody Student student){ Student oldStudent = new Student(); oldStudent.setId(id); oldStudent.setName(student.getName()); oldStudent.setAge(student.getAge()); int t = iStudentService.update(oldStudent); if (t == 1){ return student.toString(); }else { return "更新學(xué)生信息錯(cuò)誤"; } } @GetMapping("/{id}") public Student findStudentById(@PathVariable Integer id){ return iStudentService.findStudentById(id); } @GetMapping("/list") public ListfindStudentList(){ return iStudentService.findStudentList(); } @DeleteMapping("/{id}") public int deleteStudentById(@PathVariable Integer id){ return iStudentService.delete(id); } }
其他的 api 測(cè)試可以通過(guò) postman 測(cè)試。我這里已經(jīng)全部測(cè)試通過(guò),請(qǐng)放心使用。
源碼下載:https://github.com/turoDog/De...
后語(yǔ)以上SpringBoot 用 JdbcTemplates 訪問(wèn)Mysql 的教程。最后,對(duì) Python 、Java 感興趣請(qǐng)長(zhǎng)按二維碼關(guān)注一波,我會(huì)努力帶給你們價(jià)值,如果覺(jué)得本文對(duì)你哪怕有一丁點(diǎn)幫助,請(qǐng)幫忙點(diǎn)好看,讓更多人知道。
另外,關(guān)注之后在發(fā)送 1024 可領(lǐng)取免費(fèi)學(xué)習(xí)資料。資料內(nèi)容詳情請(qǐng)看這篇舊文:Python、C++、Java、Linux、Go、前端、算法資料分享
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/73286.html
摘要:中用訪問(wèn)一準(zhǔn)備工作運(yùn)行二啟動(dòng)容器并進(jìn)行表的創(chuàng)建啟動(dòng)查看鏡像啟動(dòng)查看全部的鏡像對(duì)應(yīng)的啟動(dòng)連接創(chuàng)建數(shù)據(jù)庫(kù)創(chuàng)建表三使用創(chuàng)建項(xiàng)目選擇四配置相關(guān)的文件配置配置實(shí)體類類名為層接口實(shí)現(xiàn)類層接口實(shí)現(xiàn)類層控制層五使用進(jìn)行測(cè)試沒(méi)有安裝的話可以 SpringBoot 中用 JdbcTemplate 訪問(wèn)MySQL 一. 準(zhǔn)備工作 IDEA docker : 運(yùn)行MySql 二. 啟動(dòng) docker my...
摘要:本文介紹通過(guò)訪問(wèn)關(guān)系型通過(guò)的去訪問(wèn)。通過(guò)引入這些依賴和配置一些基本信息,就可以訪問(wèn)數(shù)據(jù)庫(kù)類。具體編碼實(shí)體類省略了層具體的實(shí)現(xiàn)類層具體實(shí)現(xiàn)類構(gòu)建一組來(lái)展示可以通過(guò)來(lái)測(cè)試,具體的我已經(jīng)全部測(cè)試通過(guò),沒(méi)有任何問(wèn)題。注意構(gòu)建的風(fēng)格。 本文介紹springboot通過(guò)jdbc訪問(wèn)關(guān)系型mysql,通過(guò)spring的JdbcTemplate去訪問(wèn)。 準(zhǔn)備工作 jdk 1.8 maven 3.0 i...
閱讀 3017·2023-04-26 00:32
閱讀 498·2019-08-30 15:52
閱讀 2105·2019-08-30 15:52
閱讀 3347·2019-08-30 15:44
閱讀 3280·2019-08-30 14:09
閱讀 1416·2019-08-29 15:15
閱讀 3390·2019-08-28 18:12
閱讀 1074·2019-08-26 13:55