本文主要讲解如何在springboot下整合mybatis,并访问数据库。由于mybatis这个框架太过于流行,所以我就不讲解了。

引入依赖

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

       <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter<artifactId><version>1.3.0</version></dependency>

引入数据库连接依赖:

<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>

引入数据源

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层

@Mapper
public 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<Account> findAccountList();
}

service层

@Service
public class AccountService {@Autowiredprivate 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<Account> findAccountList() {return accountMapper.findAccountList();}
}

controller层,构建restful API


package 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 {@AutowiredAccountService 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.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";}}}

要源码下方评论去留下联系方式!

SpringBoot精藏(五)SpringBoot整合mybatis相关推荐

  1. SpringBoot 2.1.5(36)---整合Mybatis

    一起来学 SpringBoot 2.x | 第七篇:整合Mybatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射,几乎避免了所有的 JDBC 代码和手动设置参 ...

  2. SpringBoot实战(三):整合Mybatis配置多数据源

    [前言] 最近接到一个新需求,经过分析后做了相应的设计:其中需要在一个项目中操做不同的数据源:于是进行了相关验证:在此记录一下验证过程. [实战多数据源]          一.Pom中引入相应的Ja ...

  3. springboot动态切换数据源_Springboot整合Mybatis注解实现动态数据源切换

    AbstractRoutingDataSource AbstractRoutingDataSource是spring-jdbc包提供的一个了AbstractDataSource的抽象类,它实现了Dat ...

  4. asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询

    本文我们将继续分享介绍Spring Boot在整合Mybatis开发企业级应用时其他典型的业务场景,即Mybatis是如何实现多表关联查询时将查询结果集与对象进行映射的,主要的内容包含"一对 ...

  5. (一)SpringBoot 整合 MyBatis

    一.工具 IDE:idea.DB:mysql 二.创建SpringBoot工程 在Idea中使用SpringInitializr模板创建SpringBoot工程,依赖选择如下: 这里也可以不选JDBC ...

  6. springboot 整合mybatis plus

    简单介绍下 mybatis plus,mybatis plus(简称 MP)是一个 mybatis  的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 本文讲解使用 ...

  7. SpringBoot 整合mybatis、mybatis日志、并测试findAll查询数据库方法

    一.创建SpringBoot项目 随便创建一个SpringBoot项目,能启动就行 建议看我的上一个文章 SpringBoot框架快速入门搭建Hello World 二.添加mybatis依赖并配置 ...

  8. SpringBoot学习笔记(7)-整合Mybatis

    文章目录 一.配置mybatis依赖 二.配置application.properties 三.在启动类上加@MapperScan注解 四.配置资源文件读取 五.结合SpringMVC完成测试 整合环 ...

  9. SpringBoot整合mybatis+mybatis分页插件

    第一步:相关依赖 <!--web,servlet引入--> <dependency><groupId>org.springframework.boot</gr ...

最新文章

  1. leetcode--买股票的最佳时机II--python
  2. linux 开机错误 Entering emergency mode. Exit the shell to continue.
  3. 周其对话农民丰收节交易会 乡村振兴不能单单从乡村着眼
  4. 静态链接库与动态链接库的区别(Sqlite\Visual Studio 2017)
  5. 前端学习(3279):循环 遍历 2
  6. 实现两个pawn的切换
  7. html++标签页+界面,CSS+DIV实现多标签页面。
  8. 无痕模式后如何找到历史_离异后女人如何快速找到对象?成都百和情缘婚介告诉你...
  9. 为什么有些程序员是三、四台电脑一起用的?
  10. 解线性方程组——有机物燃烧的化学方程组的配平
  11. 浏览器对象模型bom的作用是什么?
  12. 麻省理工18年春软件构造课程阅读15“相等”
  13. 网购工具软件chrome扩展插件大推荐
  14. 番外篇 之 实现Unity和Android进行交互(基于Android Studio 3.1.1以及Jar包方式)
  15. PNP和NPN三极管区别
  16. 必备知识:相机标定-旋转矩阵性质
  17. 了解 React 之 Suspense 和 lazy
  18. linux学习资料(转帖收藏)
  19. Linux安装MySQL遇到的问题及其解决方式
  20. MPEG-DASH简介

热门文章

  1. dm-crypt——多功能 Linux 磁盘加密工具
  2. C/C++游戏项目:编译重温小霸王经典超级玛丽教程(附注释源码)
  3. Hspice-重要的入门仿真语句
  4. xadsafe做暗刷_手把手教你如何去掉网吧广告之万象OL篇_XADSAFE
  5. 惠普服务器-ILO使用
  6. 解决ORA-01033: ORACLE initialization or shutdown in progress
  7. 如何自己在家手动制作网线
  8. 游戏设计的艺术:一本透镜的书——第十一章 游戏机制必须平衡
  9. 肺结节圆形边界光滑_肺结节良恶性的六大鉴别要点
  10. 如何css设置div页面100%高度, body页面全高