开发环境延续上一节的开发环境这里不再做介绍

添加mybatis依赖

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

DAO层接口(这里是直接通过注解实现数据库操作,不做DAO层实现)

@Mapper
@Repository
public interface IAccountMybatisDao {
@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(@Param("id") int id);
@Select("select * 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层接口

public interface IAccountMybatisService {

int add(Account account);

int update(Account account);

int delete(int id);

Account findAccountById(int id);

List<Account> findAccountList();
}

service层实现
@Service
public class AccountMybatisServiceImpl implements IAccountMybatisService{
@Autowired
private IAccountMybatisDao accountMybatisDao;

@Override
public int add(Account account) {
return accountMybatisDao.add(account.getName(),account.getMoney());
}

@Override
public int update(Account account) {
return accountMybatisDao.update(account.getName(),account.getMoney(),account.getId());
}

@Override
public int delete(int id) {
return accountMybatisDao.delete(id);
}

@Override
public Account findAccountById(int id) {
return accountMybatisDao.findAccount(id);
}

@Override
public List<Account> findAccountList() {
return accountMybatisDao.findAccountList();
}
}

控制器

@RestController
@RequestMapping("/accountMybatis")
public class AccountMybatisController {
@Autowired
private IAccountMybatisService service;
@Autowired
private Account account;

@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Account> getAccounts() {
return service.findAccountList();
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Account getAccountById(@PathVariable("id") int id) {
return service.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.setId(id);
account.setMoney(money);
account.setName(name);
int t = service.update(account);
if (t == 1) {
return "success";
} else {
return "fail";
}

}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable(value = "id") int id) {
int t = service.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) {
account.setName(name);
account.setMoney(money);
int t = service.add(account);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
}
以上增删改查都通过postman测试通过,读者可自行验证

Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作的更多相关文章

  1. Spring Boot入门(四):开发Web Api接口常用注解总结

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...

  2. SpringBoot 整合 Mybatis 进行CRUD测试开发

    今天来和大家分享下 Spring Boot 整合 MyBatis 的 CRUD 测试方法开发.因为 MyBaits 有两种开发形式,一种基于注解,一种基于 xml . SpringBoot配置文件也有 ...

  3. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  4. 05 Mybatis的CRUD操作和Mybatis连接池

    1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...

  5. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  6. spring boot 系列之七:SpringBoot整合Mybatis

    springboot已经很流行,但是它仍需要搭配一款ORM框架来实现数据的CRUD,之前已经分享过JdbcTemplete和JPA的整合,本次分享下Mybatis的整合. 对于mybatis的使用,需 ...

  7. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  8. Spring Boot2 系列教程(二十一)整合 MyBatis

    前面两篇文章和读者聊了 Spring Boot 中最简单的数据持久化方案 JdbcTemplate,JdbcTemplate 虽然简单,但是用的并不多,因为它没有 MyBatis 方便,在 Sprin ...

  9. Spring Boot2 系列教程 (九) | SpringBoot 整合 Mybatis

    前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,本文通过注解的形式实现. 什么是 Mybatis MyBatis 是支持定制化 SQL.存储过程以及 ...

随机推荐

  1. Python小白学习之路(十三)—【递归调用】

    一.递归调用定义 在函数内部,可以调用其他函数. 如果在调用一个函数的过程中直接或间接调用自身本身,则称为递归调用 从某种意义上来说,递归调用可以实现无限循环 二.递归调用的特性 必须有一个明确的结束 ...

  2. 使用python+selenium对web进行自动化测试

    想用python代码,对web网页进行自动化测试 web自动化测试和手动测试的区别: 手动测试:通过手动去对网页的功能进行点点点 web自动化:可以通过代码,自动对网页点点点 首先,将python+s ...

  3. AngularJS学习之旅

    开篇 最近由于项目上可能需要用到AngularJS,公司将技术学习.调研的任务安排了下来,因此开始了我的AngularJS学习之路. 在这之前没写过技术博客,主要是由于太懒,另外自愧文笔不好,因此一直 ...

  4. activiti插件安装-离线安装

    直接选择离线安装,在线因为网络问题,一直提示失败. 一.下载插件Activiti BPMN 2.0 designer 链接: https://pan.baidu.com/s/1gKBHZFnmNJx7 ...

  5. reCaptcha for ASP.NET

    使用reCaptcha for asp.net library,以前一直工作正常,最近发现运行不正常了,google到了一个解决方案,不适用control而直接使用api,代码如下: markup: ...

  6. Xpath string()提取多个子节点中的文本

    <div> <ul class="show"> <li>275万购昌平邻铁三居 总价20万买一居</li> <li>00 ...

  7. (转)AIX的Dump文件学习笔记

    原文:http://czmmiao.iteye.com/blog/1144999 DUMP文件概述 为了增强故障分析能力,IBM的服务器增加了对设备故障当前环境的保存功能,就是保存一份设备故障时的内存 ...

  8. Karatsuba乘法--实现大数相乘

    Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...

  9. Jsp页面用ajax传输json数组的方法

    详细参考jquery的API 这里主要展示实例,即写法 <%@ page language="java" contentType="text/html; chars ...

  10. 安装Elasticsearch5.0 部署Head插件

    部署5.0版本的ES 5.0版本的ES跟之前的版本最大的不同之处就是多了很多环境的校验,比如jdk,max-files等等. 设置内核参数 vi /etc/sysctl.conf # 增加下面的内容 ...