mybatis-spring-boot-starter 1.3.0 操作实体类的SpringBoot例子
例程下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200428-02.zip
需求:使用mybatis实现对hy_emp表的CRUD。
实现步骤:
1.添加依赖
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
注:《SpringBoot实战派》P201提供的写法,version2.0.0会报错,故而改成1.3.0.
2.书写表对应的实体类。
package com.ufo.gatling.entity; public class Emp {
private long id; private String name; private int salary; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
3.书写Mapper接口
package com.ufo.gatling.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update; import com.ufo.gatling.entity.Emp; @Mapper
public interface EmpMapper {
@Select("select * from hy_emp where id=#{id}")
Emp findById(@Param("id") long id); @Select("select * from hy_emp")
List<Emp> findAll(); @Update("update hy_emp set name=#{name},salary=#{salary} where id=#{id}")
int updateById(Emp emp); @Delete("delete from hy_emp where id=#{id}")
int deleteById(Emp emp); @Insert("insert into hy_emp(id,name,salary) values(#{id},#{name},#{salary})")
int insert(Emp emp); @SelectProvider(type=EmpSql.class,method="findHighLevel")
List<Emp> findHighLevelEmps();
}
这种写法把SQL和接口写到了一起,比早期MyBatis版本的分两个文件明白多了(早期项目里通过Java类查找翻到接口,再去找同名xml实在是有够反人类)。如果SQL够长够复杂则可以通过SelectProvider的方式,分到一个文件里去好好写,下面就是涉及到的EmpSql类。
4.EmpSQl类:
package com.ufo.gatling.mapper; public class EmpSql {
public String findHighLevel() {
return "select * from hy_emp where salary>15000";
}
}
有人会说这个也不复杂,例子当然不复杂,真写起项目来想不复杂都不行。
5.测试类,请单个函数调试或执行。
package com.ufo.gatling; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import com.ufo.gatling.entity.Emp;
import com.ufo.gatling.mapper.EmpMapper; @SpringBootTest
class GatlingApplicationTests {
@Autowired
private EmpMapper mapper=null; @Test
void test01_findById() {
Emp emp=mapper.findById(6);
assertEquals("Felix", emp.getName());
assertEquals(20000, emp.getSalary());
} @Test
void test02_findAll() {
List<Emp> empList=mapper.findAll();
assertEquals(6, empList.size());
} @Test
void test03_updateById() {
Emp emp=new Emp();
emp.setId(1);
emp.setName("Gates");
emp.setSalary(123456); int changedCount=mapper.updateById(emp);
assertEquals(1, changedCount); Emp found=mapper.findById(1);
assertEquals("Gates", found.getName());
assertEquals(123456, found.getSalary());
} @Test
void test04_deleteById() {
Emp emp=new Emp();
emp.setId(3); int changedCount=mapper.deleteById(emp);
assertEquals(1, changedCount); emp=mapper.findById(3);
assertEquals(null, emp);
} @Test
void test05_insert() {
Emp emp=new Emp();
emp.setId(7);
emp.setName("Bear");
emp.setSalary(300000); int changedCount=mapper.insert(emp);
assertEquals(1, changedCount); Emp found=mapper.findById(7);
assertEquals("Bear", found.getName());
assertEquals(300000, found.getSalary());
} @Test
void test06_findHighLevelEmps() {
List<Emp> empList=mapper.findHighLevelEmps();
assertEquals(4, empList.size());
}
}
好了,全文就到这里,具体细节请下载代码。
Java无论是编码还是配置看网文或是书籍都觉得简单,自己动手一操作发现很多细节挡害,不用心解决都执行不下去。
真是纸上得来终觉浅,绝知此事要躬行。
--2020-04-28--
mybatis-spring-boot-starter 1.3.0 操作实体类的SpringBoot例子的更多相关文章
- spring boot:使用分布式事务seata(druid 1.1.23 / seata 1.3.0 / mybatis / spring boot 2.3.2)
一,什么是seata? Seata:Simpe Extensible Autonomous Transcaction Architecture, 是阿里中间件开源的分布式事务解决方案. 前身是阿里的F ...
- Spring Boot 整合 Redis 实现缓存操作
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 ...
- 创建自己的Spring Boot Starter
抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...
- 手把手教你定制标准Spring Boot starter,真的很清晰
写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...
- 一个简单易上手的短信服务Spring Boot Starter
前言 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式让用户进行操作,从而提高用户的实用性. Spring Boot Starter 由于 S ...
- 自定义spring boot starter 初尝试
自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- 自己写spring boot starter
自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...
- 年轻人的第一个自定义 Spring Boot Starter!
陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...
随机推荐
- Hexo博客部署到远程仓库(Conding、Gitee、Github)
一.本地环境搭建 1.安装Git Git可以有效.高速的处理各种项目版本管理.也就是用来管理你的hexo博客文章,上传到GitHub的工具. Git下载地址 安装好了之后使用git -version查 ...
- nginx进程模型解析
nginx进程模型解析 概念 master会发送请求给worker,用于处理用户的请求,模型图如下 nginx进程分类 master进程(只有1个) 接受信号传递给worker wo ...
- 2020-07-13:es是去查id再根据id去查数据库这种方式好,还是所有数据都放es,直接去查es好?
福哥答案2020-07-13: 有人觉得第一种方法好,也有人觉得第二种方法好.如果搜索字段远小于显示字段,比如搜索字段为3个,显示字段有20个,这个时候用第一种方法好.es+hbase,一般这样搭配. ...
- .NET 跨平台框架Avalonia UI: 填坑指北(一):熟悉UI操作
Avalonia 是一个跨平台的 .NET UI 框架,支持 Windows.Linux.Mac OSX... (以及Android IOS soon..) 本篇主要介绍Avalonia开发过程和L ...
- Linux Centos 下安装npm 实测可用
转载地址 https://blog.csdn.net/u012129607/article/details/60966045 1.root 登录linux 2.没有目录就自己创建一个 cd /usr/ ...
- sharedb结合elementUi编写的实时小工具
我是使用sharedb 作为后端 ,然后前端使用的elementUI样式,编写的一个值班小工具.接下来,让我们先来了解一下sharedb是什么吧? sharedb工具 github地址:https:/ ...
- AdblockPlus自定义屏蔽广告
AdblockPlus自定义屏蔽广告我推荐使用两种方法: 1. 使用CSS选择器 2. 使用样式选择器 屏蔽广告中,重要的一个问题就是识别广告. 我们要自己编写屏蔽就得将广告选出来,告诉Adblock ...
- 手把手教你在win10下搭建pytorch GPU环境(Anaconda+Pycharm)
Anaconda指的是一个开源的Python发行版本,其主要优点如下: Anaconda默认安装了常见的科学计算包,用它搭建起Python环境后不用再费时费力安装这些包: Anaconda可以创建互相 ...
- MIT 6.828 | JOS | 关于虚拟空间和物理空间的总结
Question: 做lab过程中越来越迷糊,为什么一会儿虚拟地址是4G 物理地址也是4G ,那这有什么作用呢? 解决途径: 停下来,根据当前lab的进展,再回头看上学期操作系统的ppt & ...
- JavaScript学习系列博客_27_JavaScript 遍历数组
遍历数组 - 遍历数组就是将数组中元素都获取到 - 一般情况我们都是使用for循环来遍历数组: - 使用forEach()方法来遍历数组(不兼容IE8) forEach()方法需要一个回调函数(由我们 ...