例程下载: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例子的更多相关文章

  1. 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 ...

  2. Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!   『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』   本文提纲 ...

  3. 创建自己的Spring Boot Starter

    抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...

  4. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

  5. 一个简单易上手的短信服务Spring Boot Starter

    前言 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式让用户进行操作,从而提高用户的实用性. Spring Boot Starter 由于 S ...

  6. 自定义spring boot starter 初尝试

    自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...

  7. Spring Boot Starter 介绍

    http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

  8. 自己写spring boot starter

    自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...

  9. 年轻人的第一个自定义 Spring Boot Starter!

    陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...

随机推荐

  1. 6、Java 运算符

    Java运算符按功能可分为:算数运算符.关系运算符.逻辑运算符.位运算符.赋值运算符和条件运算符. 1.算数运算符 算术运算符包括通常的加(+).减(-).乘(*).除(/).取模(%),完成整数型和 ...

  2. JS与React分别实现倒计时(天时分秒)

    JS方法 html部分 <div class="clock"> <i></i> 天 <i></i> : <i> ...

  3. Spring quartz中取得ServletContext

    在开发javaWeb定时任务的时候,有些处理要取得应用的相对路径,这就需要用到ServletContext取得到这个路径 解决思路是在web应用启动时,把ServletContext提前注入到Sche ...

  4. Oracle 存储过程 批量插入测试数据

    有时候需要做DB的效率测试时,需要模拟大量数据.可以根据一条原始数据,通过执行存储过程拷贝出大量数据: CREATE OR REPLACE PROCEDURE proc_msw_strsql IS i ...

  5. 2020-05-31:假如Redis里面有1亿个key,其中有10w个key是以某个固定的已知的前缀开头的,如何将它们全部找出来?

    福哥答案2020-05-31: 使用keys指令可以扫出指定模式的key列表.对方接着追问:如果这个redis正在给线上的业务提供服务,那使用keys指令会有什么问题?这个时候你要回答redis关键的 ...

  6. Android 开发学习进程0.16 layout_weight属性 R文件关联XML Module

    layout_weight属性 layout_weight属性我们常常用到,但有时候会发现它还有一些奇怪的属性,比如大多数使用时会把宽度设置成0,但要是宽度不设置成0会有什么效果? layout_we ...

  7. 聊聊mysql中的int(1)

    昨天有个读者问了我这样一个问题在mysql中建表的时候,我设置一个字段为int类型,长度为1,但是我发现这个字段却可以存储任意长度的数字,这是什么情况?这个问题在我刚接触数据库的时候也遇到过,我觉得有 ...

  8. Spark从入门到放弃---RDD

    什么是Spark? 关于Spark具体的定义,大家可以去阅读官网或者百度关于Spark的词条,在此不再赘述.从一个野生程序猿的角度去理解,作为大数据时代的一个准王者,Spark是一款主流的高性能分布式 ...

  9. 阿里天池 NLP 入门赛 TextCNN 方案代码详细注释和流程讲解

    thumbnail: https://image.zhangxiann.com/jung-ho-park-HbnqEhMBpPM-unsplash.jpg toc: true date: 2020/8 ...

  10. 笔记:Windows Server2008R2服务安装

    Windows Server2008R2 服务安装 服务一:IIS,internet information services,互联网信息服务,微软开发的运行在Windows系统中互联网服务,提供了w ...