Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍
添加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操作的更多相关文章
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
- SpringBoot 整合 Mybatis 进行CRUD测试开发
今天来和大家分享下 Spring Boot 整合 MyBatis 的 CRUD 测试方法开发.因为 MyBaits 有两种开发形式,一种基于注解,一种基于 xml . SpringBoot配置文件也有 ...
- 【MyBatis】MyBatis实现CRUD操作
1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...
- 05 Mybatis的CRUD操作和Mybatis连接池
1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...
- Spring Boot入门 and Spring Boot与ActiveMQ整合
1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...
- spring boot 系列之七:SpringBoot整合Mybatis
springboot已经很流行,但是它仍需要搭配一款ORM框架来实现数据的CRUD,之前已经分享过JdbcTemplete和JPA的整合,本次分享下Mybatis的整合. 对于mybatis的使用,需 ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot2 系列教程(二十一)整合 MyBatis
前面两篇文章和读者聊了 Spring Boot 中最简单的数据持久化方案 JdbcTemplate,JdbcTemplate 虽然简单,但是用的并不多,因为它没有 MyBatis 方便,在 Sprin ...
- Spring Boot2 系列教程 (九) | SpringBoot 整合 Mybatis
前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,本文通过注解的形式实现. 什么是 Mybatis MyBatis 是支持定制化 SQL.存储过程以及 ...
随机推荐
- 【bug记录】jpa 解决org.hibernate.lazyinitializationexception could not initialize proxy - no session
前言 最近开发项目比较忙,Spring Cloud的笔记得稍稍放放了,下午出来个bug,恶心的不行,功能很简单,也没有什么级联或复杂的映射关系,就是一直在报三个异常 Caused by: com.fa ...
- jzoj5710
對於每個詢問,我們先考慮插入1~r中每個數的答案,這樣子就不用考慮後面數對現在的影響 那麼我們可以在權值線段樹的每一個位置存儲每一個值最後一次結束的位置 問題變成了,如何判斷區間連續? 我們可以對線段 ...
- mysql编写存储过程(1)
存储过程:其实就是存储在数据库中,有一些逻辑语句与SQL语句组成的函数.由于是已经编译好的语句,所以执行速度快,而且也安全. 打开mysql的控制台,开始编写存储过程. 实例1: 编写存储过程: 执行 ...
- [题解]luogu P4116 Qtree3
终于来到了Qtree3, 其实这是Qtree系列中最简单的一道题,并不需要线段树, 只要树链剖分的一点思想就吼了. 对于树链剖分剖出来的每一根重链,在重链上维护一个Set就好了, 每一个Set里存的都 ...
- .NET 如何隐藏Console Application的窗口
1)创建Console Application工程 2)修改Output type 为Windows Application即可
- SocketIo+SpringMvc实现文件的上传下载
SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...
- 【数组】Missing Number
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- 机器学习--Gradient Boosting Machine(GBM)调参方法详解
一.GBM参数 总的来说GBM的参数可以被归为三类: 树参数:调节模型中每个决策树的性质 Boosting参数:调节模型中boosting的操作 其他模型参数:调节模型总体的各项运作 1.树参数 现在 ...
- 【LeetCode题解】160_相交链表
目录 160_相交链表 描述 解法一:哈希表 思路 Java 实现 Python 实现 解法二:双指针(推荐) 思路 Java 实现 Python 实现 160_相交链表 描述 编写一个程序,找到两个 ...
- 我是怎么从安卓到php再成为前端开发工程师的
记得我下定决心学Android(安卓)是17年的暑假,暑假前,学校组织了一次集训,美其名曰帮我们巩固知识,实际上就是学校和长沙的培训学校某牛达成了合作,教我们一些基础知识,然后集训完建议那些在学校没学 ...