SpringBoot用JdbcTemplates访问Mysql的过程
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容介绍了"SpringBoot用JdbcTemplates访问Mysql的过程"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希
千家信息网最后更新 2026年11月01日SpringBoot用JdbcTemplates访问Mysql的过程
本篇内容介绍了"SpringBoot用JdbcTemplates访问Mysql的过程"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
准备工作
jdk 1.8
maven 3.0
idea
mysql
初始化mysql:
-- create table `account`DROP TABLE `account` IF EXISTSCREATE 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的依赖:
org.springframework.boot spring-boot-starter-jdbc
引入mysql连接类和连接池:
mysql mysql-connector-java runtime com.alibaba druid 1.0.29
开启web:
org.springframework.boot spring-boot-starter-web
配置相关文件
在application.properties文件配置mysql的驱动类,数据库地址,数据库账号、密码信息。
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.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 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. */@Repositorypublic 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) { List 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; } }} service层
public interface IAccountService { int add(Account account); int update(Account account); int delete(int id); Account findAccountById(int id); List findAccountList();} 具体实现类:
@Servicepublic 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 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 { @Autowired IAccountService accountService; @RequestMapping(value = "/list",method = RequestMethod.GET) public List 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的过程"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
过程
数据
数据库
文件
配置
信息
内容
更多
知识
测试
实用
学有所成
接下来
可以通过
困境
地址
实体
实际
密码
工程
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
科技与互联网
互联网科技有限公司经营范围
美国互联网科技巨头监管
互联网科技的发展
浩特互联网科技
鑫亿盛互联网科技
未来的互联网科技
科技创新互联网企业
互联网 视角下的科技创新
联恩商钥互联网科技有限公司
互联网科技公司的市场部门
博兴的互联网电子科技公司
云创客互联网科技公司
互联网时代科技改变生活
互联网科技与管理
互联网金融科技公司的优势
美国互联网科技公司
互联网金融运营模式中的金融科技
玉溪新华互联网科技怎么选
互联网科技品牌
互联网科技的
奇士达互联网科技
电子科技大学互联网
科技互联网大厂
互联网金融科技展
天津腾达互联网科技有限公司
互联网科技资金流入
21世纪互联网科技成就
济南小医互联网科技有限公司招聘
上海旺乐尼互联网科技有限公司