内容简介:Tigon MyBatis v0.0.8 已经发布,为 MyBatis Mapper 提供增强,设计精巧,代码量很少,代码洁癖工程师的朋友 此版本更新内容包括: 优化插入生成Key逻辑,之前通过拦截器方式实现,在高并发环境下存在Bug 删除了1...
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
ascOrder ASC 列升序排序descOrder DSC 列降序排序orderByOrder by 列属性排序betweenBetween two values 属性列属于2个值之间buildBuild query criterion 自定义构建一个属性列查询条件startsWithValue starts with string 属性列以字符串开头,等同于col like 'val%'endsWithValue ends with string 属性列以字符串结尾,等同于col like '%val'containsValue contains string 属性列包含字符串,等同于col like '%val%'likeValue like 属性列与字符串相似eqEquals 属性列等于neNot equals 属性列小于或等于gtGreater than 属性列大于gteEquals or greater than 属性列大于或等于ltLess than 属性列小于lteEquals or less than 属性列小于inIn values 属性列属于集合notInNot in values 属性列不属于集合isNullValue is null 属性列为nullnotNullValue is not null 属性列不为nullisTrueValue is true 属性列为trueisFalseValue is false 属性列为falselimitReturn rows limit 查询/更新结果行数限制offsetReturn rows offset 查询结果偏移行数andAnd anotherSearch且另外一个Search对象orOr 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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- uiw 1.4.3 发布,增强日历控件
- DataGrip 2017.3.1 发布,增强各种功能
- fastmybatis 1.0.11 发布,增强团队协作
- fastmybatis 1.0.11 发布,增强团队协作
- OneinStack 2.0 发布,增强自动模式功能
- OneinStack 2.0 发布,增强自动模式功能
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入分析Java Web技术内幕(修订版)
许令波 / 电子工业出版社 / 2014-8-1 / CNY 79.00
《深入分析Java Web技术内幕(修订版)》新增了淘宝在无线端的应用实践,包括:CDN 动态加速、多终端化改造、 多终端Session 统一 ,以及在大流量的情况下,如何跨越性能、网络和一个地区的电力瓶颈等内容,并提供了比较完整的解决方案。 《深入分析Java Web技术内幕(修订版)》主要围绕Java Web 相关技术从三方面全面、深入地进行了阐述。首先介绍前端知识,即在JavaWeb ......一起来看看 《深入分析Java Web技术内幕(修订版)》 这本书的介绍吧!
