摘要:本文介紹通過(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 idea mysql初始化mysql
-- create table `account` DROP TABLE `account` IF EXISTS CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `money` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `account` VALUES ("1", "aaa", "1000"); INSERT INTO `account` VALUES ("2", "bbb", "1000"); INSERT INTO `account` VALUES ("3", "ccc", "1000");創(chuàng)建工程
引入依賴:
在pom文件引入spring-boot-starter-jdbc的依賴:
org.springframework.boot spring-boot-starter-jdbc
引入mysql連接類和連接池:
mysql mysql-connector-java runtime com.alibaba druid 1.0.29
開(kāi)啟web:
配置相關(guān)文件org.springframework.boot spring-boot-starter-web
在application.properties文件配置mysql的驅(qū)動(dòng)類,數(shù)據(jù)庫(kù)地址,數(shù)據(jù)庫(kù)賬號(hào)、密碼信息。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456
通過(guò)引入這些依賴和配置一些基本信息,springboot就可以訪問(wèn)數(shù)據(jù)庫(kù)類。
具體編碼
public class Account { private int id ; private String name ; private double money; ....省略了getter. setter }dao層
public interface IAccountDAO { int add(Account account); int update(Account account); int delete(int id); Account findAccountById(int id); Listdao具體的實(shí)現(xiàn)類findAccountList(); }
package com.forezp.dao.impl; import com.forezp.dao.IAccountDAO; import com.forezp.entity.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; /** * Created by fangzhipeng on 2017/4/20. */ @Repository public class AccountDaoImpl implements IAccountDAO { @Autowired private JdbcTemplate jdbcTemplate; @Override public int add(Account account) { return jdbcTemplate.update("insert into account(name, money) values(?, ?)", account.getName(),account.getMoney()); } @Override public int update(Account account) { return jdbcTemplate.update("UPDATE account SET NAME=? ,money=? WHERE id=?", account.getName(),account.getMoney(),account.getId()); } @Override public int delete(int id) { return jdbcTemplate.update("DELETE from TABLE account where id=?",id); } @Override public Account findAccountById(int id) { Listservice層list = jdbcTemplate.query("select * from account where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Account.class)); if(list!=null && list.size()>0){ Account account = list.get(0); return account; }else{ return null; } } @Override public List findAccountList() { List list = jdbcTemplate.query("select * from account", new Object[]{}, new BeanPropertyRowMapper(Account.class)); if(list!=null && list.size()>0){ return list; }else{ return null; } } }
public interface IAccountService { int add(Account account); int update(Account account); int delete(int id); Account findAccountById(int id); Listservice具體實(shí)現(xiàn)類findAccountList(); }
@Service public class AccountService implements IAccountService { @Autowired IAccountDAO accountDAO; @Override public int add(Account account) { return accountDAO.add(account); } @Override public int update(Account account) { return accountDAO.update(account); } @Override public int delete(int id) { return accountDAO.delete(id); } @Override public Account findAccountById(int id) { return accountDAO.findAccountById(id); } @Override public List構(gòu)建一組restful api來(lái)展示findAccountList() { return accountDAO.findAccountList(); } }
package com.forezp.web; import com.forezp.entity.Account; import com.forezp.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by fangzhipeng on 2017/4/20. */ @RestController @RequestMapping("/account") public class AccountController { @Autowired IAccountService accountService; @RequestMapping(value = "/list",method = RequestMethod.GET) public ListgetAccounts(){ return accountService.findAccountList(); } @RequestMapping(value = "/{id}",method = RequestMethod.GET) public Account getAccountById(@PathVariable("id") int id){ return accountService.findAccountById(id); } @RequestMapping(value = "/{id}",method = RequestMethod.PUT) public String updateAccount(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name, @RequestParam(value = "money" ,required = true)double money){ Account account=new Account(); account.setMoney(money); account.setName(name); account.setId(id); int t=accountService.update(account); if(t==1){ return account.toString(); }else { return "fail"; } } @RequestMapping(value = "",method = RequestMethod.POST) public String postAccount( @RequestParam(value = "name")String name, @RequestParam(value = "money" )double money){ Account account=new Account(); account.setMoney(money); account.setName(name); int t= accountService.add(account); if(t==1){ return account.toString(); }else { return "fail"; } } }
可以通過(guò)postman來(lái)測(cè)試,具體的我已經(jīng)全部測(cè)試通過(guò),沒(méi)有任何問(wèn)題。注意restful構(gòu)建api的風(fēng)格。
源碼下載:https://github.com/forezp/Spr...
參考資料relational-data-access
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70356.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...
摘要:本文介紹如何在中使用默認(rèn)的聲明式緩存定義和接口用來(lái)統(tǒng)一不同的緩存技術(shù)。在使用集成的時(shí)候,我們需要注冊(cè)實(shí)現(xiàn)的的。默認(rèn)使用在我們不使用其他第三方緩存依賴的時(shí)候,自動(dòng)采用作為緩存管理器。源碼下載參考資料揭秘與實(shí)戰(zhàn)二數(shù)據(jù)緩存篇快速入門 本文介紹如何在springboot中使用默認(rèn)的spring cache 聲明式緩存 Spring 定義 CacheManager 和 Cache 接口用來(lái)統(tǒng)一不...
摘要:微信公眾號(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ò)...
閱讀 1426·2021-09-23 11:21
閱讀 3124·2019-08-30 14:14
閱讀 3206·2019-08-30 13:56
閱讀 4158·2019-08-30 11:20
閱讀 1965·2019-08-29 17:23
閱讀 2780·2019-08-29 16:14
閱讀 1711·2019-08-28 18:18
閱讀 1501·2019-08-26 12:14