本文介绍springboot通过jdbc访问关系型mysql,通过spring的JdbcTemplate去访问。
准备工作
·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.bootspring-boot-starter-jdbc
引入mysql连接类和连接池:mysqlmysql-connector-javaruntimecom.alibabadruid1.0.29
开启web:org.springframework.bootspring-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 {@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 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 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 {@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 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 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的风格。
源码下载:https://github.com/forezp/SpringBootLearning
原创作者:方志朋
方志朋简介:SpringCloud中国社区联合创始人,博客访问量突破一千万,爱好开源,热爱分享,活跃于各大社区,保持着非常强的学习驱动力,终身学习践行者,终身学习受益者。目前就职于国内某家知名互联网保险公司,担任DEVOPS工程师,对微服务领域和持续集成领域研究较深,精通微服务框架SpringCloud