例程下载: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. 【项目实战】sass使用基础篇(上)

    Sass是一种CSS预处理语言.CSS预处理语言是一种新的专门的编程语言,编译后形成正常的css文件,为css增加一些编程特性,无需考虑浏览器的兼容性(完全兼容css3),让css更加简洁.适应性更强 ...

  2. PhpStorm配置Apache与php的运行环境详细教程

    本文主要说明如何在phpstorm中配置已经安装好的PHP与apache.首先需要在本地安装php,这里我安装的是phpstudy 进入PHPstorm的界面点击file 下的settings 在La ...

  3. Hyperledger Fabric 部署

    Hyperledger Fabric 部署 Hyperledger Fabric需要使用Docker.Go环境. Docker环境安装 Docker环境安装 直接查看这一篇,安装好之后将当前用户非ro ...

  4. 36 个JS 面试题

    1.JS中let和const有什么用? 在现代js中,let&const是创建变量的不同方式. 在早期的js中,咱们使用var关键字来创建变量. let&const关键字是在ES6版本 ...

  5. Debug很重要

    之前做一个小功能,就是用php发送邮件,项目中已经使用了wordpress的wp_mail,所以同事建议我继续用wp_mail函数. 然而遇到了一个奇怪的情况,邮件没有发出去,也没有任何报错日志. 照 ...

  6. unity探索者之protobuf的序列化和反序列化导致unity崩溃的问题研究

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/7574569.html 这两天博主在接微信支付SDK的时候碰到一个非常恶心又诡异的问 ...

  7. xpath和css选择器对比

    基本语法对比 都可以在html中提取内容,但xpath可以提取xml的内容.

  8. mxnet笔记

    参考链接: https://mxnet.apache.org/api/faq/distributed_training https://mxnet.apache.org/api/faq/gradien ...

  9. linux计算命令

    1.在lammps中通过car文件导出data文件 (进入工作目录xxx cd xxx/ msi2lmp.exe xxx -class I -frc clayff -i -ignore > ou ...

  10. Python输入input、输出print

    1.输入input input是用于输入数据给变量.通过键盘输入的是字符串,如果需要其他格式,需要做转换.比如int.float类型数据,int() 如下是一个例子: 如果a不进行int转换,那么输入 ...