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

添加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. django 模型对象的 update() get_or_create() 的使用

    update() 如果一个查询集是一个列表对象, 需要更新该列表对象里所有的单个数据集的数据,可以使用update()方法,而不须遍历整个查询集对象一个个逐一进行修改 obj_list = UserI ...

  2. windows 安装nexus3

    下载地址 nexus官网下载页面 文件名:nexus-3.3.1-01-win64.zip,解压,cd到bin目录 运行: nexus.exe /run 1 安装成系统服务: nexus.exe /i ...

  3. [ActionScript 3.0] UDP通信

    package com.controls.socket { import flash.events.DatagramSocketDataEvent; import flash.events.Event ...

  4. 十分钟用 Node 命令行工具打造 react-cli 脚手架

    如果你有以下想法: 每次新开项目需要copy一堆文件/文件夹,太烦!想要快速建立工程 用了vue-cli.react-app,羡慕!想要自己做一个 你只需花十分钟时间,做一个Node命令行工具,打造属 ...

  5. 自定义android ProgressDialog

    Android系统自己提供的UI的都是比较难看的,开发中经常用到自定义对话框,下面分享个最近项目中使用的加载框.   下面是源代码,主要的原理就是准备几个图片,然后循环播放. MainActivity ...

  6. CPM(Cluster Percolation method)派系过滤算法

    一.概念 (1)完全子图/全耦合网络/k-派系:所有节点全部两两相连 图1 这些全耦合网络也成为派系,k-派系表示该全耦合网络的节点数目为k 1)k-派系相邻:两个不同的k-派系共享k-1个节点,认为 ...

  7. 【Oracle】数据泵导入导出

    数据泵 expdp导出 nohup expdp  system/******** dumpfile=lysb_20121113_%U.dmp directory=dmp_dir schemas=sco ...

  8. androidStudio 打包与混淆

    在gradle中通过makeJar打包 不同模块的gradle都支持打包功能,application module的build.gradle中引入的是com.android.application插件 ...

  9. Spring统一异常处理

    1.为什么要用Spring的统一异常处理? 项目中无论是controller层.service层还是dao层都会有异常发生.每个过程都单独处理异常,系统的代码耦合度高,工作量大且不好统一,维护的工作量 ...

  10. Android中ListView的使用步骤

    第一步:  首先,在 布局文件中,声明listView控件. <ListView android:id="@+id/lv" android:layout_width=&quo ...