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 ,那如何开发一 ...
随机推荐
- jquery 事件对象笔记
jQuery元素操作 设置或获取元素固有属性 获取 prop(属性名) 修改 prop(属性名,值) 获取自定义属性 ...
- JS笔记 语法
javascript概述 简称为JS,是一款能够运行在JS解释器.引擎中的脚本语言 JS解释器.引擎 JS的运行环境 1.独立安装的js解释器 -nodeJS 2.嵌入在浏览器中的js解释器 JS基于 ...
- cudnn加速计算
cudnn加速运算 torch.backends.cudnn.enabled = True torch.backends.cudnn.benchmark = True 第一句话是说,使用的是非确定性算 ...
- 谁来教我渗透测试——黑客必须掌握的HTML基础(二)
今天我们继续看看html的学习笔记. 文本标签 标题标签<hn> 将文本设置为标题显示的标签对.设定标题字体大小,n=1(大)~6(小),标题大小一共有6种,也就是从<h1>… ...
- C# ASP 上传/下载文件
1. 上传文件前台页面 <div style="padding-left:20px;"> <asp:FileUpload ID="FileUpload ...
- C++socket编程write()、read()简介及与send()、recv()的区别
1. write 函数原型:ssize_t write(int fd, const void*buf,size_t nbytes)write函数将buf中的nbytes字节内容写入文件描述符fd.成功 ...
- Oracle创建主键优劣
创建主键方式 一个表的主键是唯一标识,不能有重复,不允许为空. 一个表的主键可以由一个字段或多个字段共同组成. -- 列级,表级建立主键 1.create table constraint_test ...
- js利用canvas绘制爱心
js代码如下: var cav = document.getElementById("a").getContext("2d"); function draw(x ...
- [netty4][netty-handler]netty之idle handler处理
初始化时记录idle时间,并启动一个延时任务,延时时间为idle时间,延时任务是io.netty.handler.timeout.IdleStateHandler.AllIdleTimeoutTask ...
- python爬虫抖音 个人资料 仅供学习参考 切勿用于商业
本文仅供学习参考 切勿用于商业 本次爬取使用fiddler+模拟器(下载抖音APP)+pycharm 1. 下载最新版本的fiddler(自行百度下载),以及相关配置 1.1.依次点击,菜单栏-Too ...