# AnnotationQueryWarpper
**Repository Path**: ayezs/AnnotationQueryWrapper
## Basic Information
- **Project Name**: AnnotationQueryWarpper
- **Description**: AnnotationQueryWarper是一个mybatis plus的扩展 可通过注解方式生成QueryWarper
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 7
- **Forks**: 0
- **Created**: 2021-11-02
- **Last Updated**: 2025-06-08
## Categories & Tags
**Categories**: database-dev
**Tags**: MyBatis, MybatisPlus
## README
# AnnotationQueryWrapper
#### 介绍
AnnotationQueryWarper是一个mybatis plus的扩展
可通过注解方式生成QueryWarper
[demo地址](https://gitee.com/ayezs/annotation-query-warpper-demo)
#### 使用说明
##### maven坐标
```xml
com.gitee.ayezs
AnnotationQueryWrapper
1.0.0
```
##### 不分组构建QueryWrapper
1. 在条件中加上 在querywapper中需要调用的方法对应的注解,如:
```java
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@Like
private String name;
@Ge
private Integer age;
@Eq
private Integer sex;
}
```
意思是,模糊查询name, 大于等于age, sex=sex
注意:在生成sql时,属性名默认会转驼峰,若想自定义字段名可使用 @TableField("")注解指定
若想直接使用属性名作为查询字段可在属性上加上 @RemainUnchanged注解, 此时如果使用 @TableField,@RemainUnchanged将不会生效
2. 构建QueryWrapper:
使用时非常简单,直接在创建QueryWrapper时使用AnnotationQueryWrapper创建就可以了
```java
@Service
public class UserServiceImpl extends ServiceImpl implements IUserService {
public void test(){
User user1 = new User();
user1.setName("的");
user1.setSex(1);
user1.setAge(23);
QueryWrapper queryWrapper = new AnnotationQueryWrapper<>(user1);
for (User user : baseMapper.selectList(queryWrapper)) {
System.out.println(user);
}
}
}
```
##### 分组构建QueryWrapper
1. 同上。在条件中加上 在querywapper中需要调用的方法对应的注解, 与不分组构建相比加了一个**group**参数,**group**参数默认包含一个接口**com.gitee.ayezs.annotation.wrapper.Default**,**group**参数的值**Group1**是自定义的一个接口,如果希望在不使用分组构建时继续使用该注解构建QueryWrapper,请在**Group1**中继承**Default**接口
```java
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@Like(group = {Group1.class})
private String name;
@Ge
private Integer age;
@Eq
private Integer sex;
}
```
2. 构建QueryWrapper
使用时要比不用分组构建多加一个参数 Group1.class,加上此标识后,会匹配group包含此类以及此类的子类的注解
```java
@Service
public class UserServiceImpl extends ServiceImpl implements IUserService {
public void test(){
User user1 = new User();
user1.setName("阿夜");
user1.setSex(1);
user1.setAge(23);
QueryWrapper queryWrapper = new AnnotationQueryWrapper<>(user1, Group1.class);
System.out.println(queryWrapper.getSqlSegment());
}
}
```
#####
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
### QQ群: 822212652
[](https://996.icu)