目录
起因
演示
使用
0.自定义注解
1.定义一个dto的条件查询类
2.调用主程序
效果图
小结
代码
注解
Dto类
完整代码
最近两天一直写后台管理统计的增删改查(很少写增删改查,所以不是很熟练),几乎每个表都要涉及到条件查询的业务,写了两三个条件查询之后发觉到模式还是比较固定的,所以就有了写基于mybatis-plus条件查询代码生成的念头。
高级的我不会用,也不需要,就简单的造个自己能用的轮子,把代码输出到控制台就好了,在经过一个半小时之后……
简要说明:
在字段标注Entity表明是映射实体
在字段标注Eq表明是条件查询的相等
在字段标注Ge表明是条件查询的大于等于,Le是小于等于,这两个注解都有一个column的属性,默认是create_time,表示对哪一列的大于等于或者小于等于
在字段标注Like注解就是模糊查询的字段
说明:就写了个临时的demo,所以注解没定义完全。根据这个思路是可以补全注解的。demo中字段名必须是数据库字段的驼峰形式。
这里我用到了分页
@Data
@EqualsAndHashCode(callSuper = true)
public class OrderConditionPageDto extends PageDto {
@Eq
private Integer userId;
private Integer companyId;
@Like
private String username;
private Integer status;
private Integer productId;
private String productName;
private Long tokenId;
@Ge(column = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date startTime;
@Le(column = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date endTime;
@Entity
private Order order; // 分页的实体
}
public static void main(String[] args){
annList.add(Eq.class.getName());
annList.add(Ge.class.getName());
annList.add(Le.class.getName());
annList.add(Like.class.getName());
printCode(OrderConditionPageDto.class);
}
主要用到了反射,然后特殊情况再特殊处理一下。代码写的很仓促,很多地方都是重复的是为了给代码结构分段,提供个思路 供大家参考完善或修改使用。有错误请指出。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Entity {
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Eq {
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Ge {
String column() default "create_time";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Le {
String column() default "create_time";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Like {
}
上边给过了
仅供参考
/**
* @author liutong
* @date
*/
public class GenerateMain {
public static List annList = new ArrayList<>();
public static String entityClassName;
public static void main(String[] args){
annList.add(Eq.class.getName());
annList.add(Ge.class.getName());
annList.add(Le.class.getName());
annList.add(Like.class.getName());
printCode(OrderConditionPageDto.class);
}
private static void printCode(Class> dto) {
Field[] declaredFields = dto.getDeclaredFields();
// 拿到映射实体类名
entityClassName = getEntity(declaredFields);
// 打印方法名 一行
head(dto.getName().substring(dto.getName().lastIndexOf('.')+1));
// 代码Part1
structure1(declaredFields);
// 代码Part2
structure2(declaredFields);
// 代码Part3
structure3(declaredFields);
// 打印一个括号
tail();
}
private static String getEntity(Field[] declaredFields) {
for (Field field : declaredFields){
if (field.getAnnotation(Entity.class)!=null){
return field.getType().getName();
}
}
return "none";
}
private static void structure3(Field[] declaredFields) {
System.out.println(" pageByParam = pageByParam == null ? PageUtil.getDefault() : pageByParam;");
System.out.println(" QueryWrapper<"+entityClassName+"> queryWrapper = new QueryWrapper<>();");
for (Field field : declaredFields){
String name = field.getName();
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations){
deal(field,name,annotation,3,null);
}
}
System.out.println(" Page<"+entityClassName+"> page = this.page(pageByParam, queryWrapper);");
System.out.println(" return PageUtil.res(page);");
}
private static void structure2(Field[] declaredFields) {
System.out.println(" if (!ObjectUtils.isEmpty(dto)){");
for (Field field : declaredFields){
String name = field.getName();
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations){
deal(field,name,annotation,2,null);
}
}
System.out.println(" pageByParam = PageUtil.getPageByParam(dto);");
System.out.println(" }");
}
/**
* 首字母大写
* @author liutong
* @date 2023/3/4 18:34
* @param name
* @return java.lang.String
*/
public static String captureName(String name) {
name = name.substring(0, 1).toUpperCase() + name.substring(1);
return name;
}
private static void structure1(Field[] declaredFields) {
for (Field field : declaredFields){
String name = field.getName();
Class> type = field.getType();
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations){
deal(field,name,annotation,1,type);
}
}
System.out.println(" Page<"+entityClassName+"> pageByParam = null;");
}
private static void tail() {
System.out.println(" }");
}
private static void head(String dtoName) {
System.out.println(" public BasePageVo<"+entityClassName+"> getByCondition("+dtoName+" dto) {");
}
private static void deal(Field field, String name, Annotation annotation,Integer type,Class clzss) {
for (int i1 = 0; i1 < annList.size(); i1++) {
String s = annList.get(i1);
boolean equals = annotation.annotationType().getName().equals(s);
if (equals){
if (type==3){
try {
doDeal(field,name,s.substring(s.lastIndexOf('.')).toLowerCase());
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
if (type==2){
System.out.println(" "+name+" = dto.get"+captureName(name)+"();");
}
if (type==1){
System.out.println(" "+clzss.getName()+" "+name+" = "+"null;");
}
}
}
}
private static void doDeal(Field field,String name, String s) throws NoSuchFieldException {
if (s.equals(".ge")){
Ge annotation = field.getAnnotation(Ge.class);
String column = annotation.column();
System.out.println(" queryWrapper = "+name+" == null ? queryWrapper : queryWrapper"+s+"(\""+column+"\","+name+");");
}
else if (s.equals(".le")){
Le annotation = field.getAnnotation(Le.class);
String column = annotation.column();
System.out.println(" queryWrapper = "+name+" == null ? queryWrapper : queryWrapper"+s+"(\""+column+"\","+name+");");
}else {
System.out.println(" queryWrapper = "+name+" == null ? queryWrapper : queryWrapper"+s+"(\""+humpToUnderline(name)+"\","+name+");");
}
}
/**
* 驼峰转下划线
* @author liutong
* @date 2023/3/4 18:35
* @param str
* @return java.lang.String
*/
public static String humpToUnderline(String str) {
String regex = "([A-Z])";
Matcher matcher = Pattern.compile(regex).matcher(str);
while (matcher.find()) {
String target = matcher.group();
str = str.replaceAll(target, "_"+target.toLowerCase());
}
return str;
}
}