讲解如何在springboot下整合mybatis,并访问数据库。

引入依赖

在pom文件引入mybatis-spring-boot-starter的依赖:

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.0

引入数据库连接依赖:

mysql

mysql-connector-java

runtime

com.alibaba

druid

1.0.29

引入数据源

application.properties配置文件中引入数据源:spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

这样,springboot就可以访问数据了。

创建数据库表

建表语句:-- 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');

具体实现

这篇文篇通过注解的形式实现。

创建实体:public class Account {

private int id ;

private String name ;

private double money;

setter...

getter...

}

dao层@Mapperpublic interface AccountMapper {

@Insert("insert into account(name, money) values(#{name}, #{money})")

int add(@Param("name") String name, @Param("money") double money);

@Update("update account set name = #{name}, money = #{money} where id = #{id}")

int update(@Param("name") String name, @Param("money") double money, @Param("id") int  id);

@Delete("delete from account where id = #{id}")

int delete(int id);

@Select("select id, name as name, money as money from account where id = #{id}")

Account findAccount(@Param("id") int id);

@Select("select id, name as name, money as money from account")

List findAccountList();}

service层@Servicepublic class AccountService {

@Autowired

private AccountMapper accountMapper;

public int add(String name, double money) {

return accountMapper.add(name, money);

}

public int update(String name, double money, int id) {

return accountMapper.update(name, money, id);

}

public int delete(int id) {

return accountMapper.delete(id);

}

public Account findAccount(int id) {

return accountMapper.findAccount(id);

}

public List findAccountList() {

return accountMapper.findAccountList();

}}

controller层,构建restful APIpackage com.forezp.web;import com.forezp.entity.Account;import com.forezp.service.AccountService;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

AccountService 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.findAccount(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) {

int t= accountService.update(name,money,id);

if(t==1) {

return "success";

}else {

return "fail";

}

}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)

public String delete(@PathVariable(value = "id")int id) {

int t= accountService.delete(id);

if(t==1) {

return "success";

}else {

return "fail";

}

}

@RequestMapping(value = "", method = RequestMethod.POST)

public String postAccount(@RequestParam(value = "name") String name,

@RequestParam(value = "money") double money) {

int t= accountService.add(name,money);

if(t==1) {

return "success";

}else {

return "fail";

}

}}

通过postman测试通过。

springboot整合mysql5.7_springboot整合mybatis访问mysql,数据库相关推荐

  1. php访问mysql数据库实验报告,php访问mysql数据库

    //建一个连接,造一个连接对象 $db = new MySQLi("localhost","root","123","mydb&q ...

  2. 使用php从网络访问mysql数据库,使用PHP从web访问mysql数据库

    一. web数据库构架的工作原理 1. 用户由浏览器发出HTTP请求,请求特定的web页面. 2. web服务器接受接收到对特定页面的请求,检索相应文件,并将其传递给php引擎处理. 3. php引擎 ...

  3. eclipse spring mysql,eclipse环境下的springboot框架+mybatis访问MySQL报错空指针

    "/")public classTestController { @RequestMapping("/login")publicString login() { ...

  4. Springboot03整合SpringDataJPA访问MySQL数据库

    使用SpringBoot访问MySQL数据库,并且结合SpringDataJPA完成CRUD(Create,Read,Update,Delete 结合 Springboot01 中的demo案例 Sp ...

  5. 简单配置Mybatis,Druid访问MySQL数据库

    文章目录 前言 一.Mybatis是什么 二.Druid是什么 三.使用步骤 1. 导入Jar包 2. application.yml配置Druid数据源 3. application.yml配置My ...

  6. SpringBoot 实战 (八) | 使用 Spring Data JPA 访问 Mysql 数据库

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 Spring Data JPA 的使用. 什么是 Spring Data JPA 在介绍 Spri ...

  7. Springboot学习1——通过JPA访问MySQL数据库

    本文展示如何通过JPA访问MySQL数据库. JPA全称Java Persistence API,即Java持久化API,它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据 ...

  8. MySql数据库记录相差14小时排错,使用Java访问Mysql数据库时出现时区异常的解决方案

    最近遇到1个大坑,A系统迁移到B系统,2边系统 同1个字段 createTime 看到的不一致. 表象: A系统: 2019-6-10 17:34 B系统: 2019-6-11 .... 再次尝试: ...

  9. mybatis获取mysql源数据类型_spring集成mybatis实现mysql数据库读写分离

    前言 在网站的用户达到一定规模后,数据库因为负载压力过高而成为网站的瓶颈.幸运的是目前大部分的主流数据库都提供主从热备功能,通过配置两台数据库主从关系,可以将一台数据库的数据更新同步到另一台服务器上. ...

最新文章

  1. Martini 中的 Handler
  2. html5页面常用的代码,最全的 HTML5 知识汇总
  3. UltraTextEditor
  4. c++ map 修改value_C++知识分享之STL容器:set 容器与 map 容器的简单应用
  5. centos7开放端口访问不了_基于TiUP cluster 在centos7系统上模拟生产环境部署TiDB数据库...
  6. Hybris commerce的promotion rule里的固定折扣功能
  7. 信息学奥赛C++语言: 区间内的真素数
  8. linux服务器拓扑图,Linux服务器作为网关的DNS分离解析服务(CentOS 7版本)
  9. [转]win7 64位下android开发环境的搭建
  10. 【转载】石油天然气常用单位换算
  11. dom操作节点之常用方法
  12. onload 事件、DOMContentLoaded事件、DOM加载顺序
  13. 【转】ASP.NET AJAX入门系列(9):使用ScriptManager控件
  14. 一些常用单位之间的换算
  15. C# 项目实战(经典)
  16. 基本农田卫星地图查询_别被迷惑了,你手机上的北斗导航App和北斗卫星导航没关系...
  17. Latex 宏定义中 \relax
  18. 电脑ssd硬盘怎么安装使用
  19. 从民国时期小学生的两篇作文来看两位小作者的写作能力。
  20. php支付宝聚合支付源码,ThinkPHP5.1聚合支付源码

热门文章

  1. java8 并行执行方法_如何在Java8中执行此并行任务
  2. opencv_python 读取4通道png图片
  3. NLP——基于transformer 的翻译系统
  4. git 建立一个私有模块
  5. 谨慎的Waymo CEO:未来几十年,自动驾驶无法做到无处不在
  6. 修复Eclipse在Mac OSX下启动时卡住问题
  7. java中关键字volatile的作用(转载)
  8. SJCL:斯坦福大学JS加密库
  9. 微软MVP社区活动(西宁站)
  10. 在 C# 中 (x == X) 何时成立?