# mybatisExampleExt **Repository Path**: denger/mybatisExampleExt ## Basic Information - **Project Name**: mybatisExampleExt - **Description**: entity 转化为 mybatis example 的一个拓展,省去各种判断和生成。 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2019-12-05 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mybatisExampleExt #### 介绍 entity 转化为 mybatis example 的一个拓展,省去各种判断和生成。 兼容官方的mybatis mapper与通用tk mapper。使用示例https://gitee.com/denger/mybatisExampleExt-demo #### 软件架构 基于jdk 1.8 为接口增加default的特性,利用反射属性根据定义的注解以及其值动态生成mybatis example。 #### 使用说明 1. 引入maven pom.xml ```xml com.gitee.denger mybatis-example-ext 1.0.2 ``` 1. entity类实现[MybatisExample](./src/main/java/com/gitee/denger/mybatis/example/ext/MybatisExample.java),必须指定其泛型,字段使用注解: @AndEqualTo @AndGreaterThan @AndGreaterThanOrEqualTo @AndIn @AndLessThan @AndLessThanOrEqualTo @AndLike @AndNotEqualTo @AndNotIn @AndNotLike ```java import com.gitee.denger.mybatis.example.ext.annotation.AndGreaterThanOrEqualTo; import com.gitee.denger.mybatis.example.ext.annotation.AndLessThanOrEqualTo; import lombok.Data; @Data public class CommonReq { /** * 创建时间开始,20190101123456 */ @AndGreaterThanOrEqualTo(property = "createTime") String createTimeStart; // 也可以是Date类型,但需要与数据库Entity的类型一致 /** * 创建时间结束,20190101123456 */ @AndLessThanOrEqualTo(property = "createTime") String createTimeEnd; // 也可以是Date类型,但需要与数据库Entity的类型一致 } ``` entity类可存在继承关系 ```java import com.gitee.denger.mybatis.example.ext.annotation.AndEqualTo; import com.gitee.denger.mybatis.example.ext.annotation.AndLike; import com.gitee.denger.mybatis.example.ext.MybatisExample; import lombok.Data; import tk.mybatis.mapper.entity.Example; /** * 注意实现的泛型 */ @Data public class ButtonRequ extends CommonReq implements MybatisExample { // 或者实现通用tk的mapper // public class ButtonRequ implements MybatisExampleForTk { /** * 主键id */ @AndEqualTo //相等 private Long id; /** * 名称 */ @AndLike // 默认为中间匹配 %value% private String name; /** * 英文名称 */ @AndLike(property = "nameEn") //处理字段名 private String englishName; } ``` 2. entity实例化的对象直接调用toExample(),请看如下单元测试 ```java @Test public void testCreateExample() { ButtonRequ requ = new ButtonRequ(); requ.setName("新增"); requ.setNameEn("add"); Example example = requ.toExample(); // 获取Example List