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.存储过程以及 ...
随机推荐
- java实现简单扫雷游戏
/** * 一个简单的扫雷游戏 MainFram.java */ package www.waston; import java.awt.BorderLayout; import java.awt.C ...
- ASP.NET MVC Forms验证机制
ASP.NET MVC 3 使用Forms身份验证 身份验证流程 一.用户登录 1.验证表单:ModelState.IsValid 2.验证用户名和密码:通过查询数据库验证 3.如果用户名和密码正确, ...
- iOS--线程的创建
1.获取当前线程 NSThread *current=[NSThread currentThread]; 2.获取主线程的另外一种方式 NSThread *main=[NSThread mainThr ...
- RDLC_部署到不同的浏览器
首先我用的是vs2015 的reportview插件 在数据库中应该配置报表的服务器地址,在项目中添加ReportViewer 插件,单独用一个页面显示接收报表 <form id="f ...
- 为了用python计算一个汉字的中心点,差点没绞尽脑汁活活累死
为了用python计算一个汉字的中心点,差点没绞尽脑汁活活累死
- (转)Python 字符串
原文:http://www.runoob.com/python/python-strings.html
- Java之IO(十四)IO包中其它类
转载请注明出处:http://www.cnblogs.com/lighten/p/7267553.html 1.前言 此章介绍IO包中剩余未介绍的几个流和工具类,包括LineNumberReader. ...
- Linq基础知识之延迟执行
Linq中的绝大多数查询运算符都有延迟执行的特性,查询并不是在查询创建的时候执行,而是在遍历的时候执行,也就是在enumerator的MoveNext()方法被调用的时候执行,大说数Linq查询操作实 ...
- js前端日期格式化处理
js前端日期格式化处理 1.项目中时间返回值,很过时候为毫秒值,我们需要转换成 能够看懂的时间的格式: 例如: yyyy-MM-dd HH:mm:ss 2.处理方法(处理方法有多种,可以传值到前端 ...
- Win7下无法提交MapReduce Job到集群环境(转)
一. 对hadoop eclipse plugin认识不足 http://zy19982004.iteye.com/blog/2024467曾经说到我最hadoop eclipse plugin作用的 ...