在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:tigon-mybatis开源软件地址:https://gitee.com/chyxion/tigon-mybatis开源软件介绍:Tigon MyBatis简介Tigon MyBatis为Spring工程中MyBatis的Mapper提供优质增强,主要有以下特点
开始使用
<dependency> <groupId>me.chyxion.tigon</groupId> <artifactId>tigon-mybatis</artifactId> <version>0.0.8</version></dependency> 使用示例下面是使用示例,可以在源代码中找到更详细的单元测试用例 定义Entitypackage 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 }} 定义Mapperpackage 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;@Mapperpublic interface UserMapper extends BaseMapper<Integer, User> {} 注入Mapper对象@Autowiredprivate UserMapper mapper; I. 插入val user = new User();user.setName("Donghuang");user.setAccount("donghuang");...user.setCreatedAt(new Date());// 插入单条记录mapper.insert(user);val user1 = new User();user1.setName("Gemily");user1.setAccount("gemily");...user1.setCreatedAt(new Date());val user2 = new User();user2.setName("Luffy");user2.setAccount("luffy");...user2.setCreatedAt(new Date());// 批量插入记录mapper.insert(Arrays.asList(user1, user2)); II. 查询根据 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));
III. 更新通过 // 根据主键查询记录val user = mapper.find(1);user.setName("东皇大叔");user.setUpdatedBy("SYS");user.setUpdatedAt(new Date());// 更新单个实体对象mapper.update(user); 通过 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)); 更新列为 // 更新id为274229记录的属性列remark为nullmapper.setNull("remark", 274229);// 更新id为1154记录的属性列remark为nullmapper.setNull("remark", new Search("id", 1154));// 更新全表的属性列remark为null,小心操作!!!mapper.setNull("remark", new Search()); IV. 删除通过 // 根据主键删除记录mapper.delete(1); 通过 // 根据属性ID删除记录mapper.delete(new Search("id", 1));// 等同于 mapper.delete(1); V. 杂项除了上面说到的一些基础增删改查操作,还有一些实用功能,如 配置说明
原理Tigon MyBatis并不改变MyBatis相关功能,所做的只是在程序启动期间检测业务Mapper接口,如果继承了相关 代码阅读都看到这里了,动动小手clone工程,直接打开UserMapperTest.java右击调试运行,心事全都被你发现,代码写得烂的地方回来喷我,一定改。 其他在前面使用 val user = mapper.find(new Search("account", "donghuang")); 可以将这些常量定义在 public static final String ACCOUNT = "account"; 使用过程中可以使用属性常量,如 val user = mapper.find(new Search(User.ACCOUNT, "donghuang")); 也可以使用 最后为什么要有这个项目,其实这些代码本人从2014年就陆续在写在用,在自己参与的一些项目里默默奉献。 有想过开源,奈何一直忙着修福报,此外很重要的一点是,觉得方案并不完善,还是比较长比较臭。 开源界已经有很多MyBatis相关的项目了,包括官方出品的 欢迎有兴趣的同学一起共建。 |
请发表评论