日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SpringBoot用JdbcTemplates访问Mysql

發布時間:2024/1/23 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot用JdbcTemplates访问Mysql 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文介紹springboot通過jdbc訪問關系型MySQL,通過spring的JdbcTemplate去訪問。

準備工作

  • 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');

創建工程

引入依賴:

在pom文件引入spring-boot-starter-jdbc的依賴:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId> </dependency>

引入mysql連接類和連接池:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.29</version></dependency>

開啟web:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

配置相關文件

在application.properties文件配置mysql的驅動類,數據庫地址,數據庫賬號、密碼信息。

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

通過引入這些依賴和配置一些基本信息,springboot就可以訪問數據庫類。

具體編碼

實體類

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);List<Account> 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 {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic int add(Account account) {return jdbcTemplate.update("insert into account(name, money) values(?, ?)",account.getName(),account.getMoney());}@Overridepublic int update(Account account) {return jdbcTemplate.update("UPDATE account SET NAME=? ,money=? WHERE id=?",account.getName(),account.getMoney(),account.getId());}@Overridepublic int delete(int id) {return jdbcTemplate.update("DELETE from TABLE account where id=?",id);}@Overridepublic Account findAccountById(int id) {List<Account> 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;}}@Overridepublic List<Account> findAccountList() {List<Account> list = jdbcTemplate.query("select * from account", new Object[]{}, new BeanPropertyRowMapper(Account.class));if(list!=null && list.size()>0){return list;}else{return null;}} }

service層

public interface IAccountService {int add(Account account);int update(Account account);int delete(int id);Account findAccountById(int id);List<Account> findAccountList();}

具體實現類:

@Service public class AccountService implements IAccountService {@AutowiredIAccountDAO accountDAO;@Overridepublic int add(Account account) {return accountDAO.add(account);}@Overridepublic int update(Account account) {return accountDAO.update(account);}@Overridepublic int delete(int id) {return accountDAO.delete(id);}@Overridepublic Account findAccountById(int id) {return accountDAO.findAccountById(id);}@Overridepublic List<Account> findAccountList() {return accountDAO.findAccountList();} }

構建一組restful api來展示

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 {@AutowiredIAccountService accountService;@RequestMapping(value = "/list",method = RequestMethod.GET)public List<Account> getAccounts(){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";}}}

可以通過postman來測試,具體的我已經全部測試通過,沒有任何問題。注意restful構建api的風格。

總結

以上是生活随笔為你收集整理的SpringBoot用JdbcTemplates访问Mysql的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。