Tigon MyBatis v0.0.8 发布,极简 MyBatis Mapper 增强
Tigon MyBatis v0.0.8 已经发布,为 MyBatis Mapper 提供增强,设计精巧,代码量很少,代码洁癖工程师的朋友
此版本更新内容包括:
- 优化插入生成Key逻辑,之前通过拦截器方式实现,在高并发环境下存在Bug
- 删除了100多行代码,今天又变强了
详情查看:https://gitee.com/chyxion/tigon-mybatis/releases/v0.0.8
Tigon MyBatis
简介
Tigon MyBatis为Spring工程中MyBatis的Mapper提供增强,主要有以下特点
- 代码又少又壮,绝不做多余的事情
- 仅需Mapper继承接口,实现
增
删
改
查
,无额外配置,爽到没女朋友 - 用完即走,毫不留恋
开始使用
- 引入Maven依赖
<dependency> <groupId>me.chyxion.tigon</groupId> <artifactId>tigon-mybatis</artifactId> <version>0.0.8</version> </dependency>
使用示例
定义Entity
package me.chyxion.tigon.mybatis.entity; import lombok.Getter; import lombok.Setter; import java.util.Date; import lombok.ToString; import java.io.Serializable; import me.chyxion.tigon.mybatis.Table; import me.chyxion.tigon.mybatis.NotUpdate; import me.chyxion.tigon.mybatis.NotUpdateWhenNull; @Getter @Setter @ToString @Table("tb_user") public class User implements Serializable { private static final long serialVersionUID = 1L; private Integer id; // 标记账户不被更新 @NotUpdate private String account; // 当手机号为null不被更新 @NotUpdateWhenNull private String mobile; private String name; private Gender gender; private String password; private Date birthDate; private String city; private String avatar; private Boolean active; private String remark; private String createdBy; private Date createdAt; private String updatedBy; private Date updatedAt; public enum Gender { MALE, FEMALE } }
定义Mapper
package me.chyxion.tigon.mybatis.mapper; import java.util.List; import me.chyxion.tigon.mybatis.BaseMapper; import org.apache.ibatis.annotations.Mapper; import me.chyxion.tigon.mybatis.entity.User; @Mapper public interface UserMapper extends BaseMapper<Integer, User> { }
注入Mapper对象
@Autowired private UserMapper mapper;
I. 插入
val user = new User(); user.setName("Donghuang"); user.setAccount("donghuang"); user.setMobile("137647788xx"); user.setPassword(RandomStringUtils.randomAlphanumeric(16)); user.setGender(User.Gender.MALE); user.setBirthDate(DateUtils.parseDate("1994-04-04")); user.setCity("Shanghai"); user.setActive(true); user.setRemark("Uncle Donghuang"); user.setCreatedBy("donghuang"); user.setCreatedAt(new Date()); // 插入单条记录 mapper.insert(user); val user1 = new User(); user1.setName("Gemily"); user1.setAccount("gemily"); user1.setMobile("15770780xxx"); user1.setPassword(RandomStringUtils.randomAlphanumeric(16)); user1.setGender(User.Gender.FEMALE); user1.setBirthDate(DateUtils.parseDate("1990-06-06")); user1.setCity("Hangzhou"); user1.setActive(true); user1.setCreatedBy("donghuang"); user1.setCreatedAt(new Date()); val user2 = new User(); user2.setName("Luffy"); user2.setAccount("luffy"); user2.setMobile("137647799xx"); user2.setPassword(RandomStringUtils.randomAlphanumeric(16)); user2.setGender(User.Gender.MALE); user2.setBirthDate(DateUtils.parseDate("1997-07-07")); user2.setCity("East sea"); user2.setActive(true); user2.setRemark("Luffy"); user2.setCreatedBy("donghuang"); user2.setCreatedAt(new Date()); // 批量插入记录 mapper.insert(Arrays.asList(user1, user2));
II. 查询
根据ID
查询单个对象
val id = 1154; // 根据主键查询单条记录 val user = mapper.find(id);
根据属性查询单个对象
// 根据属性account, mobile查询单个对象 val user = mapper.find( new Search("account", "donghuang") .eq("mobile", "137647788xx"));
根据属性查询列表
// 根据属性birthDate, gender查询数据列表 // 查询结果根据属性birthDate升序排序 // 返回数据限制42条 val users = mapper.list(new Search() .between("birthDate", DateUtils.parseDate("1982-04-04"), DateUtils.parseDate("1994-04-04") ) .eq("gender", User.Gender.MALE) .asc("birthDate") .limit(42));
Search
对象支持的API
asc
Order ASC 列升序排序desc
Order DSC 列降序排序orderBy
Order by 列属性排序between
Between two values 属性列属于2个值之间build
Build query criterion 自定义构建一个属性列查询条件startsWith
Value starts with string 属性列以字符串开头,等同于col like 'val%'
endsWith
Value ends with string 属性列以字符串结尾,等同于col like '%val'
contains
Value contains string 属性列包含字符串,等同于col like '%val%'
like
Value like 属性列与字符串相似eq
Equals 属性列等于ne
Not equals 属性列小于或等于gt
Greater than 属性列大于gte
Equals or greater than 属性列大于或等于lt
Less than 属性列小于lte
Equals or less than 属性列小于in
In values 属性列属于集合notIn
Not in values 属性列不属于集合isNull
Value is null 属性列为nullnotNull
Value is not null 属性列不为nullisTrue
Value is true 属性列为trueisFalse
Value is false 属性列为falselimit
Return rows limit 查询/更新结果行数限制offset
Return rows offset 查询结果偏移行数and
And anotherSearch
且另外一个Search对象or
Or anotherSearch
或另外一个Search对象
III. 更新
通过Entity
根据ID
更新
// 根据主键查询记录 val user = mapper.find(1); user.setName("东皇大叔"); user.setUpdatedBy("SYS"); user.setUpdatedAt(new Date()); // 更新单个实体对象 mapper.update(user);
通过Map<String, Object>
更新
val update = new HashMap<String, Object>(6); update.put("name", "东皇大叔"); update.put("updatedBy", "SYS"); update.put("updatedAt", new Date()); // 通过Map更新ID为1的记录 mapper.update(update, 1); // 效果同上 // mapper.update(update, new Search("id", 1)); // mapper.update(update, new Search(1));
更新列为NULL
// 更新id为274229记录的属性列remark为null mapper.setNull("remark", 274229); // 更新id为1154记录的属性列remark为null mapper.setNull("remark", new Search("id", 1154)); // 更新全表的属性列remark为null,小心操作!!! mapper.setNull("remark", new Search());
IV. 删除
通过ID
删除数据
// 根据主键删除记录 mapper.delete(1);
通过Search
对象删除数据
// 根据属性ID删除记录 mapper.delete(new Search("id", 1)); // 等同于 mapper.delete(1);
V. 杂项
除了上面说到的一些基础增删改查操作,还有一些实用功能,如@Transient
@UseGeneratedKeys
@NoPrimaryKey
@NotUpdateWhenNull
@RawValue
等注解,插入、更新前回调,以及支持扩展自定义的方法等。
配置说明
- SpringBoot项目,无需其他操作,引入依赖即可
- Spring项目,注册Bean me.chyxion.tigon.mybatis.TigonMyBatisConfiguration
- 业务Mapper继承me.chyxion.tigon.mybatis.BaseMapper或相关衍生Mapper,Base(Query, Insert, Update, Delete)Mapper

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
开源 | dl_inference 更新:增加 TensorRT、MKL 集成,提高深度学习模型推理速度
01背 景 dl_inference是58同城推出的通用深度学习推理服务,可在生产环境中快速上线由TensorFlow、PyTorch、Caffe等框架训练出来的深度学习模型。dl_inference于2020年3月26号发布,可参见《开源|dl_inference:通用深度学习推理服务》。 我们在2021年11月对dl_inference再次进行更新,从发布至今新增如下Features: 1、集成TensorRT加速深度学习推理,支持将TensorFlow-SavedModel模型、PyTorch-Pth模型进行自动优化,转换为TensorRT模型后部署,提高模型在GPU上的推理速度。 2、集成Intel Math Kernel Library库的TensorFlow Serving推理框架,加速TensorFlow模型CPU上推理。 3、支持Caffe模型推理,提供丰富的模型应用示例。 02使用TensorRT加速深度学习推理 深度学习模型推理阶段对算力和时延具有很高的要求,如果将训练好的神经网络直接部署到推理端,很有可能出现算力不足无法运行或者推理时间较长等问题,因此我们需要对训...
- 下一篇
Kubernetes v1.23.0 正式发布,新特性一览
“ 「K8S 生态周报」内容主要包含我所接触到的 K8S 生态相关的每周值得推荐的一些信息。欢迎订阅知乎专栏「k8s生态」[1]。 ” Kubernetes v1.23 今天正式发布,这是 2021 年发布的第三个版本,也是今年最后一个正式发布的版本。 此版本中主要包括 47 项增强更新,其中 11 项达到 stable, 17 项达到 beta 还有 19 项达到 alpha 。当然,也有 1 项被标记为废弃。相比于 v1.22 从数量上来说是少了一点(v1.22 有 53 项增强更新),但这并不影响这是一个很棒的版本! 在 Kubernetes 的发布周期变更为 每4个月一个版本 后,很明显的感觉就是不用在升级上面花费太多时间了,毕竟 Kubernetes 的升级操作是个体力活,大家觉得呢? 我们一起来看看这个版本中有哪些值得关注的变更吧! 新增 kubectl alpha events 命令 在之前的 《K8S 生态周报| Helm 新版本发布增强对 OCI 的支持》 文章的上游进展中我曾为大家介绍了该功能。它是按照 KEP #1440 实施的。 “ 增加此命令主要是由于在不修改...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Hadoop3单机部署,实现最简伪集群
- CentOS7设置SWAP分区,小内存服务器的救世主