Tigon MyBatis v0.0.6 发布,再见 2020

栏目: 软件资讯 · 发布时间: 3年前

内容简介:Release Notes 优化Search#eq,Search#ne参数为NULL时候处理为is null,is not null @UseGeneratedKeys注解支持多参数 增加查询方法BaseQueryMapper#list(List keys),BaseQueryMapper#list(PrimarayKey[] keys) ...

Release Notes

  • 优化Search#eq,Search#ne参数为NULL时候处理为is null,is not null
  • @UseGeneratedKeys注解支持多参数
  • 增加查询方法BaseQueryMapper#list(List keys),BaseQueryMapper#list(PrimarayKey[] keys)
  • 优化Jdbc3KeyGen实现,增加共享变量
  • 优化SqlSessionFactory增强,只有发现SupperMapper子类,才做增强
  • 查询、更新支持驼峰格式属性名,转换为下划线列名
  • 完善单元测试
  • 代码简化,优化

Tigon MyBatis

简介

Tigon MyBatis为Spring工程中MyBatis的Mapper提供增强,主要有以下特点

  • 代码又少又壮,绝不做多余的事情
  • 仅需Mapper继承接口,实现   ,无额外配置,爽到没女朋友
  • 用完即走,毫不留恋

开始使用

  • 引入Maven依赖
<dependency>
  <groupId>me.chyxion.tigon</groupId>
  <artifactId>tigon-mybatis</artifactId>
  <version>0.0.6</version>
</dependency>

使用示例

下面是使用示例,可以在源代码中找到更详细的单元测试。Talk is cheep,read the fine source code.

定义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;

@Getter
@Setter
@ToString
@Table("tb_user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer id;
    @NotUpdate
    private String account;
    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. 插入

final User 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());

// single insert
mapper.insert(user);

final User 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());

final User 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());

// batch insert
mapper.insert(Arrays.asList(user1, user2));

II. 查询

根据ID查询单个对象

final Integer id = 1154;
final User user = mapper.find(id);

根据属性查询单个对象

final User user = mapper.find(
    new Search("account", "donghuang")
        .eq("mobile", "137647788xx"));

根据属性查询列表

final List<User> users = mapper.list(new Search()
    .between("birth_date",
        DateUtils.parseDate("1982-04-04"),
        DateUtils.parseDate("1994-04-04")
    )
    .eq("gender", User.Gender.MALE)
    .asc("birth_date")
    .limit(42));

Search对象支持的API

  • and And another Search
  • asc Order ASC
  • between Between two values
  • build Build query criterion
  • contains Value contains string
  • desc Order DSC
  • endsWith Value ends with string
  • eq Eqauls
  • gt Greater than
  • gte Eqauls or greater than
  • in In values
  • isNull Value is null
  • like Value like
  • limit Return rows limit
  • lt Less than
  • lte Eqauls or less than
  • ne Not equals
  • notIn Not in values
  • notNull Value is not null
  • offset Return rows offset
  • or Or another Search
  • orderBy Order by
  • startsWith Value starts with string

III. 更新

通过Entity根据ID更新

final User user = mapper.find(1);

user.setName("东皇大叔");
user.setUpdatedBy("SYS");
user.setUpdatedAt(new Date());

mapper.update(user);

通过Map<String, Object>更新

final Map<String, Object> update = new HashMap<>(6);
update.put("name", "东皇大叔");
update.put("updatedBy", "SYS");
update.put("updatedAt", new Date());

mapper.update(update, 1);
// OR
// mapper.update(update, new Search("id", 1));
// mapper.update(update, new Search(1));

更新列为NULL

// Update remark to NULL of id 274229
mapper.setNull("remark", 274229);
// Update remark to NULL of id 1154L
mapper.setNull("remark", new Search("id", 1154));
// Update all remarks to NULL. BE CAREFUL!!!
mapper.setNull("remark", new Search());

IV. 删除

通过ID删除数据

mapper.delete(1);

通过Search对象删除数据

mapper.delete(new Search("id", 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

原理

Tigon MyBatis并不改变MyBatis相关功能,所做的只是在程序启动期间检测业务Mapper接口,如果继承了相关BaseMapper.java,则注入相关方法MappedStatement,具体逻辑参见源码,超简单,超幼稚。

其他

在前面使用Search的例子中,我们需要一些User的属性常量字符串,比如

final User user = mapper.find(new Search("account", "donghuang"));

可以将这些常量定义在User类中,如

public static final String ACCOUNT = "account";

使用过程中可以使用属性常量,如

final User user = mapper.find(new Search(User.ACCOUNT, "donghuang"));

也可以使用Lombok@FieldNameConstants注解生成,只是这个注解还处于试验阶段,有一定不稳定风险。


以上所述就是小编给大家介绍的《Tigon MyBatis v0.0.6 发布,再见 2020》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Rails Cookbook

Rails Cookbook

奥西尼 / 江苏东南大学 / 2007-6 / 68.00元

Rails是业界领先的新一代Web 2.0应用程序开发框架,而这本《Rails Cookbook》里充满了为了让你成为Rails开发专家而准备的各种解决方案。讨论范围覆盖了从基本概念,如安装Rails及设置开发环境,到最新的各种技巧,如开发符合REST协议规范的Web服务等。 Rails可提供更轻量级的代码、更丰富的功能和更快捷的量身定制过程,由此带来了一场Web开发革命。《Rails Co......一起来看看 《Rails Cookbook》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具